Commands
Commands are special entities in Telegram messages, that serve as instructions for bots.
Usage
Revisit the commands section in the Introduction for Developers written by the Telegram team.
grammY provides special handling for commands (e.g.
and
). You can directly register listeners for certain commands via bot
.
// Respond to the command /start.
bot.command("start" /* , ... */);
// Respond to the command /help.
bot.command("help" /* , ... */);
// Respond to the commands /a, /b, /c, and /d.
bot.command(["a", "b", "c", "d"] /* , ... */);
Note that only those commands that are in the beginning of a message are handled, so if a user sends 'Please do not send
, then your listener will not be called, even though the
command is contained in the message.
Telegram supports sending targeted commands to bots, i.e. commands that end with @your
. grammY handles this automatically for you, so bot
will match messages with
and with
as commands. You can choose to match only targeted commands by specifying bot
.
Suggest Commands to Users
You can call
await bot.api.setMyCommands([
{ command: "start", description: "Start the bot" },
{ command: "help", description: "Show help text" },
{ command: "settings", description: "Open settings" },
]);
to make Telegram clients display a list of suggested commands in the text input field.
Alternatively, you can configure this by talking to @BotFather.
Arguments
Users can send arguments along with their commands. You can access the argument string via ctx
.
bot.command("add", (ctx) => {
// `item` will be 'apple pie' if a user sends '/add apple pie'.
const item = ctx.match;
});
Note that you can always access the entire message’s text via ctx
.
Deep Linking Support
Revisit the deep linking section in the Introduction for Developers written by the Telegram team.
When a user visits https:
, their Telegram client will show a START button that (when clicked) sends the string from the URL parameter along with the message, in this example, the message text will be '
. Telegram clients will not show the payload to the user (they will only see '
in the UI), however, your bot will receive it. grammY extracts this payload for you, and provides it under ctx
. In our example, ctx
would contain the string 'payload'
.
Deep linking is useful if you want to build a referral system, or track where users discovered your bot. For example, your bot could send a channel post with an inline query button. The button contains a URL like the one above, e.g. https:
. When a user clicks on the button underneath the post, their Telegram client will open a chat with your bot, and display the START button as described above. This way, your bot can identify where a user came from, and that they clicked the button underneath a specific channel post.
Naturally, you can also embed such links anywhere else: on the web, in messages, in QR codes, etc.