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.
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.
Step 2: Handle the commands in code
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
16:30
1:42:18
28:14
41:09
9:47
8:23