Telegram bots2 minutes to read

How do I host a grammY or Telegraf Telegram bot in Node.js?

Deploy a grammY or Telegraf bot on a Bothosting Node.js polling worker, save the token, and verify that a private /ping gets exactly one reply.

Run one polling worker per token

Choose Node.js polling bot under Telegram bot hosting for a grammY bot that calls bot.start() or a Telegraf bot that calls bot.launch() without webhook options. The worker asks Telegram for new messages over an outbound connection. You can close your laptop after the hosted bot is running.

Save the token from BotFather in Variables as TELEGRAM_BOT_TOKEN. Stop the copy on your computer before starting the hosted one. If you previously registered an HTTPS webhook, follow the webhook-to-polling migration guide to keep pending updates.

A /ping handler for grammY

For a new project, run npm init -y and npm install grammy locally. Save this as index.mjs. The command name in the handler is ping. In Telegram you type it with the slash.

index.mjs for grammY
import { Bot } from 'grammy';
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN);
bot.command('ping', ctx => ctx.reply('pong from grammY'));
bot.start();

How grammY receives updates. Deploy grammY and diagnose handler errors.

The same command in Telegraf

If your project uses Telegraf, install telegraf as a production dependency with npm install telegraf. Use this version of index.mjs for the test.

index.mjs for Telegraf
import { Telegraf } from 'telegraf';
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
bot.command('ping', ctx => ctx.reply('pong from Telegraf'));
bot.launch();

Telegraf command and launch examples. Deploy compiled TypeScript with Telegraf.

Check which process replies

Upload index.mjs, package.json and package-lock.json to the deployment root. Set MAIN_FILE to index.mjs and start the worker. Open a private chat with the bot username from BotFather and send /ping. The reply should name the library you deployed. Restart the worker and repeat with your laptop copy still stopped.

A Telegram 409 Conflict mentioning another getUpdates request points to a second poller using that token. Look for a local development process or the previous host. For a project that already handles incoming HTTP requests, use the Node.js webhook profile and HTTPS setup.