Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions addressing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,77 @@ 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) {
t.Fatalf("Command() = %q for another bot", ctx.Command())
}
}

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()

Expand Down
36 changes: 34 additions & 2 deletions client_me.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
22 changes: 18 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Loading