diff --git a/addressing_test.go b/addressing_test.go index 8cea261..0390a2b 100644 --- a/addressing_test.go +++ b/addressing_test.go @@ -46,10 +46,9 @@ func TestCommand_AddressedToOtherBot(t *testing.T) { t.Fatalf("messages = %q, want %q", messages, want) } - // getMe is fetched once, on the first @-addressed command. - if calls := rec.Calls(); len(calls) != 1 || calls[0].Method != "getMe" { - t.Fatalf("calls = %+v", calls) - } + // getMe is fetched once, on the first @-addressed command, and getChat + // once, on the first command addressed to another username. + rec.AssertSequence(t, "getMe", "getChat") ctx := tgtest.NewContext(t, client, tgtest.Message(&group, "/start@other_bot")) if ctx.Command() != "" || gogram.FilterCommand("start")(ctx) { @@ -57,6 +56,67 @@ func TestCommand_AddressedToOtherBot(t *testing.T) { } } +func TestCommand_ActiveUsernames(t *testing.T) { + t.Parallel() + + rec := tgtest.NewRecorder() + rec.WillReturn("getMe", &gogram.User{ID: 1, IsBot: true, Username: "my_bot"}) + rec.WillReturn("getChat", &gogram.ChatFullInfo{ + ID: 1, Type: gogram.ChatPrivate, ActiveUsernames: []string{"my_bot", "Collectible"}, + }) + + client, err := gogram.NewClient(testToken, gogram.WithCaller(rec)) + if err != nil { + t.Fatal(err) + } + + group := gogram.Chat{ID: -5, Type: gogram.ChatSupergroup} + + for text, want := range map[string]string{ + "/start@my_bot": "/start", + "/start@collectible": "/start", + "/start@Collectible": "/start", + "/start@other_bot": "", + "/start@collectible2": "", + } { + if got := tgtest.NewContext(t, client, tgtest.Message(&group, text)).Command(); got != want { + t.Errorf("Command(%q) = %q, want %q", text, got, want) + } + } + + rec.AssertSequence(t, "getMe", "getChat") + rec.AssertCalled(t, "getChat", tgtest.Match(func(p *gogram.GetChatParams) bool { + return p.ChatID.ID == 1 + })) +} + +func TestCommand_GetChatFails(t *testing.T) { + t.Parallel() + + rec := tgtest.NewRecorder() + rec.WillReturn("getMe", &gogram.User{ID: 1, IsBot: true, Username: "my_bot"}) + rec.WillFail("getChat", errDenied) + + client, err := gogram.NewClient(testToken, gogram.WithCaller(rec)) + if err != nil { + t.Fatal(err) + } + + // Without the extra usernames only the main one addresses the bot, + // and the failed fetch is retried on the next foreign username. + for range 2 { + if ctx := tgtest.NewContext(t, client, tgtest.Text(1, "/start@other_bot")); ctx.Command() != "" { + t.Fatalf("Command() = %q, want none", ctx.Command()) + } + } + + if ctx := tgtest.NewContext(t, client, tgtest.Text(1, "/start@my_bot")); ctx.Command() != "/start" { + t.Fatalf("Command() = %q, want /start", ctx.Command()) + } + + rec.AssertSequence(t, "getMe", "getChat", "getChat") +} + func TestCommand_GetMeFails(t *testing.T) { t.Parallel() diff --git a/client_me.go b/client_me.go index 3fa879b..14c741a 100644 --- a/client_me.go +++ b/client_me.go @@ -8,8 +8,9 @@ import ( // botInfo caches GetMe: the bot's identity never changes while it runs. type botInfo struct { - me atomic.Pointer[User] - once sync.Mutex // serializes the first fetch; later calls are lock-free + me atomic.Pointer[User] + usernames atomic.Pointer[[]string] // active usernames from getChat, see usernames + once sync.Mutex // serializes the first fetch; later calls are lock-free } // Me returns the bot's own [User] from getMe, fetched once and cached. @@ -35,3 +36,34 @@ func (c *Client) Me(ctx context.Context) (*User, error) { return me, nil } + +// usernames returns the bot's active usernames from getChat on its own id: +// the one from getMe plus any extra (collectible) ones, all of which +// address the bot in "/cmd@username". Fetched once and cached; a failed +// fetch is not cached. +func (c *Client) usernames(ctx context.Context, me *User) ([]string, error) { + if names := c.info.usernames.Load(); names != nil { + return *names, nil + } + + c.info.once.Lock() + defer c.info.once.Unlock() + + if names := c.info.usernames.Load(); names != nil { + return *names, nil + } + + chat, err := c.GetChat(ctx, &GetChatParams{ChatID: ChatID{ID: me.ID}}) + if err != nil { + return nil, err + } + + var names []string + if chat != nil { + names = chat.ActiveUsernames + } + + c.info.usernames.Store(&names) + + return names, nil +} diff --git a/router.go b/router.go index cd8d13c..0690df9 100644 --- a/router.go +++ b/router.go @@ -132,9 +132,12 @@ func currentCommand(ctx *Context) string { return command } -// addressedToMe reports whether bot is this bot's username. The username -// comes from [Client.Me], fetched once on the first @-addressed command; -// when it cannot be fetched the command is accepted, as before the check. +// addressedToMe reports whether bot is one of this bot's usernames. The +// main username comes from [Client.Me], fetched once on the first +// @-addressed command; when it cannot be fetched the command is accepted, +// as before the check. Only a username that differs from it costs one +// getChat, fetched once, for the bot's extra (collectible) usernames; when +// that fails the command is treated as addressed to another bot. func addressedToMe(ctx *Context, bot string) bool { if ctx.client == nil { return true @@ -148,7 +151,18 @@ func addressedToMe(ctx *Context, bot string) bool { } } - return me.Username == "" || strings.EqualFold(bot, me.Username) + if me.Username == "" || strings.EqualFold(bot, me.Username) { + return true + } + + names, err := ctx.client.usernames(ctx.context, me) + if err != nil { + return false + } + + return slices.ContainsFunc(names, func(name string) bool { + return strings.EqualFold(bot, name) + }) } // Process processes an update.