Skip to content

Commit fbef619

Browse files
authored
Merge pull request #464 from fluffur/pass-tg-entities-to-update
feat(update): pass tg Entities to Update
2 parents 26467f4 + 2863d26 commit fbef619

6 files changed

Lines changed: 33 additions & 22 deletions

File tree

business_updates.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ func (b *Bot) dispatchBusinessMessage(ctx context.Context, e tg.Entities, connec
7171
}
7272

7373
u := &Update{}
74+
75+
u.Entities = e
76+
7477
if edited {
7578
u.EditedBusinessMessage = m
7679
} else {

coverage_dispatch_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ func TestDispatchMessageRouting(t *testing.T) {
3434

3535
regular := &tg.Message{ID: 1, Message: "x", PeerID: &tg.PeerUser{UserID: 10}}
3636
regular.SetFromID(&tg.PeerUser{UserID: 10})
37-
b.dispatchMessage(ctx, regular, false)
38-
b.dispatchMessage(ctx, regular, true)
37+
38+
e := tg.Entities{}
39+
b.dispatchMessage(ctx, e, regular, false)
40+
b.dispatchMessage(ctx, e, regular, true)
3941

4042
channelMsg := &tg.Message{ID: 2, Message: "x", PeerID: &tg.PeerChannel{ChannelID: 50}}
41-
b.dispatchMessage(ctx, channelMsg, false)
42-
b.dispatchMessage(ctx, channelMsg, true) // edited channel post (no registrar; exercises the switch)
43+
b.dispatchMessage(ctx, e, channelMsg, false)
44+
b.dispatchMessage(ctx, e, channelMsg, true) // edited channel post (no registrar; exercises the switch)
4345

4446
// A service message converts to nil and routes nothing.
45-
b.dispatchMessage(ctx, &tg.MessageService{ID: 3, PeerID: &tg.PeerUser{UserID: 10}}, false)
47+
b.dispatchMessage(ctx, e, &tg.MessageService{ID: 3, PeerID: &tg.PeerUser{UserID: 10}}, false)
4648

4749
want := []string{"message", "edited", "channel"}
4850
if len(fired) != len(want) {

dispatch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestDispatchMessageDropsOutgoing(t *testing.T) {
167167

168168
b.OnMessage(func(c *Context) error { fired = true; return nil })
169169
// An outgoing (bot's own) message must be dropped, not dispatched.
170-
b.dispatchMessage(context.Background(), &tg.Message{Out: true, Message: "self"}, false)
170+
b.dispatchMessage(context.Background(), tg.Entities{}, &tg.Message{Out: true, Message: "self"}, false)
171171

172172
if fired {
173173
t.Fatal("outgoing message must not be dispatched")

on.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,53 @@ import (
1111
// called once from New. Update-conversion failures are logged and swallowed so
1212
// a single bad update never tears down the update stream.
1313
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)
14+
b.disp.OnNewMessage(func(ctx context.Context, e tg.Entities, u *tg.UpdateNewMessage) error {
15+
b.dispatchMessage(ctx, e, u.Message, false)
1616

1717
return nil
1818
})
19-
b.disp.OnEditMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateEditMessage) error {
20-
b.dispatchMessage(ctx, u.Message, true)
19+
b.disp.OnEditMessage(func(ctx context.Context, e tg.Entities, u *tg.UpdateEditMessage) error {
20+
b.dispatchMessage(ctx, e, u.Message, true)
2121

2222
return nil
2323
})
24-
b.disp.OnNewChannelMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateNewChannelMessage) error {
25-
b.dispatchMessage(ctx, u.Message, false)
24+
b.disp.OnNewChannelMessage(func(ctx context.Context, e tg.Entities, u *tg.UpdateNewChannelMessage) error {
25+
b.dispatchMessage(ctx, e, u.Message, false)
2626

2727
return nil
2828
})
29-
b.disp.OnEditChannelMessage(func(ctx context.Context, _ tg.Entities, u *tg.UpdateEditChannelMessage) error {
30-
b.dispatchMessage(ctx, u.Message, true)
29+
b.disp.OnEditChannelMessage(func(ctx context.Context, e tg.Entities, u *tg.UpdateEditChannelMessage) error {
30+
b.dispatchMessage(ctx, e, u.Message, true)
3131

3232
return nil
3333
})
3434
b.disp.OnBotCallbackQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotCallbackQuery) error {
35-
b.route(ctx, &Update{CallbackQuery: callbackQueryFromTg(e, u)})
35+
b.route(ctx, &Update{CallbackQuery: callbackQueryFromTg(e, u), Entities: e})
3636

3737
return nil
3838
})
3939
b.disp.OnBotInlineQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotInlineQuery) error {
40-
b.route(ctx, &Update{InlineQuery: inlineQueryFromTg(e, u)})
40+
b.route(ctx, &Update{InlineQuery: inlineQueryFromTg(e, u), Entities: e})
4141

4242
return nil
4343
})
4444
b.disp.OnBotInlineSend(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotInlineSend) error {
45-
b.route(ctx, &Update{ChosenInlineResult: chosenInlineResultFromTg(e, u)})
45+
b.route(ctx, &Update{ChosenInlineResult: chosenInlineResultFromTg(e, u), Entities: e})
4646

4747
return nil
4848
})
4949
b.disp.OnInlineBotCallbackQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateInlineBotCallbackQuery) error {
50-
b.route(ctx, &Update{CallbackQuery: inlineCallbackQueryFromTg(e, u)})
50+
b.route(ctx, &Update{CallbackQuery: inlineCallbackQueryFromTg(e, u), Entities: e})
5151

5252
return nil
5353
})
5454
b.disp.OnBotShippingQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotShippingQuery) error {
55-
b.route(ctx, &Update{ShippingQuery: shippingQueryFromTg(e, u)})
55+
b.route(ctx, &Update{ShippingQuery: shippingQueryFromTg(e, u), Entities: e})
5656

5757
return nil
5858
})
5959
b.disp.OnBotPrecheckoutQuery(func(ctx context.Context, e tg.Entities, u *tg.UpdateBotPrecheckoutQuery) error {
60-
b.route(ctx, &Update{PreCheckoutQuery: preCheckoutQueryFromTg(e, u)})
60+
b.route(ctx, &Update{PreCheckoutQuery: preCheckoutQueryFromTg(e, u), Entities: e})
6161

6262
return nil
6363
})
@@ -67,7 +67,7 @@ func (b *Bot) installHandlers() {
6767
// dispatchMessage converts a message and routes it as the appropriate update
6868
// field. Channel-broadcast messages become channel posts; everything else is a
6969
// regular message. edited selects the edited_* fields.
70-
func (b *Bot) dispatchMessage(ctx context.Context, msg tg.MessageClass, edited bool) {
70+
func (b *Bot) dispatchMessage(ctx context.Context, e tg.Entities, msg tg.MessageClass, edited bool) {
7171
// Drop the bot's own outgoing messages. MTProto echoes them back on the
7272
// update stream, but the HTTP Bot API never delivers them — without this a
7373
// bot that replies to messages would answer its own replies in a loop.
@@ -88,6 +88,8 @@ func (b *Bot) dispatchMessage(ctx context.Context, msg tg.MessageClass, edited b
8888

8989
u := &Update{}
9090

91+
u.Entities = e
92+
9193
switch {
9294
case m.Chat.Type == ChatTypeChannel && edited:
9395
u.EditedChannelPost = m

on_self_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestDispatchSkipsOwnOutgoingMessage(t *testing.T) {
2222
Message: "echo",
2323
PeerID: &tg.PeerUser{UserID: 42},
2424
}
25-
b.dispatchMessage(context.Background(), own, false)
25+
b.dispatchMessage(context.Background(), tg.Entities{}, own, false)
2626

2727
if fired {
2828
t.Fatal("handler fired for the bot's own outgoing message")

update.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package botapi
22

3+
import "github.com/gotd/td/tg"
4+
35
// Update represents one incoming update. At most one of the optional fields is
46
// present in any given update.
57
type Update struct {
@@ -30,6 +32,8 @@ type Update struct {
3032
// account.
3133
DeletedBusinessMessages *BusinessMessagesDeleted `json:"deleted_business_messages,omitempty"`
3234

35+
Entities tg.Entities `json:"-"`
36+
3337
// botUsername is this bot's @username (without the @), set by the router so
3438
// command predicates can tell a command targeted at this bot ("/cmd@me")
3539
// from one targeted at another ("/cmd@other"). Not part of the Bot API

0 commit comments

Comments
 (0)