Skip to main content

Webhook

A webhook is an HTTP endpoint that Telegram calls automatically whenever an event occurs — such as a user sending a message to a bot, a join request arriving, or a payment being confirmed. Instead of your server repeatedly polling the Telegram API, Telegram pushes updates to your endpoint in real time. Autogram registers a webhook with Telegram to receive updates that trigger automations, process identity verification codes, and handle channel events without polling overhead.

Registering the endpoint

Telegram will only deliver to an HTTPS URL, and only on a short list of permitted ports. You can register a secret token at the same time; Telegram then echoes it in a header on every delivery, which is how the receiver distinguishes a genuine update from a forged POST to a public URL. Only one delivery mode is active at a time, so long polling stops returning updates once a webhook is set.

Delivery, retries and backpressure

Telegram expects a 2xx response. Anything else is retried a bounded number of times and then abandoned, so a handler that crashes on a malformed payload can lose that update permanently. Because updates are pushed rather than pulled, a slow handler becomes a queue: the standard shape is to acknowledge immediately and do the real work on a background worker.

Subscribing to the right updates

A webhook can be narrowed to specific update types, and the default set deliberately omits a few noisy ones that must be requested explicitly. Narrowing cuts traffic noticeably but silently hides events you forgot to ask for. When automations go quiet, the first thing to read is the webhook status the API returns — it reports how many updates are waiting for delivery and the most recent error.

What happens when your webhook endpoint goes down?

Telegram keeps updates for your bot until the bot receives them, but never longer than 24 hours. If the endpoint is down for a short time, the backlog arrives when it recovers. If it stays down for more than a day, the oldest updates are lost for good. To see the current state, call getWebhookInfo. It returns pending_update_count, the time and text of the last delivery error (last_error_date and last_error_message), the connection limit and the update types you subscribed to. A growing pending count with a fresh error message means Telegram can reach you but your handler rejects the requests. A zero count with no activity often points to the subscription, not the server. To go back to polling, call deleteWebhook, because getUpdates does not work while a webhook is set. Pass drop_pending_updates to discard the backlog instead of replaying it.

How does Autogram use the webhook for your channel?

You do not set up a webhook yourself. When you connect a channel, you add the Autogram bot as an administrator, and Autogram receives the bot's updates through its own webhook. It registers the webhook with a secret token and subscribes to message, my_chat_member, chat_member and callback_query updates. The chat_member type is one that Telegram leaves out of the default set, so it must be requested by name. Autogram needs it to see member changes such as joins. Autogram repeats the registration every day and when it deploys a release, so a changed subscription does not stay inactive. A bot token has one webhook URL at a time. If you run a separate bot of your own, give it its own token, because pointing the same token at another URL takes updates away from the first receiver.

Worked example: reading getWebhookInfo

Three readings cover most cases. First: pending_update_count is 0 and there is no last_error_message. Delivery works. If the bot still seems quiet, check allowed_updates, because chat_member, message_reaction and message_reaction_count are not in the default set. Second: pending_update_count grows and last_error_message reads like "Wrong response from the webhook: 500 Internal Server Error". Telegram reaches your server and your handler fails, so read your logs for the time in last_error_date. Third: the pending count is stable and the error is a timeout or a certificate error. Telegram cannot complete the request, so check the certificate chain, the port and DNS. After you fix the cause, the backlog arrives, if the updates are less than 24 hours old.

Why can Telegram not reach your endpoint?

Telegram documents what a webhook server needs. It must accept IPv4, because IPv6 is not supported for webhooks. It must listen on port 443, 80, 88 or 8443 and handle TLS 1.2 or newer. The certificate must be verified or self-signed, match the domain you registered and come with every intermediate certificate. If you filter by IP, allow the subnets 149.154.160.0/20 and 91.108.4.0/22. A firewall rule that blocks unknown clients can block these subnets. Telegram may deliver an update again, so use update_id to ignore repeats. The max_connections setting runs from 1 to 100 and defaults to 40. In Autogram the webhook checks the secret header, answers 401 when it does not match and acknowledges every valid update. Slow Telegram calls run in background tasks, so they do not hold the request.

Put these concepts to work

Automate your Telegram publishing with AI content, quality scoring, and smart scheduling.