Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Telegram bot webhook vs polling: which one to use

Polling 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.

By Livops team, the people who build and run LivopsUpdated

Every Telegram bot has to choose how updates reach it: it can ask Telegram for them (polling), or give Telegram an address to send them to (a webhook). The choice decides what you host — a process that never stops, or an HTTPS endpoint — and a few failure modes that are easy to walk into. This page explains both from the Bot API’s own documentation, says when each is the better fit, shows how aiogram, python-telegram-bot, Telegraf and grammY switch between them, and ends with how both run on Livops.

How does polling work?

The bot calls getUpdates in a loop. With a timeout above zero it is long polling, where the request is held open until there is something to return; the default of 0 is ordinary short polling[1]. Libraries set a timeout for you — aiogram’s start_polling uses 10 seconds[6]. An update counts as confirmed once the bot asks with an offset higher than its update_id, and the offset should be recalculated after every answer to avoid duplicates[1].

What it asks of the host: outbound internet and a process that never stops. Anything that puts idle processes to sleep stops the bot.

How does a webhook work?

You call setWebhook with an HTTPS URL, and for every update Telegram sends an HTTPS POST to it with the update as JSON[1]. The address must be reachable on port 443, 80, 88 or 8443[1], over TLS 1.2 or newer, on IPv4 — IPv6 is not supported for webhooks[2]. Requests come from 149.154.160.0/20 and 91.108.4.0/22, though Telegram warns the range may change[2].

Pass a secret_token (1–256 characters, A-Z a-z 0-9 _ -) and Telegram sends it in the X-Telegram-Bot-Api-Secret-Token header of every request, so your code can tell Telegram from anybody else[1]. Answer with a 2XX; any other status makes Telegram repeat the request and, after a reasonable number of attempts, give up[1]. Up to max_connections deliveries run at once, 40 by default and at most 100[1]. getWebhookInfo shows how many updates are waiting (pending_update_count) and the last delivery error[1].

Webhook vs polling at a glance

Polling Webhook
Who connects The bot connects to Telegram Telegram connects to the bot
What you need Outbound internet A public HTTPS address on 443, 80, 88 or 8443[1]
What runs A process that never stops A server that answers requests; it can be a function
While the bot is down Updates wait, up to 24 hours[1] Updates wait, up to 24 hours; failed deliveries are retried[1]
Parallel updates As your library handles them Up to max_connections at once[1]
Easy to get wrong Two copies polling one token; a leftover webhook The certificate, the port, a slow handler, an unchecked secret

Which one should you use?

grammY’s guide sums it up: long polling is simpler, webhooks are cheaper, and without a strong reason, use long polling[3]. In practice:

  • Use polling when the bot runs as a process anyway — a VPS, a container, a platform that keeps it up. There is no address to set up and nothing to secure on the way in, and Telegram’s own guide says there is nothing wrong with getUpdates[2].
  • Use a webhook when the code runs only per request — a serverless function — or already sits inside a web server, such as a Mini App’s backend. Then keep handlers fast: Telegram resends an update it did not get an answer to in time, and grammY’s webhookCallback gives up after 10 seconds by default for that reason[3].

Either way, delivery is at least once. A polling bot killed without a clean stop can see up to 100 updates again[4], and a webhook retries. For a payment or an order, de-duplicate on update_id[4].

How each library does it

Library Polling Webhook Sets the webhook itself
aiogram 3 dp.start_polling(bot) SimpleRequestHandler on aiohttp[5] Only if your code calls bot.set_webhook()[5]
python-telegram-bot 22 app.run_polling() app.run_webhook() with the webhooks extra[7] Yes, run_webhook always calls set_webhook[8]
Telegraf 4 bot.launch() bot.webhookCallback(), bot.createWebhook() or bot.launch({ webhook }) createWebhook and launch({ webhook }) do; webhookCallback does not[9]
grammY 1 bot.start() webhookCallback(bot, adapter)[3] No; you call bot.api.setWebhook()[3]

Three of them clean up when going back: python-telegram-bot’s run_polling always calls delete_webhook before polling[8], Telegraf’s launch() does the same without webhook options[9], and so does grammY’s bot.start()[10]. aiogram’s start_polling does not[6]: with aiogram, a webhook left behind has to be removed with deleteWebhook, or getUpdates fails[1].

