IntegrationsBeginner

How to add slash commands and a menu to a Telegram bot

Register command hints with BotFather and handle /start, /help, and custom commands in code.

7 minBeginner

Slash commands give your bot a tidy menu. When a user types a slash, Telegram shows the command list you registered, and your code routes each command to the right handler. This guide sets up both the visible menu and the logic behind it.

What you need

  • A bot token from BotFather
  • A running bot (polling or webhook)
  • A short list of commands you want to offer

Step 1: Register the command hints

In BotFather, send /setcommands, choose your bot, then paste a list of command and description pairs. These power the menu users see when they tap the slash icon.

Chat with @BotFather
You
/setcommands
Agent
Choose a bot to change the list of commands.
You
@order_helper_bot
Agent
OK. Send me a list of commands, one per line, format: command - description
You
start - Start the bot\nhelp - Show what I can do\nstatus - Check your last order
Agent
Success! Command list updated.
Hints are cosmetic
Registering commands in BotFather only changes the menu. Your code still has to detect and respond to each command. The two steps are independent.

Step 2: Handle the commands in code

commands.mjs
import TelegramBot from "node-telegram-bot-api";

const bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, {
  polling: true,
});

bot.onText(/^\/start/, (msg) => {
  bot.sendMessage(msg.chat.id, "Welcome. Type /help to see what I do.");
});

bot.onText(/^\/help/, (msg) => {
  bot.sendMessage(
    msg.chat.id,
    "Commands:\n/status - check your last order\n/help - this message"
  );
});

bot.onText(/^\/status/, (msg) => {
  bot.sendMessage(msg.chat.id, "Your last order shipped on Tuesday.");
});

Step 3: Set a custom menu button (optional)

You can replace the default menu button with a label using setChatMenuButton, which is handy if you also run a Mini App and want a launch button.

curl "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/setChatMenuButton" \
  -H "Content-Type: application/json" \
  -d '{"menu_button":{"type":"commands"}}'

Result

Users now see a clean command menu, and each command triggers the matching reply. Adding a new feature is as simple as registering one more hint and one more handler.

Watch related tutorials

Tags
#telegram#commands#botfather#bot#ux