-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
625 lines (524 loc) · 15.8 KB
/
bot.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package bote
import (
"context"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/maxbolgarin/abstract"
"github.com/maxbolgarin/errm"
"github.com/maxbolgarin/lang"
tele "gopkg.in/telebot.v4"
)
// InitBundle is a struct for initing users of the bot. You should provide it to [Bot.Start] method.
// You should create a mapping of every [State] to the corresponding [HandlerFunc].
type InitBundle struct {
// Handler is a handler for [State]. It will be called for initing user.
// It is required.
Handler HandlerFunc
// Data is a callback data, that can be obtained from [Context.Data] inside [HandlerFunc].
// It is optional and shoud be provided if you use [Context.Data] in your [InitBundle.Handler].
Data string
// Text is a text of simulating message, that can be obtained from [Context.Text] inside [HandlerFunc].
// It is optional and shoud be provided if you use [Context.Text] in your [InitBundle.Handler].
Text string
// State is a state, that will be set for user after [InitBundle.Handler] is called.
// It is optional and shoud be provided if you don't want to live with the state that will be set in [InitBundle.Handler].
State State
}
// Bot is a main struct of this package. It contains all necessary components for working with Telegram bot.
type Bot struct {
bot *baseBot
um *userManagerImpl
msgs MessageProvider
rlog UpdateLogger
defaultLanguageCode string
middlewares *abstract.SafeSlice[MiddlewareFunc]
deleteMessages bool
}
// New creates the bot with optional options.
func New(ctx context.Context, token string, optsFuncs ...func(*Options)) (*Bot, error) {
var opts Options
for _, f := range optsFuncs {
f(&opts)
}
return NewWithOptions(ctx, token, opts)
}
// NewWithOptions starts the bot with options.
func NewWithOptions(ctx context.Context, token string, opts Options) (*Bot, error) {
if token == "" {
return nil, errm.New("token cannot be empty")
}
opts, err := prepareOpts(opts)
if err != nil {
return nil, errm.Wrap(err, "prepare opts")
}
um, err := newUserManager(ctx, opts.UserDB, opts.Logger)
if err != nil {
return nil, errm.Wrap(err, "new user manager")
}
b, err := newBaseBot(token, opts.Config, opts.Logger)
if err != nil {
return nil, errm.Wrap(err, "start bot")
}
bote := &Bot{
bot: b,
um: um,
msgs: opts.Msgs,
rlog: opts.UpdateLogger,
defaultLanguageCode: opts.Config.DefaultLanguageCode,
middlewares: abstract.NewSafeSlice[MiddlewareFunc](),
deleteMessages: *opts.Config.DeleteMessages,
}
b.addMiddleware(bote.masterMiddleware)
bote.AddMiddleware(bote.cleanMiddleware)
if *opts.Config.LogUpdates {
bote.AddMiddleware(bote.logMiddleware)
}
return bote, nil
}
// Start inits all users of the bot with provided stateMap. Then it starts the bot in a separate goroutine.
// It logs an error if something went wrong.
// Context is used to handle init process, it is not using by the bot.
// Don't forget to call Stop() to gracefully shutdown the bot.
func (b *Bot) Start(ctx context.Context, stateMap map[State]InitBundle, startHandler InitBundle) {
tm := abstract.StartTimer()
rp := abstract.NewRateProcessor(ctx, maxInitTasksPerSecond)
users := b.um.getAllUsersContexts()
for _, user := range users {
rp.AddTask(func(ctx context.Context) error {
err := b.initUser(user, stateMap, startHandler)
if err != nil {
return errm.Wrap(err, "init", "user_id", user.ID())
}
return nil
})
}
errs := rp.Wait()
if len(errs) > 0 {
b.bot.log.Error("there are errors in user init", "errors", errs)
}
b.bot.log.Info(fmt.Sprintf("inited %d/%d users", len(users)-len(errs), len(users)), "elapsed_time", tm.ElapsedTime())
b.bot.start()
}
// Stop gracefully shuts the poller down.
func (b *Bot) Stop() {
b.bot.stop()
}
// Bot returns the underlying *tele.Bot.
func (b *Bot) Bot() *tele.Bot {
return b.bot.tbot
}
// GetUser returns user by its ID.
func (b *Bot) GetUser(userID int64) User {
return b.um.getUser(userID)
}
// GetAllUsers returns all users.
func (b *Bot) GetAllUsers() []User {
return b.um.getAllUsers()
}
// SetMessageProvider sets message provider.
func (b *Bot) SetMessageProvider(msgs MessageProvider) {
b.msgs = msgs
}
// AddMiddleware adds middleware functions that will be called on each update.
func (b *Bot) AddMiddleware(f ...MiddlewareFunc) {
b.middlewares.Append(f...)
}
// Handle sets handler for any endpoint. Endpoint can be string or callback button.
func (b *Bot) Handle(endpoint any, f HandlerFunc) {
b.bot.handle(endpoint, func(c tele.Context) (err error) {
defer lang.RecoverWithErrAndStack(b.bot.log, &err)
ctx := b.newContext(c)
if err = f(ctx); err != nil {
return ctx.handleError(err)
}
if c.Callback() != nil {
c.Respond(&tele.CallbackResponse{})
}
return nil
})
}
// HandleText sets handler for text messages.
// You should provide a single handler for all text messages, that will call another handlers based on the state.
func (b *Bot) HandleText(f HandlerFunc) {
b.bot.handle(tele.OnText, func(c tele.Context) (err error) {
defer lang.RecoverWithErrAndStack(b.bot.log, &err)
ctx := b.newContext(c)
if !ctx.user.hasTextMessages() {
return nil
}
ctx.textMsgID = ctx.user.lastTextMessage()
return ctx.handleError(f(ctx))
})
}
// HandleStart sets handler for start command.
func (b *Bot) HandleStart(h HandlerFunc, commands ...string) {
if len(commands) > 0 {
for _, c := range commands {
b.Handle(c, h)
}
return
}
b.Handle("/start", h)
}
func (b *Bot) masterMiddleware(upd *tele.Update) bool {
defer lang.Recover(b.bot.log)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
sender := getSender(upd)
user, err := b.um.prepareUser(ctx, sender)
if err != nil {
b.bot.log.Error("cannot prepare user", "error", err, "user_id", sender.ID, "username", sender.Username)
b.bot.send(sender.ID, b.msgs.Messages(b.defaultLanguageCode).GeneralError())
return false
}
return b.middlewares.Range(func(mf MiddlewareFunc) bool {
return mf(upd, user)
})
}
func (b *Bot) cleanMiddleware(upd *tele.Update, userRaw User) bool {
user := userRaw.(*userContextImpl)
msgIDs := user.Messages()
if msgIDs.ErrorID > 0 {
b.bot.delete(user.ID(), msgIDs.ErrorID)
user.setErrorMessage(0)
}
if upd.Message != nil && b.deleteMessages {
b.bot.delete(user.ID(), upd.Message.ID)
}
// TODO: sanitize
return true
}
var cbackRx = regexp.MustCompile(`^\f([-\w]+)(\|(.+))?$`)
func (b *Bot) logMiddleware(upd *tele.Update, userRaw User) bool {
user := userRaw.(*userContextImpl)
fields := make([]any, 0, 7)
fields = append(fields,
"user_id", user.ID(),
"username", user.Username(),
)
switch {
case upd.Message != nil:
fields = append(fields, "state", user.StateMain(), "msg_id", upd.Message.ID, "text", maxLen(upd.Message.Text, MaxTextLenInLogs))
if user.hasTextMessages() {
msgID := user.lastTextMessage()
ts, _ := user.State(msgID)
fields = append(fields, "text_state", ts, "text_state_msg_id", msgID)
}
b.rlog.Log(MessageUpdate, fields...)
case upd.Callback != nil:
var (
payload = upd.Callback.Data
button string
)
if upd.Callback.Message != nil {
st, _ := user.State(upd.Callback.Message.ID)
fields = append(fields, "state", st, "msg_id", upd.Callback.Message.ID)
} else {
fields = append(fields, "state", user.StateMain())
}
if match := cbackRx.FindAllStringSubmatch(payload, -1); match != nil {
button, payload = match[0][1], match[0][3]
button = parseBtnUnique(button)
fields = lang.AppendIfAll(fields, "button", any(button))
}
fields = lang.AppendIfAll(fields, "payload", any(payload))
b.rlog.Log(CallbackUpdate, fields...)
return true
}
return true
}
func userFields(user User, fields ...any) []any {
f := make([]any, 0, len(fields)+6)
f = append(f, "user_id", user.ID(), "username", user.Username(), "state", user.StateMain())
return append(f, fields...)
}
func (b *Bot) initUser(user *userContextImpl, stateMap map[State]InitBundle, startHandler InitBundle) error {
msgsToInit := append(user.Messages().HistoryIDs, user.Messages().MainID)
for i, j := 0, len(msgsToInit)-1; i < j; i, j = i+1, j-1 {
msgsToInit[i], msgsToInit[j] = msgsToInit[j], msgsToInit[i] // reverse to init from first in msg list
}
errs := errm.NewList()
var msgWithoutState []int
for _, msgID := range msgsToInit {
st, ok := user.State(msgID)
if !ok {
msgWithoutState = append(msgWithoutState, msgID)
continue
}
bundle, foundBundle := stateMap[st]
if !ok {
bundle = startHandler
}
firstError := b.init(bundle, user, msgID, st)
if firstError != nil {
if !foundBundle {
errs.Wrap(firstError, "start handler", "msg_id", msgID, "state", st)
continue
}
secondError := b.init(startHandler, user, msgID, st)
if secondError != nil {
errs.Wrap(secondError, "start handler", "msg_id", msgID, "state", st, "first_error", firstError)
continue
}
}
b.bot.log.Debug("init", "user_id", user.ID(), "msg_id", msgID, "state", st)
}
for _, msgID := range msgWithoutState {
b.bot.log.Debug("forget history message", "user_id", user.ID(), "msg_id", msgID)
user.forgetHistoryMessage(msgID)
}
return errs.Err()
}
func (b *Bot) init(bundle InitBundle, user *userContextImpl, msgID int, expectedState State) error {
// Minimum update to handle all possible methods in [Context]
upd := tele.Update{
Message: &tele.Message{
Text: bundle.Text,
Sender: &tele.User{
ID: user.ID(),
},
},
Callback: &tele.Callback{
Message: &tele.Message{ID: msgID},
Data: bundle.Data,
},
}
err := bundle.Handler(&contextImpl{
bt: b,
ct: b.bot.tbot.NewContext(upd),
user: user,
})
if err != nil {
return errm.Wrap(err, "run handler")
}
// Expected state not changed after running handler
if bundle.State.NotChanged() {
newState, ok := user.State(msgID)
if !ok {
return errm.New("new state not found")
}
if newState != expectedState {
return errm.New("unexpected", "state", newState)
}
return nil
}
user.setState(bundle.State, msgID)
return nil
}
var (
errEmptyUserID = errm.New("empty user id")
errEmptyMsgID = errm.New("empty msg id")
)
type baseBot struct {
tbot *tele.Bot
log Logger
defaultOptions []any
middlewares []func(upd *tele.Update) bool
}
func newBaseBot(token string, cfg Config, log Logger) (*baseBot, error) {
b := &baseBot{
log: log,
defaultOptions: []any{cfg.ParseMode},
middlewares: make([]func(upd *tele.Update) bool, 0),
}
if cfg.NoPreview {
b.defaultOptions = append(b.defaultOptions, tele.NoPreview)
}
bot, err := tele.NewBot(tele.Settings{
Token: token,
Poller: tele.NewMiddlewarePoller(&tele.LongPoller{Timeout: cfg.LPTimeout}, b.middleware),
Client: &http.Client{Timeout: 2 * cfg.LPTimeout},
Verbose: cfg.Debug,
OnError: func(err error, ctx tele.Context) {
var userID int64
if ctx != nil && ctx.Chat() != nil {
userID = ctx.Chat().ID
}
b.log.Error("error callback", "error", err, "user_id", userID)
},
})
if err != nil {
return nil, errm.Wrap(err, "new telebot")
}
b.tbot = bot
return b, nil
}
func (b *baseBot) start() {
b.log.Info("bot is starting")
lang.Go(b.log, b.tbot.Start)
}
func (b *baseBot) stop() {
b.log.Info("bot is stopping")
b.tbot.Stop()
}
func (b *baseBot) addMiddleware(f func(upd *tele.Update) bool) {
b.middlewares = append(b.middlewares, f)
}
func (b *baseBot) handle(endpoint any, handler tele.HandlerFunc) {
b.tbot.Handle(endpoint, handler)
}
func (b *baseBot) send(userID int64, msg string, options ...any) (int, error) {
if userID == 0 {
return 0, errEmptyUserID
}
m, err := b.tbot.Send(userIDWrapper(userID), msg, append(options, b.defaultOptions...)...)
if err != nil {
return 0, err
}
return m.ID, nil
}
func (b *baseBot) edit(userID int64, msgID int, what any, options ...any) error {
if userID == 0 {
return errEmptyUserID
}
if msgID == 0 {
return errEmptyMsgID
}
_, err := b.tbot.Edit(getEditable(userID, msgID), what, append(options, b.defaultOptions...)...)
if err != nil {
if strings.Contains(err.Error(), "message is not modified") {
b.log.Warn("message is not modified", "msg_id", msgID, "user_id", userID)
return nil
}
return err
}
return nil
}
func (b *baseBot) editReplyMarkup(userID int64, msgID int, markup *tele.ReplyMarkup) error {
if userID == 0 {
return errEmptyUserID
}
if msgID == 0 {
return errEmptyMsgID
}
_, err := b.tbot.EditReplyMarkup(getEditable(userID, msgID), markup)
if err != nil {
return err
}
return nil
}
func (b *baseBot) delete(userID int64, msgIDs ...int) error {
if userID == 0 {
return errEmptyUserID
}
errSet := errm.NewList()
for _, msgID := range msgIDs {
if msgID == 0 {
return errEmptyMsgID
}
if err := b.tbot.Delete(getEditable(userID, msgID)); err != nil {
errSet.Add(err)
}
}
return errSet.Err()
}
func (b *baseBot) deleteHistory(userID int64, lastMessageID int) map[int]struct{} {
deleted := map[int]struct{}{}
var counter int
for msgID := lastMessageID - 1; msgID > 1; msgID-- {
err := b.delete(userID, msgID)
if err != nil {
counter += 1
} else {
counter = 0
deleted[msgID] = struct{}{}
}
if counter == 5 {
break
}
time.Sleep(100 * time.Millisecond)
}
return deleted
}
func (b *baseBot) middleware(upd *tele.Update) bool {
if upd.MyChatMember != nil {
if lang.Deref(upd.MyChatMember.NewChatMember).Role == "kicked" {
b.log.Warn("bot is blocked",
"user_id", lang.Deref(upd.MyChatMember.Sender).ID,
"username", lang.Deref(upd.MyChatMember.Sender).Username,
"old_role", lang.Deref(upd.MyChatMember.OldChatMember).Role,
"new_role", lang.Deref(upd.MyChatMember.NewChatMember).Role)
return false
}
if lang.Deref(upd.MyChatMember.OldChatMember).Role == "kicked" {
b.log.Info("bot is unblocked",
"user_id", lang.Deref(upd.MyChatMember.Sender).ID,
"username", lang.Deref(upd.MyChatMember.Sender).Username,
"old_role", lang.Deref(upd.MyChatMember.OldChatMember).Role,
"new_role", lang.Deref(upd.MyChatMember.NewChatMember).Role)
return false
}
}
for _, m := range b.middlewares {
if !m(upd) {
return false
}
}
return true
}
type userIDWrapper int64
func (u userIDWrapper) Recipient() string {
return strconv.Itoa(int(u))
}
type noopLogger struct{}
func (noopLogger) Debug(msg string, fields ...any) {}
func (noopLogger) Info(msg string, fields ...any) {}
func (noopLogger) Warn(msg string, fields ...any) {}
func (noopLogger) Error(msg string, fields ...any) {}
type updateLogger struct {
l Logger
}
func (r *updateLogger) Log(t UpdateType, fields ...any) {
r.l.Debug(t.String(), fields...)
}
func getEditable(senderID int64, messageID int) tele.Editable {
return &tele.Message{ID: messageID, Chat: &tele.Chat{ID: senderID}}
}
func maxLen(s string, max int) string {
if len(s) <= max {
return s
}
return s[:max]
}
func getSender(upd *tele.Update) *tele.User {
switch {
case upd.Callback != nil:
return upd.Callback.Sender
case upd.Message != nil:
return upd.Message.Sender
case upd.Query != nil:
return upd.Query.Sender
case upd.MessageReaction != nil:
return upd.MessageReaction.User
case upd.InlineResult != nil:
return upd.InlineResult.Sender
case upd.MyChatMember != nil:
return upd.MyChatMember.Sender
case upd.EditedMessage != nil:
return upd.EditedMessage.Sender
case upd.ShippingQuery != nil:
return upd.ShippingQuery.Sender
case upd.ChannelPost != nil:
return upd.ChannelPost.Sender
case upd.EditedChannelPost != nil:
return upd.EditedChannelPost.Sender
case upd.PreCheckoutQuery != nil:
return upd.PreCheckoutQuery.Sender
case upd.PollAnswer != nil:
return upd.PollAnswer.Sender
case upd.ChatJoinRequest != nil:
return upd.ChatJoinRequest.Sender
case upd.BusinessMessage != nil:
return upd.BusinessMessage.Sender
case upd.BusinessConnection != nil:
return upd.BusinessConnection.Sender
case upd.EditedBusinessMessage != nil:
return upd.EditedBusinessMessage.Sender
default:
return nil
}
}