|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/log" |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +// installBusinessHandlers wires the business-connection updates to the router. It |
| 11 | +// is called from installHandlers. |
| 12 | +func (b *Bot) installBusinessHandlers() { |
| 13 | + b.disp.OnBotBusinessConnect(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotBusinessConnect) error { |
| 14 | + b.route(ctx, &Update{BusinessConnection: b.businessConnectionFromTg(u.Connection, e.Users)}) |
| 15 | + |
| 16 | + return nil |
| 17 | + }) |
| 18 | + b.disp.OnBotNewBusinessMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateBotNewBusinessMessage) error { |
| 19 | + b.dispatchBusinessMessage(ctx, u.ConnectionID, u.Message, false) |
| 20 | + |
| 21 | + return nil |
| 22 | + }) |
| 23 | + b.disp.OnBotEditBusinessMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateBotEditBusinessMessage) error { |
| 24 | + b.dispatchBusinessMessage(ctx, u.ConnectionID, u.Message, true) |
| 25 | + |
| 26 | + return nil |
| 27 | + }) |
| 28 | + b.disp.OnBotDeleteBusinessMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateBotDeleteBusinessMessage) error { |
| 29 | + chat, err := b.chatByPeer(ctx, u.Peer) |
| 30 | + if err != nil { |
| 31 | + b.logger().Error(ctx, "Convert business deleted messages", log.Error(err)) |
| 32 | + |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + b.route(ctx, &Update{DeletedBusinessMessages: &BusinessMessagesDeleted{ |
| 37 | + BusinessConnectionID: u.ConnectionID, |
| 38 | + Chat: chat, |
| 39 | + MessageIDs: u.Messages, |
| 40 | + }}) |
| 41 | + |
| 42 | + return nil |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +// dispatchBusinessMessage converts a message delivered over a business connection |
| 47 | +// and routes it as a (edited) business message. Unlike dispatchMessage it keeps |
| 48 | +// outgoing messages: the Bot API delivers the whole business conversation, |
| 49 | +// including messages the account itself sent. |
| 50 | +func (b *Bot) dispatchBusinessMessage(ctx context.Context, connectionID string, msg tg.MessageClass, edited bool) { |
| 51 | + m, err := b.messageFromTg(ctx, msg) |
| 52 | + if err != nil { |
| 53 | + b.logger().Error(ctx, "Convert business message", log.Error(err)) |
| 54 | + |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if m == nil { |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + m.BusinessConnectionID = connectionID |
| 63 | + |
| 64 | + u := &Update{} |
| 65 | + if edited { |
| 66 | + u.EditedBusinessMessage = m |
| 67 | + } else { |
| 68 | + u.BusinessMessage = m |
| 69 | + } |
| 70 | + |
| 71 | + b.route(ctx, u) |
| 72 | +} |
| 73 | + |
| 74 | +// businessConnectionID returns the connection id the update belongs to, or empty |
| 75 | +// when the update is not a business update. |
| 76 | +func (u *Update) businessConnectionID() string { |
| 77 | + switch { |
| 78 | + case u.BusinessMessage != nil: |
| 79 | + return u.BusinessMessage.BusinessConnectionID |
| 80 | + case u.EditedBusinessMessage != nil: |
| 81 | + return u.EditedBusinessMessage.BusinessConnectionID |
| 82 | + case u.BusinessConnection != nil: |
| 83 | + return u.BusinessConnection.ID |
| 84 | + case u.DeletedBusinessMessages != nil: |
| 85 | + return u.DeletedBusinessMessages.BusinessConnectionID |
| 86 | + default: |
| 87 | + return "" |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Kind predicates for business updates. |
| 92 | +func hasBusinessMessage(u *Update) bool { return u.BusinessMessage != nil } |
| 93 | +func hasEditedBusinessMessage(u *Update) bool { return u.EditedBusinessMessage != nil } |
| 94 | +func hasBusinessConnection(u *Update) bool { return u.BusinessConnection != nil } |
| 95 | +func hasDeletedBusinessMessages(u *Update) bool { return u.DeletedBusinessMessages != nil } |
| 96 | + |
| 97 | +// OnBusinessMessage registers a handler for new messages from a connected |
| 98 | +// business account. |
| 99 | +func (b *Bot) OnBusinessMessage(h Handler, predicates ...Predicate) { |
| 100 | + b.on(h, prepend(hasBusinessMessage, predicates)...) |
| 101 | +} |
| 102 | + |
| 103 | +// OnEditedBusinessMessage registers a handler for edited messages from a |
| 104 | +// connected business account. |
| 105 | +func (b *Bot) OnEditedBusinessMessage(h Handler, predicates ...Predicate) { |
| 106 | + b.on(h, prepend(hasEditedBusinessMessage, predicates)...) |
| 107 | +} |
| 108 | + |
| 109 | +// OnBusinessConnection registers a handler for business connection updates (the |
| 110 | +// bot was connected to, disconnected from, or had its rights changed on a |
| 111 | +// business account). |
| 112 | +func (b *Bot) OnBusinessConnection(h Handler, predicates ...Predicate) { |
| 113 | + b.on(h, prepend(hasBusinessConnection, predicates)...) |
| 114 | +} |
| 115 | + |
| 116 | +// OnDeletedBusinessMessages registers a handler for messages deleted from a |
| 117 | +// connected business account. |
| 118 | +func (b *Bot) OnDeletedBusinessMessages(h Handler, predicates ...Predicate) { |
| 119 | + b.on(h, prepend(hasDeletedBusinessMessages, predicates)...) |
| 120 | +} |
| 121 | + |
| 122 | +// BusinessMessage returns the business message the update carries (new or |
| 123 | +// edited), or nil when the update carries none. |
| 124 | +func (c *Context) BusinessMessage() *Message { |
| 125 | + switch { |
| 126 | + case c.Update.BusinessMessage != nil: |
| 127 | + return c.Update.BusinessMessage |
| 128 | + case c.Update.EditedBusinessMessage != nil: |
| 129 | + return c.Update.EditedBusinessMessage |
| 130 | + default: |
| 131 | + return nil |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// Business returns a BusinessContext scoped to the update's business connection, |
| 136 | +// and whether the update belongs to one. Use it to act on behalf of the |
| 137 | +// connected account from within a business update handler. |
| 138 | +func (c *Context) Business() (*BusinessContext, bool) { |
| 139 | + id := c.Update.businessConnectionID() |
| 140 | + if id == "" { |
| 141 | + return nil, false |
| 142 | + } |
| 143 | + |
| 144 | + return c.Bot.Business(id), true |
| 145 | +} |
0 commit comments