Commands & Handlers
Register listeners for any Telegram update type using the bot global.
Commands
bot.command('start', (ctx) => {
ctx.reply('Welcome! 👋');
});
Text matching
bot.hears('hello', (ctx) => ctx.reply('Hi!'));
bot.hears(/order #(\d+)/i, (ctx) => {
const id = ctx.match[1];
ctx.reply('Looking up order ' + id);
});
Callback actions
bot.action('confirm_pay', async (ctx) => {
await ctx.answerCbQuery();
await ctx.editMessageText('Payment confirmed ✓');
});
Fallback handlers
If you want a last-resort text response, add a general text handler after your command and pattern-specific handlers.
bot.on('text', async (ctx) => {
await ctx.reply("Sorry, I didn't understand that. Type /help for available commands.");
});
Place broad text fallbacks near the end of your handler setup. If they come too early, more specific text handlers may never run.
Was this page helpful?
Last updated March 22, 2026