Telegram bots2 minutes to read

Deploy a grammY bot and diagnose polling handler errors

Deploy a grammY project to a Node.js polling worker, add a handler error hook, and test graceful shutdown.

Start the production entry file

Choose Node.js polling bot on Telegram bot hosting. Include grammy in production dependencies and upload the npm lockfile. Set MAIN_FILE to the file that calls bot.start(), and save TELEGRAM_BOT_TOKEN in Variables.

Stop the old poller using that token before starting the hosted worker. Check one private command so you can tell whether the handler is loaded and processing updates.

Log handler failures without logging the update

Install bot.catch() before calling bot.start(). The hook receives the framework error and context for a failed middleware invocation. Use the update ID and error type as correlation values, and leave chat text and credentials out of logs.

Observe a grammY middleware error
bot.catch(({ ctx, error }) => {
  const kind = error instanceof Error ? error.name : 'UnknownError';
  console.error('Handler failed for update', ctx.update.update_id, kind);
});

process.once('SIGINT', () => bot.stop());
process.once('SIGTERM', () => bot.stop());
await bot.start();

Exercise a known handler that you expect to succeed, then inspect Console during a reproducible failing request. Startup failures and handler failures are separate checks. A bad token or another poller can prevent the bot from reaching its handler at all. grammY error handling.

Retain sessions you need after deployment

If the bot uses grammY’s default session storage, exercise a session-based command after a restart to see what resets. Choose a persistent storage adapter for sessions that must survive deployments. grammY session storage and the grammY and Telegraf quickstart.