Switching between them

  1. Webhook to polling: call deleteWebhook. With drop_pending_updates it also throws away what is waiting[1]; without it, the waiting updates arrive on the first getUpdates.
  2. Polling to webhook: stop the polling process first — setWebhook while it still polls only turns its getUpdates into errors — then set the webhook with a secret.
  3. Check: getWebhookInfo returns an empty url for a polling bot[1], and the address, the waiting count and the last error for a webhook bot.

Webhook vs polling on Livops

A bot is its own project type on Livops, and the mode is a setting of it: Settings → Connection to Telegram, Polling or Webhook, then Save and redeploy. Polling is the default. In the new-project wizard, a Python bot whose code uses webhook calls (SimpleRequestHandler, run_webhook) and never polls starts in webhook mode; a Node bot starts in polling until you change it.

Polling needs nothing: no domain, no port, no HTTPS. On every push the old process is stopped first — SIGTERM, then ten seconds — and the new one starts after it, so two copies never poll with one token. Messages sent in between wait at Telegram. About every ten minutes the platform asks Telegram’s getWebhookInfo about the bot, and warns you if a webhook is set (so polling receives nothing) or if messages pile up while the process runs.

Webhook is set up by the platform, and your code never calls setWebhook:

  • It generates WEBHOOK_SECRET and gives the bot PORT, WEBHOOK_PATH (/webhook unless you choose another) and WEBHOOK_URL.
  • Once the new version answers on PORT, it tells Telegram the address with that secret, and the edge refuses requests to the path without the header before they reach your code.
  • Telegram delivers to the platform’s hook address, which runs behind Cloudflare; the bot’s own address, for a Mini App, stays direct.
  • About every ten minutes it checks getWebhookInfo, shows the waiting count and the last error on the project page, and puts the webhook back if something moved it.
  • Switching back to polling deletes the webhook first, keeping the waiting updates; deleting the project drops them.

The token must be in BOT_TOKEN for webhook mode. What the code looks like in each library: aiogram, python-telegram-bot, Telegraf, grammY.

Both modes cost the same, 19 credits a month (19,000 so’m), because both keep a process running. One credit is 1,000 so’m; see Pricing.

Questions

Can a Telegram bot use webhook and polling at the same time?

No. The Bot API offers two mutually exclusive ways of receiving updates, and getUpdates does not work while a webhook is set. Call deleteWebhook to go back to polling.

Is a webhook faster than polling?

Rarely in a way that matters. A long-polling request is held open until there is something to return, so updates arrive within moments either way. The bigger difference is what you have to run: a loop that never stops, or an HTTPS address.

Which ports does a Telegram webhook accept?

443, 80, 88 and 8443, over TLS 1.2 or newer, on IPv4. Other ports do not work, and IPv6 is not supported for webhooks.

What happens to messages while my bot is down?

Telegram keeps them, for at most 24 hours, either way. A polling bot gets them on its next getUpdates; for a webhook, Telegram retries a failed delivery and gives up after a reasonable number of attempts.

Why does my polling bot get no messages?

Most often a webhook is still set, from an earlier host or a test. getWebhookInfo shows it; deleteWebhook removes it. python-telegram-bot, Telegraf and grammY delete it themselves when they start polling; aiogram does not.

Do I need to call setWebhook on Livops?

No. Switch the bot to webhook in Settings and the platform tells Telegram the address and a secret it generates. Your code listens on PORT, receives on WEBHOOK_PATH and checks WEBHOOK_SECRET.

Does a webhook bot cost less on Livops?

No. A bot is 19 credits a month (19,000 so'm) in either mode, because both keep a process running.

Sources

  1. Telegram Bot API — Getting updates (core.telegram.org). Checked .
  2. Telegram — Marvin's Marvellous Guide to All Things Webhook (core.telegram.org). Checked .
  3. grammY — Long polling vs. webhooks (grammy.dev). Checked .
  4. grammY — Scaling up IV, reliability (grammy.dev). Checked .
  5. aiogram — Webhook (docs.aiogram.dev). Checked .
  6. aiogram source — aiogram/dispatcher/dispatcher.py (raw.githubusercontent.com). Checked .
  7. python-telegram-bot — Application (v22.8) (docs.python-telegram-bot.org). Checked .
  8. python-telegram-bot source — telegram/ext/_updater.py (raw.githubusercontent.com). Checked .
  9. Telegraf source — src/telegraf.ts (raw.githubusercontent.com). Checked .
  10. grammY source — src/bot.ts (raw.githubusercontent.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your bot

Got a question? — @livops_uz