From 23a492b5403e8af9603a0722da4f92d914d5e9d6 Mon Sep 17 00:00:00 2001 From: InkCrow Date: Thu, 19 Nov 2026 23:08:32 +0800 Subject: [PATCH] fix: match commands addressed to the bot by username --- README.md | 2 +- bot.go | 34 ++++++++++- bot_test.go | 143 ++++++++++++++++++++++++++++++++++++++++++++++ handlers.go | 30 +++++++--- handlers_test.go | 96 ++++++++++++++++++++++--------- methods.go | 6 ++ options.go | 4 +- process_update.go | 3 +- 8 files changed, 279 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 42e34e19..c0e9f736 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,7 @@ b, err := bot.New("YOUR_BOT_TOKEN_FROM_BOTFATHER", opts...) - `WithDebugHandler(handler DebugHandler)` - add debug handler - `WithHTTPClient(pollTimeout time.Duration, client HttpClient)` - set custom http client - `WithServerURL(serverURL string)` - set server url -- `WithSkipGetMe()` - skip call GetMe on bot init +- `WithSkipGetMe()` - skip call GetMe on bot init. A command handler for a bare command such as `start` then matches `/start@bot_username` only after `GetMe` succeeds. - `WithAllowedUpdates(params AllowedUpdates)` - set [allowed_updates](https://core.telegram.org/bots/api#getupdates) for getUpdates method - `WithUpdatesChannelCap(cap int)` - set updates channel capacity, by default 1024 - `WithWebhookSecretToken(webhookSecretToken string)` - set X-Telegram-Bot-Api-Secret-Token header sent from telegram servers to confirm validity of update diff --git a/bot.go b/bot.go index 6a792e7d..15087c12 100644 --- a/bot.go +++ b/bot.go @@ -34,6 +34,13 @@ type MatchFunc func(update *models.Update) bool type Bot struct { lastUpdateID int64 + // usernameMx guards username, the username from the last successful GetMe + // call for the bot the token belongs to. SetToken holds it while changing the + // token, so checking the bot ID and clearing the username happen together; + // other code still reads the token without it. + usernameMx sync.RWMutex + username string + url string token string pollTimeout time.Duration @@ -117,9 +124,34 @@ func (b *Bot) ID() int64 { return i } -// SetToken sets the bot token +// SetToken sets the bot token. If the token belongs to a different bot, by its +// ID prefix, the username recorded by GetMe is cleared until GetMe succeeds +// again. A new token for the same bot, as after revoking the old one, keeps it. func (b *Bot) SetToken(token string) { + b.usernameMx.Lock() + defer b.usernameMx.Unlock() + previousID := b.ID() b.token = token + if b.ID() != previousID { + b.username = "" + } +} + +// setUsername records the username GetMe returned, unless the token now +// belongs to another bot, as when it was switched during the request. The +// token starts with the bot's ID, which GetMe returns as the user ID. +func (b *Bot) setUsername(me *models.User) { + b.usernameMx.Lock() + defer b.usernameMx.Unlock() + if me.ID == b.ID() { + b.username = me.Username + } +} + +func (b *Bot) botUsername() string { + b.usernameMx.RLock() + defer b.usernameMx.RUnlock() + return b.username } // Token returns the bot token diff --git a/bot_test.go b/bot_test.go index bfe9899c..9eedb0ee 100644 --- a/bot_test.go +++ b/bot_test.go @@ -3,9 +3,11 @@ package bot import ( "context" "encoding/json" + "errors" "io" "net/http" "net/http/httptest" + "reflect" "strings" "sync" "testing" @@ -412,3 +414,144 @@ func TestBot_SetToken(t *testing.T) { t.Errorf("SetToken() = %s, want %s", b.token, "123456:xxx") } } + +func getMeClient(t *testing.T, username string) HttpClient { + return getUpdatesClientFunc(func(req *http.Request) (*http.Response, error) { + if !strings.HasSuffix(req.URL.Path, "/getMe") { + t.Errorf("unexpected request %s", req.URL.Path) + } + return getUpdatesJSONResponse(`{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"bot","username":"` + username + `"}}`), nil + }) +} + +func addressedCommandUpdate(text string) *models.Update { + return &models.Update{Message: &models.Message{ + Text: text, + Entities: []models.MessageEntity{{Type: models.MessageEntityTypeBotCommand, Offset: 0, Length: len(text)}}, + }} +} + +func TestBot_ProcessUpdateMatchesCommandAddressedToBot(t *testing.T) { + var handled []string + b, err := New("1:token", WithHTTPClient(time.Second, getMeClient(t, "my_bot")), WithNotAsyncHandlers(), + WithDefaultHandler(func(_ context.Context, _ *Bot, update *models.Update) { + handled = append(handled, "default "+update.Message.Text) + })) + if err != nil { + t.Fatal(err) + } + b.RegisterHandler(HandlerTypeMessageText, "start", MatchTypeCommandStartOnly, func(_ context.Context, _ *Bot, update *models.Update) { + handled = append(handled, "start "+update.Message.Text) + }) + + b.ProcessUpdate(context.Background(), addressedCommandUpdate("/start@my_bot")) + b.ProcessUpdate(context.Background(), addressedCommandUpdate("/start@other_bot")) + + want := []string{"start /start@my_bot", "default /start@other_bot"} + if !reflect.DeepEqual(handled, want) { + t.Errorf("handled %q, want %q", handled, want) + } +} + +func TestBot_SetTokenForgetsUsernameOfAnotherBot(t *testing.T) { + b, err := New("1:token", WithHTTPClient(time.Second, getMeClient(t, "my_bot"))) + if err != nil { + t.Fatal(err) + } + b.SetToken("1:revoked-and-replaced") + if got := b.botUsername(); got != "my_bot" { + t.Errorf("username after a new token of the same bot = %q, want my_bot", got) + } + b.SetToken("2:token") + if got := b.botUsername(); got != "" { + t.Errorf("username after switching to another bot = %q, want empty", got) + } +} + +func TestBot_GetMeIgnoresUsernameOfAnotherBot(t *testing.T) { + var b *Bot + // GetMe is sent with bot 2's token, and the token is switched back to bot 1 + // before the reply arrives, so the reply describes another bot. + b, err := New("1:token", WithSkipGetMe(), WithHTTPClient(time.Second, getUpdatesClientFunc(func(req *http.Request) (*http.Response, error) { + b.SetToken("1:token") + return getUpdatesJSONResponse(`{"ok":true,"result":{"id":2,"is_bot":true,"first_name":"bot","username":"other_bot"}}`), nil + }))) + if err != nil { + t.Fatal(err) + } + b.SetToken("2:token") + if _, err := b.GetMe(context.Background()); err != nil { + t.Fatal(err) + } + if got := b.botUsername(); got != "" { + t.Errorf("username = %q, want empty for a GetMe answered by another bot", got) + } +} + +func TestBot_GetMeFailureKeepsUsername(t *testing.T) { + fail := false + b, err := New("1:token", WithHTTPClient(time.Second, getUpdatesClientFunc(func(req *http.Request) (*http.Response, error) { + if fail { + return nil, errors.New("network down") + } + return getUpdatesJSONResponse(`{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"bot","username":"my_bot"}}`), nil + }))) + if err != nil { + t.Fatal(err) + } + fail = true + if _, err := b.GetMe(context.Background()); err == nil { + t.Fatal("GetMe succeeded, want an error") + } + if got := b.botUsername(); got != "my_bot" { + t.Errorf("username after a failed GetMe = %q, want my_bot", got) + } +} + +func TestBot_GetMeDecodeErrorRecordsNoUsername(t *testing.T) { + b, err := New("1:token", WithSkipGetMe(), WithHTTPClient(time.Second, getUpdatesClientFunc(func(*http.Request) (*http.Response, error) { + // The id and username decode even though is_bot fails. + return getUpdatesJSONResponse(`{"ok":true,"result":{"id":1,"username":"my_bot","is_bot":"yes"}}`), nil + }))) + if err != nil { + t.Fatal(err) + } + if _, err := b.GetMe(context.Background()); err == nil { + t.Fatal("GetMe succeeded, want a decode error") + } + if got := b.botUsername(); got != "" { + t.Errorf("username = %q, want empty after a failed GetMe", got) + } +} + +// Run with -race: the username is read while it is set and while the token +// changes. +func TestBot_UsernameConcurrentAccess(t *testing.T) { + b := &Bot{token: "1:token"} + var wg sync.WaitGroup + for i := 0; i < 4; i++ { + wg.Add(3) + go func() { + defer wg.Done() + for j := 0; j < 100; j++ { + b.setUsername(&models.User{ID: 1, Username: "my_bot"}) + } + }() + go func() { + defer wg.Done() + for j := 0; j < 100; j++ { + b.SetToken("2:token") + b.SetToken("1:token") + } + }() + go func() { + defer wg.Done() + for j := 0; j < 100; j++ { + if got := b.botUsername(); got != "" && got != "my_bot" { + t.Errorf("username = %q", got) + } + } + }() + } + wg.Wait() +} diff --git a/handlers.go b/handlers.go index da9264b3..5cf6677f 100644 --- a/handlers.go +++ b/handlers.go @@ -43,7 +43,9 @@ type handler struct { matchFunc MatchFunc } -func (h handler) match(update *models.Update) bool { +// match reports whether the handler accepts update. botUsername is the bot's +// username, or empty if unknown. +func (h handler) match(update *models.Update, botUsername string) bool { if h.matchType == matchTypeFunc { return h.matchFunc(update) } @@ -61,9 +63,9 @@ func (h handler) match(update *models.Update) bool { case MatchTypeContains: return h.matchContains(data) case MatchTypeCommand: - return h.matchCommand(data, entities) + return h.matchCommand(data, entities, botUsername) case MatchTypeCommandStartOnly: - return h.matchCommandStartOnly(data, entities) + return h.matchCommandStartOnly(data, entities, botUsername) case matchTypeRegexp: return h.matchRegexp(data) default: @@ -139,11 +141,11 @@ func extractCommand(data string, entity models.MessageEntity) (string, bool) { return data[entity.Offset+1 : entity.Offset+entity.Length], true } -func (h handler) matchCommand(data string, entities []models.MessageEntity) bool { +func (h handler) matchCommand(data string, entities []models.MessageEntity, botUsername string) bool { for _, e := range entities { if e.Type == models.MessageEntityTypeBotCommand { command, ok := extractCommand(data, e) - if ok && command == h.pattern { + if ok && h.isCommand(command, botUsername) { return true } } @@ -151,11 +153,11 @@ func (h handler) matchCommand(data string, entities []models.MessageEntity) bool return false } -func (h handler) matchCommandStartOnly(data string, entities []models.MessageEntity) bool { +func (h handler) matchCommandStartOnly(data string, entities []models.MessageEntity, botUsername string) bool { for _, e := range entities { if e.Type == models.MessageEntityTypeBotCommand && e.Offset == 0 { command, ok := extractCommand(data, e) - if ok && command == h.pattern { + if ok && h.isCommand(command, botUsername) { return true } } @@ -163,6 +165,20 @@ func (h handler) matchCommandStartOnly(data string, entities []models.MessageEnt return false } +// isCommand reports whether command, without its leading "/", is the handler's +// command. In groups, clients address a command to a bot by appending its +// username, as in /start@bot_username. Telegram usernames are case-insensitive. +func (h handler) isCommand(command, botUsername string) bool { + if command == h.pattern { + return true + } + if h.pattern == "" || botUsername == "" { + return false + } + name, target, addressed := strings.Cut(command, "@") + return addressed && name == h.pattern && strings.EqualFold(target, botUsername) +} + func (b *Bot) RegisterHandlerMatchFunc(matchFunc MatchFunc, f HandlerFunc, m ...Middleware) string { b.handlersMx.Lock() defer b.handlersMx.Unlock() diff --git a/handlers_test.go b/handlers_test.go index 4224c5a4..f8856413 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -37,7 +37,7 @@ func Test_match_func(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{ID: 42}) + res := h.match(&models.Update{ID: 42}, "") if !called { t.Error("not called") } @@ -53,12 +53,12 @@ func Test_match_exact(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{Message: &models.Message{Text: "zzz"}}) + res := h.match(&models.Update{Message: &models.Message{Text: "zzz"}}, "") if res { t.Error("unexpected true result") } - res = h.match(&models.Update{Message: &models.Message{Text: "xxx"}}) + res = h.match(&models.Update{Message: &models.Message{Text: "xxx"}}, "") if !res { t.Error("unexpected false result") } @@ -71,12 +71,12 @@ func Test_match_caption_exact(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{Message: &models.Message{Caption: "zzz"}}) + res := h.match(&models.Update{Message: &models.Message{Caption: "zzz"}}, "") if res { t.Error("unexpected true result") } - res = h.match(&models.Update{Message: &models.Message{Caption: "xxx"}}) + res = h.match(&models.Update{Message: &models.Message{Caption: "xxx"}}, "") if !res { t.Error("unexpected false result") } @@ -89,12 +89,12 @@ func Test_match_prefix(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xabcdef"}}) + res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xabcdef"}}, "") if res { t.Error("unexpected true result") } - res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "abcdef"}}) + res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "abcdef"}}, "") if !res { t.Error("unexpected false result") } @@ -107,12 +107,12 @@ func Test_match_contains(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabxx"}}) + res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabxx"}}, "") if res { t.Error("unexpected true result") } - res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabcdef"}}) + res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "xxabcdef"}}, "") if !res { t.Error("unexpected false result") } @@ -127,12 +127,12 @@ func Test_match_regexp(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}}) + res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}}, "") if res { t.Error("unexpected true result") } - res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "abcdef"}}) + res = h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "abcdef"}}, "") if !res { t.Error("unexpected false result") } @@ -145,7 +145,7 @@ func Test_match_invalid_type(t *testing.T) { h := findHandler(b, id) - res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}}) + res := h.match(&models.Update{CallbackQuery: &models.CallbackQuery{Data: "123abc"}}, "") if res { t.Error("unexpected true result") } @@ -193,7 +193,7 @@ func Test_match_exact_game(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if !res { t.Error("unexpected true result") } @@ -216,7 +216,7 @@ func Test_match_command_start(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if !res { t.Error("unexpected result") } @@ -238,7 +238,7 @@ func Test_match_command_start(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if !res { t.Error("unexpected result") } @@ -260,7 +260,7 @@ func Test_match_command_start(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if res { t.Error("unexpected result") } @@ -282,7 +282,7 @@ func Test_match_command_start(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if !res { t.Error("unexpected result") } @@ -304,7 +304,7 @@ func Test_match_command_start(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if res { t.Error("unexpected result") } @@ -326,7 +326,7 @@ func Test_match_command_start(t *testing.T) { }, } - res := h.match(&u) + res := h.match(&u, "") if res { t.Error("unexpected result") } @@ -343,7 +343,7 @@ func Test_match_NilUpdateMessageIsFalse(t *testing.T) { Message: nil, } - res := h.match(&u) + res := h.match(&u, "") if res { t.Error("want 'false', but got 'true'") } @@ -859,6 +859,46 @@ func TestExtractCommand(t *testing.T) { } } +func Test_match_commandAddressedToBot(t *testing.T) { + tests := []struct { + name string + pattern string + text string + botUsername string + want bool + }{ + {name: "addressed to the bot", pattern: "foo", text: "/foo@my_bot", botUsername: "my_bot", want: true}, + {name: "username in another case", pattern: "foo", text: "/foo@My_Bot", botUsername: "my_bot", want: true}, + {name: "addressed to another bot", pattern: "foo", text: "/foo@other_bot", botUsername: "my_bot", want: false}, + {name: "addressed to a bot whose username extends ours", pattern: "foo", text: "/foo@my_bot_2", botUsername: "my_bot", want: false}, + {name: "another command", pattern: "foo", text: "/bar@my_bot", botUsername: "my_bot", want: false}, + {name: "empty username suffix", pattern: "foo", text: "/foo@", botUsername: "my_bot", want: false}, + {name: "username unknown", pattern: "foo", text: "/foo@my_bot", botUsername: "", want: false}, + {name: "empty username suffix with username unknown", pattern: "foo", text: "/foo@", botUsername: "", want: false}, + {name: "empty pattern", pattern: "", text: "/@my_bot", botUsername: "my_bot", want: false}, + {name: "pattern with the username", pattern: "foo@my_bot", text: "/foo@my_bot", botUsername: "", want: true}, + } + for _, handlerType := range []HandlerType{HandlerTypeMessageText, HandlerTypePhotoCaption} { + for _, matchType := range []MatchType{MatchTypeCommand, MatchTypeCommandStartOnly} { + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + entities := []models.MessageEntity{{Type: models.MessageEntityTypeBotCommand, Offset: 0, Length: len(tt.text)}} + u := &models.Update{Message: &models.Message{ + Text: tt.text, + Entities: entities, + Caption: tt.text, + CaptionEntities: entities, + }} + h := handler{handlerType: handlerType, matchType: matchType, pattern: tt.pattern} + if got := h.match(u, tt.botUsername); got != tt.want { + t.Errorf("match = %v, want %v: handlerType=%d matchType=%d", got, tt.want, handlerType, matchType) + } + }) + } + } + } +} + func TestHandler_matchCommand(t *testing.T) { tests := []struct { name string @@ -928,7 +968,7 @@ func TestHandler_matchCommand(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := handler{pattern: tt.pattern} - result := h.matchCommand(tt.data, tt.entities) + result := h.matchCommand(tt.data, tt.entities, "") if result != tt.wantMatch { t.Errorf("matchCommand() = %v, want %v", result, tt.wantMatch) } @@ -995,7 +1035,7 @@ func TestHandler_matchCommandStartOnly(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { h := handler{pattern: tt.pattern} - result := h.matchCommandStartOnly(tt.data, tt.entities) + result := h.matchCommandStartOnly(tt.data, tt.entities, "") if result != tt.wantMatch { t.Errorf("matchCommandStartOnly() = %v, want %v", result, tt.wantMatch) } @@ -1038,7 +1078,7 @@ func Test_match_invalidCommandEntities(t *testing.T) { CaptionEntities: entities, }} h := handler{handlerType: handlerType, matchType: matchType, pattern: pattern} - if h.match(u) { + if h.match(u, "") { t.Errorf("invalid command matched: handlerType=%d matchType=%d pattern=%q", handlerType, matchType, pattern) } }) @@ -1063,7 +1103,7 @@ func Test_match_commandSkipsInvalidEntities(t *testing.T) { for _, handlerType := range []HandlerType{HandlerTypeMessageText, HandlerTypePhotoCaption} { for _, matchType := range []MatchType{MatchTypeCommand, MatchTypeCommandStartOnly} { h := handler{handlerType: handlerType, matchType: matchType, pattern: "start"} - if !h.match(u) { + if !h.match(u, "") { t.Errorf("valid command after invalid entity did not match: handlerType=%d matchType=%d", handlerType, matchType) } } @@ -1074,7 +1114,7 @@ func Test_match_missingUpdateData(t *testing.T) { for _, handlerType := range []HandlerType{HandlerTypeMessageText, HandlerTypePhotoCaption, HandlerTypeCallbackQueryData, HandlerTypeCallbackQueryGameShortName, HandlerTypeInlineQuery, HandlerTypeWidgetQueryData, HandlerTypeSharedWidgetQueryData} { for _, matchType := range []MatchType{MatchTypeExact, MatchTypePrefix, MatchTypeContains, matchTypeRegexp} { h := handler{handlerType: handlerType, matchType: matchType, re: regexp.MustCompile("")} - if h.match(&models.Update{}) { + if h.match(&models.Update{}, "") { t.Errorf("missing data matched an empty pattern: handlerType=%d matchType=%d", handlerType, matchType) } } @@ -1084,7 +1124,7 @@ func Test_match_missingUpdateData(t *testing.T) { func Test_match_unknownHandlerTypePreservesEmptyData(t *testing.T) { for _, matchType := range []MatchType{MatchTypeExact, MatchTypePrefix, MatchTypeContains, matchTypeRegexp} { h := handler{handlerType: HandlerType(999), matchType: matchType, re: regexp.MustCompile("")} - if !h.match(&models.Update{}) { + if !h.match(&models.Update{}, "") { t.Errorf("unknown handler type no longer matches an empty pattern: matchType=%d", matchType) } } @@ -1120,7 +1160,7 @@ func Test_match_inlineQuery(t *testing.T) { id = b.RegisterHandler(HandlerTypeInlineQuery, tt.pattern, tt.matchType, nil) } h := findHandler(b, id) - if got := h.match(&models.Update{InlineQuery: &models.InlineQuery{Query: tt.query}}); got != tt.want { + if got := h.match(&models.Update{InlineQuery: &models.InlineQuery{Query: tt.query}}, ""); got != tt.want { t.Errorf("match(%q) = %v, want %v", tt.query, got, tt.want) } }) @@ -1154,7 +1194,7 @@ func Test_match_widgetQuery(t *testing.T) { } else { upd.SharedWidgetQuery = &models.SharedWidgetQuery{Data: tt.data} } - if got := h.match(upd); got != tt.want { + if got := h.match(upd, ""); got != tt.want { t.Errorf("match(%q) = %v, want %v", tt.data, got, tt.want) } }) diff --git a/methods.go b/methods.go index 49fb3635..b8a9fc5c 100644 --- a/methods.go +++ b/methods.go @@ -30,9 +30,15 @@ func (b *Bot) DeleteWebhook(ctx context.Context, params *DeleteWebhookParams) (b } // GetMe https://core.telegram.org/bots/api#getme +// GetMe also records the bot's username when the returned user is the bot the +// token belongs to, so a MatchTypeCommand or MatchTypeCommandStartOnly handler +// for start also matches /start@bot_username. func (b *Bot) GetMe(ctx context.Context) (*models.User, error) { result := &models.User{} err := b.rawRequest(ctx, "getMe", nil, result) + if err == nil { + b.setUsername(result) + } return result, err } diff --git a/options.go b/options.go index 587f40db..098cff92 100644 --- a/options.go +++ b/options.go @@ -88,7 +88,9 @@ func WithServerURL(serverURL string) Option { } } -// WithSkipGetMe allows skip call GetMe on bot init +// WithSkipGetMe allows skip call GetMe on bot init. A MatchTypeCommand or +// MatchTypeCommandStartOnly handler for a bare command such as start then +// matches /start@bot_username only after a GetMe call succeeds. func WithSkipGetMe() Option { return func(b *Bot) { b.skipGetMe = true diff --git a/process_update.go b/process_update.go index 88d56beb..da26e158 100644 --- a/process_update.go +++ b/process_update.go @@ -35,8 +35,9 @@ func (b *Bot) findHandler(upd *models.Update) HandlerFunc { b.handlersMx.RLock() defer b.handlersMx.RUnlock() + username := b.botUsername() for _, h := range b.handlers { - if h.match(upd) { + if h.match(upd, username) { return h.handler } }