Telegram delivers a bot's messages in two ways. With long polling the bot fetches them; with a webhook Telegram sends them to an HTTPS address. The two do not work together, so you must decide before launch.
This guide is based on the official documentation of the Telegram Bot API and the grammY framework.
1. Comparison
| Aspect | Long polling | Webhook |
|---|---|---|
| Operations | The bot fetches messages with the getUpdates method. | Telegram sends the message in an HTTPS POST request to the address you set. |
| Public address and certificate | Not needed. | Required: HTTPS and a valid certificate. |
| Always-on program | Required. | Not necessarily; a request-triggered environment is possible. |
| Setup complexity | Simpler; grammY recommends it when there is no good reason for webhooks. | More steps: endpoint, SSL and a setWebhook call. |
2. Telegram's rules
- getUpdates: the timeout parameter enables long polling; the default of 0 means short polling, which the documentation reserves for testing.
- The offset must be one greater than the last processed update_id; this confirms earlier updates.
- setWebhook: HTTPS is required and the supported ports are 443, 80, 88 and 8443.
- If the endpoint does not respond with a 2xx status, Telegram retries and eventually gives up.
- The secret_token is 1–256 characters (A–Z, a–z, 0–9, _ and -) and arrives in every request in the X-Telegram-Bot-Api-Secret-Token header.
- According to the Telegram FAQ redirects are not supported, the certificate CN must exactly match the domain, and wildcard certificates may not work.
- According to grammY the webhook handler must finish within roughly 10 seconds or Telegram resends the update.
- While a webhook is set getUpdates does not work; to switch back you need deleteWebhook first.
3. Switching from long polling to a webhook
- Create an HTTPS endpoint for the bot on your own subdomain and check that it is reachable from outside.
- Stop the long polling process; do not call bot.start() when using a webhook.
- Call setWebhook with the endpoint and your own secret_token; pass the token from an environment variable.
- Check the url, pending_update_count and last_error_message fields in the getWebhookInfo response.
- The endpoint should verify the X-Telegram-Bot-Api-Secret-Token header and reject unknown requests.
curl -s "https://api.telegram.org/bot${BOT_TOKEN}/setWebhook" \
-d "url=https://bot.cegneved.hu/telegram/webhook" \
-d "secret_token=${WEBHOOK_SECRET}"4. Problems and where to look first
| Symptom | Initial investigation |
|---|---|
| The bot receives no messages | Whether a webhook is set; if so, getUpdates does not work. |
| No requests reach the webhook | The getWebhookInfo last_error_message field, the certificate and the port. |
| Repeated messages | The endpoint does not return 2xx or is too slow. |
| 429 error when sending | According to the FAQ about one message per second per chat and no more than 20 per minute in a group is advised. |



