grammY bot deploy: polling, webhooks and hosting
Deploy a grammY bot as one always-on Node.js process: bot.start() for polling, or webhookCallback() behind an HTTP server, with bot.stop() on SIGTERM. On Livops a repository with grammy in package.json runs as a bot from every push, for 19 credits a month (19,000 so'm).
By Livops team, the people who build and run LivopsUpdated
grammY is a framework for Telegram bots that runs on Node.js, Deno and in the browser[10]; 1.46.0 is the current release on npm[8]. Deploying it on a server is short work once three things are right: how the bot receives updates, how it stops, and where it keeps sessions. This page covers each from grammY’s own guides, the webhook wiring that works behind a platform, and the exact files Livops reads to run one.
What does a grammY bot need in production?
Polling or a webhook, decided once
Telegram delivers updates either through getUpdates or to a webhook, and the two are mutually exclusive[9]. grammY’s guide puts it plainly: long polling is simpler, webhooks are cheaper, and without a strong reason long polling is the one to pick[1]. With webhooks you must not call bot.start()[1].
One update at a time, or the runner
bot.start() is simple long polling that handles updates one after another[1]. For concurrent handling, grammY runner does the polling instead; with sessions, its sequentialize middleware keeps each chat’s messages in order[3].
A graceful stop
Stop the bot on the signals a host sends[2]:
process.once("SIGINT", () => bot.stop());
process.once("SIGTERM", () => bot.stop());
A proper stop syncs the update offset with Telegram, so nothing is lost; a hard kill can make the bot handle up to 100 updates it has already seen[2]. With the runner, stop its handle instead[2]. Processing is at least once, so for anything that must not happen twice — a payment, an order — de-duplicate on update_id[2].
Sessions in a real store
By default sessions live in RAM and are lost as soon as the bot stops, which grammY says is most likely not what you want in production[4]. The grammY storages repository has adapters for files, Redis, PostgreSQL, MongoDB and more[5].
A production grammY bot, polling
package.json:
{
"name": "my-bot",
"private": true,
"scripts": { "start": "node bot.js" },
"dependencies": { "grammy": "^1.46.0" }
}
bot.js:
const { Bot } = require("grammy");
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) => ctx.reply("Hello!"));
bot.on("message:text", (ctx) => ctx.reply(ctx.message.text));
process.once("SIGINT", () => bot.stop());
process.once("SIGTERM", () => bot.stop());
bot.start();
The same bot on a webhook
webhookCallback(bot, adapter, options) turns the bot into a request handler for a web framework: express, fastify, hono, koa, Node’s own http and many more[7]. It calls bot.init() on the first request by itself[6]. Its options matter on a server:
secretToken: a request without the matchingX-Telegram-Bot-Api-Secret-Tokenheader never reaches the bot[6]; thehttpadapter answers it 401[7].timeoutMilliseconds, 10 000 by default, andonTimeout,"throw"by default[6]. grammY keeps this shorter than Telegram’s own timeout so a slow handler does not make Telegram send the update again; long work belongs in a queue[1].
The http adapter reads any request’s body, whatever its method or path[7], so route to it yourself:
const http = require("node:http");
const { Bot, webhookCallback } = require("grammy");
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) => ctx.reply("Hello!"));
const handleUpdate = webhookCallback(bot, "http", {
secretToken: process.env.WEBHOOK_SECRET,
});
const server = http.createServer((req, res) => {
if (req.method === "POST" && req.url === process.env.WEBHOOK_PATH) {
handleUpdate(req, res).catch((err) => {
console.error(err);
if (!res.headersSent) res.writeHead(500).end();
});
return;
}
res.writeHead(404).end();
});
server.listen(Number(process.env.PORT));
process.once("SIGTERM", () => server.close());
There is no bot.api.setWebhook() here on purpose: where the platform sets the webhook, your code should not move it.
Deploy a grammY bot on Livops
- Push the repository, or upload the folder as a
.zip, and create a project from it in the cabinet. - Under Settings → Variables, add
BOT_TOKENand mark it as a secret: its value is never shown again. - Deploy. The log shows
npm installand the start.
grammy or @grammyjs/runner in package.json makes the project a Telegram bot. Livops runs npm install, your build script if there is one — for a TypeScript bot, point start at the compiled file, such as node dist/bot.js — and starts npm run start. With no start script it runs index.js, bot.js, app.js, main.js or server.js from the root or src/. A .nvmrc or .node-version picks the Node version. Or set it in livops.json:
{
"kind": "bot",
"start": "node bot.js"
}
Polling is the default and the polling bot above runs as it is: no port, no domain. A Node bot’s source is not read to guess its mode, so for a webhook open Settings → Connection to Telegram, choose Webhook and press Save and redeploy. The platform generates WEBHOOK_SECRET, sets PORT and WEBHOOK_PATH (/webhook unless you change it), tells Telegram the address with the secret once the bot answers, and refuses requests to the path that lack it — the webhook example above is what it expects. In webhook mode the token must be in BOT_TOKEN.
What happens after that:
- On a push, a polling bot is stopped first — SIGTERM, then ten seconds before it is killed, the window
bot.stop()has to sync the offset in — and the new version starts after it, so two copies never poll at once. A webhook bot’s new copy must answer onPORTwithin a minute and takes the updates before the old one stops. - If the bot exits in its first 30 seconds, the deploy is marked failed with its last lines in the log.
- If it crashes later, it is started again; ten crashes inside 15 minutes and it is stopped, and you are notified with its last lines — in Telegram, if your account is linked.
- Files go in
/app/data(process.env.DATA_DIR), which survives restarts and deploys; the code folder is read-only and/tmpis wiped. For a session store, add Redis or PostgreSQL under Databases and readREDIS_URLorDATABASE_URL. - The clock is UTC. Add
TZ=Asia/Tashkentunder Variables for schedules in Tashkent time.
A bot costs 19 credits a month (19,000 so’m), polling or webhook. One credit is 1,000 so’m; see Pricing.
Livops, a VPS or serverless?
grammY has webhook adapters for serverless platforms such as AWS Lambda and Cloudflare[7], and a bot with little traffic can live there as a webhook — at the price of the timeout rules above on every update. A VPS gives you root and room for many bots, and the upkeep of the machine with it. Livops sits between: an always-on process you deploy with git push, polling or webhook, without root; a bot that needs system software of its own is better on a VPS or in its own Dockerfile.
Questions
Polling or webhook for a grammY bot?
Polling, unless you have a reason. grammY's own guide calls long polling the simpler choice and webhooks the cheaper one. On a platform that keeps the process running anyway, polling needs nothing set up.
Does grammY runner work on Livops?
Yes. @grammyjs/runner is one of the libraries that mark a project as a Telegram bot. Stop it with the handle's stop() on SIGTERM, the way grammY's reliability guide shows.
Why does my webhook bot answer 401?
webhookCallback was given a secretToken and the request did not carry the same one in X-Telegram-Bot-Api-Secret-Token. On Livops, pass process.env.WEBHOOK_SECRET; the platform sets that same secret on the webhook.
Do I call bot.api.setWebhook on Livops?
No. In webhook mode the platform tells Telegram the address and the secret once the bot answers. Your code listens on PORT, takes POSTs on WEBHOOK_PATH and checks WEBHOOK_SECRET.
Where do grammY sessions go?
Not in the default RAM storage, which is lost when the bot stops. Use a storage adapter: grammY has them for Redis, PostgreSQL, files and more. On Livops, a file belongs in /app/data, and a Redis database gives you REDIS_URL.
How much does it cost?
19 credits a month, 19,000 so'm, the same for polling and webhook. There is no free tier for bots; the first bot on an account that has not paid runs for 15 minutes on a zero balance.
Sources
- grammY — Long polling vs. webhooks (grammy.dev). Checked .
- grammY — Scaling up IV, reliability (grammy.dev). Checked .
- grammY — Concurrency with grammY runner (grammy.dev). Checked .
- grammY — Sessions and storing data (grammy.dev). Checked .
- grammY storages — packages (github.com). Checked .
- grammY source — src/convenience/webhook.ts (raw.githubusercontent.com). Checked .
- grammY source — src/convenience/frameworks.ts (raw.githubusercontent.com). Checked .
- npm registry — grammy, latest (registry.npmjs.org). Checked .
- Telegram Bot API — Getting updates (core.telegram.org). Checked .
- grammY — Introduction (grammy.dev). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your grammY botRelated
- Telegram bot webhook vs polling: which one to usePolling means your bot asks Telegram for updates in a loop; a webhook means Telegram POSTs each update to your HTTPS address. Polling is simpler and needs no address; a webhook needs HTTPS but no always-running loop. A bot uses one or the other, never both. On Livops both run for the same 19 credits a month.
- Telegraf bot hosting: deploying a Telegraf 4 bot on Node.jsA Telegraf bot is one Node.js process: bot.launch() for polling, or bot.webhookCallback() behind an HTTP server for a webhook, with bot.stop() on SIGTERM. On Livops a repository with telegraf in package.json is run as a bot from every push, for 19 credits a month (19,000 so'm).
- aiogram bot hosting: how to deploy an aiogram 3 botAn aiogram 3 bot needs one process that never stops, the token in an environment variable, state kept outside memory and a clean exit on SIGTERM. On Livops you push a repository with bot.py and requirements.txt, and it runs for 19 credits a month (19,000 so'm), polling or webhook.
- python-telegram-bot hosting: deploying a v22 botA python-telegram-bot bot runs as one Application: run_polling() for polling, run_webhook() with the webhooks extra for a webhook. Both stop cleanly on SIGTERM. On Livops it runs from a push with bot.py and requirements.txt for 19 credits a month (19,000 so'm).