|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/tg" |
| 7 | + "go.uber.org/zap" |
| 8 | +) |
| 9 | + |
| 10 | +// installHandlers wires the raw tg.UpdateDispatcher to the Bot API router. It is |
| 11 | +// called once from New. Update-conversion failures are logged and swallowed so |
| 12 | +// a single bad update never tears down the update stream. |
| 13 | +func (b *Bot) installHandlers() { |
| 14 | + b.disp.OnNewMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateNewMessage) error { |
| 15 | + b.dispatchMessage(ctx, u.Message, false) |
| 16 | + return nil |
| 17 | + }) |
| 18 | + b.disp.OnEditMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateEditMessage) error { |
| 19 | + b.dispatchMessage(ctx, u.Message, true) |
| 20 | + return nil |
| 21 | + }) |
| 22 | + b.disp.OnNewChannelMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateNewChannelMessage) error { |
| 23 | + b.dispatchMessage(ctx, u.Message, false) |
| 24 | + return nil |
| 25 | + }) |
| 26 | + b.disp.OnEditChannelMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateEditChannelMessage) error { |
| 27 | + b.dispatchMessage(ctx, u.Message, true) |
| 28 | + return nil |
| 29 | + }) |
| 30 | + b.disp.OnBotCallbackQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotCallbackQuery) error { |
| 31 | + b.route(ctx, &Update{CallbackQuery: callbackQueryFromTg(e, u)}) |
| 32 | + return nil |
| 33 | + }) |
| 34 | + b.disp.OnBotInlineQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotInlineQuery) error { |
| 35 | + b.route(ctx, &Update{InlineQuery: inlineQueryFromTg(e, u)}) |
| 36 | + return nil |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +// dispatchMessage converts a message and routes it as the appropriate update |
| 41 | +// field. Channel-broadcast messages become channel posts; everything else is a |
| 42 | +// regular message. edited selects the edited_* fields. |
| 43 | +func (b *Bot) dispatchMessage(ctx context.Context, msg tg.MessageClass, edited bool) { |
| 44 | + m, err := b.messageFromTg(ctx, msg) |
| 45 | + if err != nil { |
| 46 | + b.log.Error("Convert message", zap.Error(err)) |
| 47 | + return |
| 48 | + } |
| 49 | + if m == nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + u := &Update{} |
| 54 | + switch { |
| 55 | + case m.Chat.Type == ChatTypeChannel && edited: |
| 56 | + u.EditedChannelPost = m |
| 57 | + case m.Chat.Type == ChatTypeChannel: |
| 58 | + u.ChannelPost = m |
| 59 | + case edited: |
| 60 | + u.EditedMessage = m |
| 61 | + default: |
| 62 | + u.Message = m |
| 63 | + } |
| 64 | + b.route(ctx, u) |
| 65 | +} |
| 66 | + |
| 67 | +// OnMessage registers a handler for new messages matching the predicates. |
| 68 | +func (b *Bot) OnMessage(h Handler, predicates ...Predicate) { |
| 69 | + b.on(h, prepend(func(u *Update) bool { return u.Message != nil }, predicates)...) |
| 70 | +} |
| 71 | + |
| 72 | +// OnEditedMessage registers a handler for edited messages. |
| 73 | +func (b *Bot) OnEditedMessage(h Handler, predicates ...Predicate) { |
| 74 | + b.on(h, prepend(func(u *Update) bool { return u.EditedMessage != nil }, predicates)...) |
| 75 | +} |
| 76 | + |
| 77 | +// OnChannelPost registers a handler for new channel posts. |
| 78 | +func (b *Bot) OnChannelPost(h Handler, predicates ...Predicate) { |
| 79 | + b.on(h, prepend(func(u *Update) bool { return u.ChannelPost != nil }, predicates)...) |
| 80 | +} |
| 81 | + |
| 82 | +// OnCallbackQuery registers a handler for callback queries from inline keyboards. |
| 83 | +func (b *Bot) OnCallbackQuery(h Handler, predicates ...Predicate) { |
| 84 | + b.on(h, prepend(func(u *Update) bool { return u.CallbackQuery != nil }, predicates)...) |
| 85 | +} |
| 86 | + |
| 87 | +// OnInlineQuery registers a handler for inline queries. |
| 88 | +func (b *Bot) OnInlineQuery(h Handler, predicates ...Predicate) { |
| 89 | + b.on(h, prepend(func(u *Update) bool { return u.InlineQuery != nil }, predicates)...) |
| 90 | +} |
| 91 | + |
| 92 | +// prepend returns p followed by rest, without mutating rest. |
| 93 | +func prepend(p Predicate, rest []Predicate) []Predicate { |
| 94 | + out := make([]Predicate, 0, len(rest)+1) |
| 95 | + out = append(out, p) |
| 96 | + out = append(out, rest...) |
| 97 | + return out |
| 98 | +} |
0 commit comments