node-telegram-bot-api icon indicating copy to clipboard operation
node-telegram-bot-api copied to clipboard

bot.command() matches commands addressed to other bots (/start@other_bot)

Open kgeminicdev opened this issue 1 month ago • 2 comments

Bug Report

I have read:

I am using the latest version of the library.

Expected Behavior

bot.command("start", …) handles /start and /start@my_bot, and ignores /start@other_bot. That's the addressing rule in Telegram's commands docs.

Actual Behavior

The command regex in src/core/bot.ts is ^\/(start)(@\w+)?(?:\s+(.*))?$, and the @\w+ group is never compared to the bot's own username. So /start@other_bot runs the handler too.

Bots in privacy mode don't see this, because Telegram doesn't deliver other bots' commands to them. A bot with privacy mode off, or one that is a group admin, receives every message. In a group with several bots, it answers commands meant for the others.

Steps to reproduce the Behavior

const bot = new Bot(token);
bot.command("start", () => console.log("handled"));
await bot.handleUpdate({
  update_id: 1,
  message: {
    message_id: 1, date: 0,
    chat: { id: -100, type: "supergroup" },
    text: "/start@other_bot",
  },
});
// logs "handled"

Fixing it needs the bot's own username. That could come from a getMe() in init(), or an explicit username option, and then the regex would reject a non-matching @ part. Happy to send a PR once you pick a direction.

kgeminicdev avatar Nov 02 '26 17:11 kgeminicdev

Confirmed, thanks for the clear report. Reproduced on master: /start@other_bot hi runs the /start handler with ctx.match = "hi".

Direction:

  • bot.init() calls getMe() and caches the bot's username. init() is already memoized and awaited by both startPolling() and handleUpdate(), so polling and webhooks are both covered.
  • The cached value expires after a fixed time (e.g. 1h). The next update after expiry refreshes it in the background. A failed refresh keeps the old value, so a Telegram hiccup never drops commands.
  • command() compares the @name part case-insensitively with the cached username. No @ part: match as today. A different name: next().
  • An explicit username in BotOptions skips getMe() entirely (useful for cold-start-sensitive webhooks and for tests).
  • Expose the result as bot.botInfo (the getMe() User), so handlers do not need their own getMe() call.

Tests should use the injected fake fetch for getMe, and cover /start, /start@my_bot, /START@MY_BOT, /start@other_bot, and the username option path.

A PR along these lines is welcome.

yagop avatar Nov 02 '26 19:11 yagop

Fixed in #1383 (merged to master). bot.command("start") now ignores commands addressed to another bot: /start@other_bot falls through to downstream middleware, /start@your_bot runs the handler (the @name compare is case-insensitive, as Telegram usernames are), and a bare /start still always matches. The bot learns its own @username from a single getMe() in bot.init(), kept for the process lifetime (a bot's username is permanent, so no refresh), and its identity is exposed as bot.botInfo and to handlers as ctx.me. Thanks for the report and the clear write-up of the cases!

yagop avatar Nov 03 '26 11:11 yagop