-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
74 lines (65 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"log"
"sync"
"time"
"github.com/mr-linch/go-tg"
"github.com/mr-linch/go-tg/examples"
"github.com/mr-linch/go-tg/tgb"
)
func main() {
pm := tg.HTML
onStart := func(ctx context.Context, msg *tgb.MessageUpdate) error {
return msg.Answer(pm.Text(
"👋 Hi, I'm retry flood demo, send me /spam command for start.",
"🔁 I will retry when receive flood wait error",
"Stop spam with shutdown bot service",
)).DoVoid(ctx)
}
onSpam := func(ctx context.Context, mu *tgb.MessageUpdate) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if err := mu.Answer(pm.Text("🔁 spamming...")).DoVoid(ctx); err != nil {
log.Printf("answer: %v", err)
}
}()
}
wg.Wait()
}
}
}
examples.Run(tgb.NewRouter().
Message(onSpam, tgb.Command("spam")).
ChannelPost(onSpam, tgb.Command("spam")).
Message(onStart).
ChannelPost(onStart).
Error(func(ctx context.Context, update *tgb.Update, err error) error {
log.Printf("error in handler: %v", err)
return nil
}),
tg.WithClientInterceptors(
tg.Interceptor(func(ctx context.Context, req *tg.Request, dst any, invoker tg.InterceptorInvoker) error {
defer func(started time.Time) {
log.Printf("request: %s took: %s", req.Method, time.Since(started))
}(time.Now())
return invoker(ctx, req, dst)
}),
tg.NewInterceptorRetryFloodError(
// we override the default timeAfter function to log the retry flood delay
tg.WithInterceptorRetryFloodErrorTimeAfter(func(sleep time.Duration) <-chan time.Time {
log.Printf("retry flood error after %s", sleep)
return time.After(sleep)
}),
),
),
)
}