Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Webhooks and Hosting

Long Polling vs Webhooks

Understand how bots receive events: Telegram getUpdates long polling with offsets, webhooks, and the Discord gateway versus interactions endpoint, with runnable demos.

A bot is a program that reacts to events, so the first question is how those events reach it. There are only three delivery models in use: your bot asks repeatedly (polling), the platform calls your URL (webhook), or your bot keeps a socket open and the platform pushes events down it (Discord's gateway). Each has a different cost, and the choice shapes how you host the bot.

All demos in this lesson run a small fake Telegram server on 127.0.0.1 and talk to it with fetch. It speaks the same JSON shape as the real Bot API (getUpdates, offset, timeout), so the polling loop is the real thing; only the server is a stand-in. Real bots need a token from BotFather, which this site never asks for.

The three models

flowchart LR
    subgraph Polling
      B1[Bot] -- "getUpdates (waits up to 30 s)" --> T1[Telegram]
      T1 -- "updates" --> B1
    end
    subgraph Webhook
      T2[Telegram] -- "HTTPS POST /hook" --> B2[Your public URL]
    end
    subgraph Gateway
      D[Discord] <-- "WebSocket, heartbeats, events" --> B3[Bot]
    end

Polling and the gateway start from your side and need no public address. A webhook needs a public HTTPS URL.

Model Platform Needs public URL Idle cost Good for
Long polling Telegram no one open request development, small bots, home servers
Webhook Telegram, Discord interactions yes (HTTPS) none serverless, scale-to-zero, many bots
Gateway WebSocket Discord no one connection anything that needs messages, presence, voice, member events

Long polling

With getUpdates, the bot sends a request and Telegram holds it open until there is something to say or timeout seconds pass. That is long polling: low latency without hammering the server.

Every update has an increasing update_id. To acknowledge updates you pass offset = last_update_id + 1 on the next call; Telegram then forgets everything before it. If you forget the offset, the same updates arrive again forever.

Node.js A fake Bot API and a real polling loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Three details a production loop needs that the demo skips:

  1. Error handling and backoff. A network blip must not crash the loop. Catch, wait with growing delays, retry (see rate limits and backoff).
  2. Only one poller per token. A second getUpdates caller gets a 409 Conflict. Two running copies of a bot is the classic cause.
  3. Processing failures. If you advance the offset before handling an update and then crash, that update is lost. If you advance after, a crash re-delivers it. Pick on purpose; for most bots "lose rarely" beats "process twice".

Webhooks

A webhook reverses the direction. You register a public HTTPS URL once (setWebhook), and Telegram sends each update as a POST with a JSON body. You must answer quickly with 200; if you do not, Telegram retries and may back off. The server side is covered in the Telegram webhook server lesson.

Node.js The shape of a webhook receiver

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Discord: gateway and interactions

Discord splits things differently:

  • Gateway (WebSocket) delivers messages, member joins, reactions, voice state and presence. You identify with your token and intents, then must send heartbeats at the interval the server gives you. Libraries such as discord.js handle this; no public URL is needed.
  • Interactions endpoint (webhook) delivers only interactions: slash commands, buttons, modals, select menus. You give Discord an HTTPS URL, and Discord signs each request with Ed25519, which you must verify (next lessons). A bot that uses only slash commands can run with no gateway connection at all, on serverless platforms.

A bot that needs to read ordinary messages or see who joined needs the gateway. A bot that only answers commands can use just the endpoint.

Choosing

  • Starting out or developing locally: Telegram long polling, Discord gateway.
  • Production Telegram bot on a platform that can wake on a request, or with several instances behind a load balancer: webhook.
  • Never both at once for Telegram: setting a webhook disables getUpdates, and deleteWebhook switches back.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the offset parameter of getUpdates do?

    Passing last update_id + 1 tells Telegram to discard everything older.
  2. 2. Which delivery model needs a public HTTPS URL?

    Polling and the gateway are outbound connections from your bot; a webhook is an inbound request from the platform.
  3. 3. Why should a webhook handler answer 200 before doing slow work?

    Platforms expect a fast acknowledgment; slow handlers cause timeouts and duplicate deliveries.
  4. 4. A Discord bot only answers slash commands. What is true?

    Interactions can be delivered to an HTTPS endpoint, so no WebSocket is needed for command-only bots.

Summary

  • Events reach a bot by polling, by webhook, or over a pushed WebSocket (Discord's gateway).
  • Telegram long polling uses timeout and offset; exactly one poller per token.
  • Webhooks need public HTTPS and a fast 200 response.
  • Discord's gateway is for messages and presence; the interactions endpoint is for commands and components.
  • Use polling or the gateway while developing, and a webhook when you want scale-to-zero or multiple instances.

Next: A Telegram webhook server