Webhook or long polling? How Botxona works
Updated:
Telegram bots can receive updates (messages, button presses) in two ways: via a webhook (Telegram pushes them to your server) or via long polling (the bot keeps asking Telegram itself, getUpdates). Botxona supports both. Long polling is the simplest and the recommended default; if your bot already uses a webhook, you don't need to rewrite it.
Which one to choose
- Long polling — no port, no HTTP server, nothing to configure.
start_polling()/run_polling()/bot.launch()/bot.start()work out of the box. Choose this for new bots. - Webhook — choose it if the code already uses a webhook (migrated from another host, or you prefer that style). You don't need a public domain, an SSL certificate or an open port — Botxona delivers updates straight into the bot's container.
How it works
Botxona runs thousands of bots on shared infrastructure, and every bot is scale-to-zero: it stops completely when idle (details). The real Telegram webhook always belongs to the platform: Botxona's always-on proxy receives every update, queues it in order, wakes the bot if it is asleep, and hands the update over in the way you chose:
- Polling: the bot calls
getUpdatesand the proxy answers from its own buffer. - Webhook: the proxy itself sends each update as
POSTto the HTTP server inside your bot's container, one at a time and in order. The server answers200— the update is confirmed. Updates are never lost: while the server is down or asleep, they wait in the queue.
Either way, delivery is ordered and at-least-once.
The three modes (Settings → Updates)
| Mode | What it does |
|---|---|
| Automatic (default) | Works like Telegram. If the bot calls setWebhook, updates start going to the webhook; if it calls deleteWebhook, back to polling. Most existing webhook bots work with no changes except "listen on ". |
| Polling | Polling is forced. setWebhook is ignored (it replies ok but does nothing) and the owner gets a notice. Useful if a bot calls setWebhook by accident. |
| Webhook | Webhook is forced from the very first second, even if the bot never calls setWebhook (typical for a bot that registered its webhook once by hand). |
You can also choose the mode in botxona.yaml:
updates: webhook # auto | polling | webhook
webhook:
port: 8080 # the port your HTTP server listens on
path: /webhook # used when the bot doesn't call setWebhook itself
If you chose the mode in the dashboard, it takes priority over botxona.yaml. If it isn't set anywhere, auto applies.
Environment variables Botxona gives your bot
| Variable | Value |
|---|---|
PORT | The port your HTTP server must listen on (0.0.0.0:); 8080 by default |
WEBHOOK_PATH | The path to serve, /webhook by default |
WEBHOOK_URL | https://webhook.botxona.internal + the path — pass it to setWebhook |
WEBHOOK_DOMAIN | webhook.botxona.internal — for frameworks that want a domain and a path separately (Telegraf) |
WEBHOOK_SECRET | The secret token: pass it as secret_token; Botxona sends it back in the X-Telegram-Bot-Api-Secret-Token header |
UPDATE_DELIVERY | auto, polling or webhook — lets one codebase choose run_polling or run_webhook |
The host in WEBHOOK_URL is not used for routing: Botxona only reads the path from it and always delivers to your own container. Don't add these variables to .env.example or to the dashboard — they are set automatically (see Environment variables).
Rules for a webhook bot
- Run a plain HTTP server on
0.0.0.0:— no TLS, no certificate. - Don't publish a port or buy a domain — only Botxona itself can reach the container, over a private network.
- Remove the Telegram IP filter (for example aiogram's
ip_filter_middleware) — requests come from Botxona, not from Telegram. The secret token is enough. - Keep the Telegram API address at the library default.
- Reply with
200quickly. Anything else is retried and recorded as an error. A reply of the form{"method": "sendMessage", ...}in the response body also works.
Framework snippets
aiogram 3:
import os
from aiohttp import web
from aiogram.webhook.aiohttp_server import SimpleRequestHandler, setup_application
async def on_startup(bot):
await bot.set_webhook(os.environ["WEBHOOK_URL"], secret_token=os.environ["WEBHOOK_SECRET"])
dp.startup.register(on_startup)
app = web.Application()
SimpleRequestHandler(dp, bot, secret_token=os.environ["WEBHOOK_SECRET"]).register(app, path=os.environ["WEBHOOK_PATH"])
setup_application(app, dp, bot=bot)
web.run_app(app, host="0.0.0.0", port=int(os.environ["PORT"]))
python-telegram-bot 21:
app.run_webhook(
listen="0.0.0.0", port=int(os.environ["PORT"]),
url_path=os.environ["WEBHOOK_PATH"].lstrip("/"),
webhook_url=os.environ["WEBHOOK_URL"],
secret_token=os.environ["WEBHOOK_SECRET"],
)
grammY (Express):
app.post(process.env.WEBHOOK_PATH, webhookCallback(bot, "express", { secretToken: process.env.WEBHOOK_SECRET }))
await bot.api.setWebhook(process.env.WEBHOOK_URL, { secret_token: process.env.WEBHOOK_SECRET })
app.listen(+process.env.PORT)
Telegraf 4:
bot.launch({
webhook: {
domain: process.env.WEBHOOK_DOMAIN,
port: +process.env.PORT,
path: process.env.WEBHOOK_PATH,
secretToken: process.env.WEBHOOK_SECRET,
},
})
go-telegram/bot:
b, _ := bot.New(token, bot.WithWebhookSecretToken(os.Getenv("WEBHOOK_SECRET")))
b.SetWebhook(ctx, &bot.SetWebhookParams{URL: os.Getenv("WEBHOOK_URL"), SecretToken: os.Getenv("WEBHOOK_SECRET")})
go b.StartWebhook(ctx)
http.ListenAndServe(":"+os.Getenv("PORT"), b.WebhookHandler())
During the bot check, Botxona looks for an open port in the test container and reports it in the check report ("Webhook server is listening on port 8080"). More: Reading the check result.
Common errors
"409 Conflict: can't use getUpdates method while webhook is active" — the bot is in webhook mode (it called setWebhook, or the mode is set to Webhook) but the code also calls getUpdates. Pick one: either call deleteWebhook and stay on polling, or remove the polling call. A different 409 — "terminated by other getUpdates request" — means one token is being polled from two places at once (for example your computer and Botxona): stop the extra one. More: Errors and fixes.
The bot doesn't answer in webhook mode. Check Settings → Updates: the last delivery error is shown there.
Connection refused— nothing listens on the port. Make sure the server listens on0.0.0.0:(notlocalhost) and the Port in the settings matches the real one.Wrong response from the webhook: 404— the path is wrong. Serve.Wrong response from the webhook: 401/403— thesecret_tokendoesn't match, or an IP filter is rejecting the request.Wrong response from the webhook: 500— an exception in the handler; look at the logs. A "poisoned" update that fails 8 times in a row is skipped so it doesn't block the queue.
If the bot calls getWebhookInfo itself, it sees the same last_error_message.
A webhook set earlier on another host. Botxona's platform takes over the real webhook for your token, so an old address does no harm. In polling mode it's still handy to call deleteWebhook on startup — it's harmless.
Next step
Upload your bot: Hosting your first bot in 10 minutes.