How to switch a Telegram bot from polling to webhooks
Move a bot off long polling and onto a webhook URL so Telegram pushes updates to your server.
Polling is great for local development: your bot repeatedly asks Telegram for new messages. In production, webhooks are better. Telegram pushes each update to a public HTTPS URL the instant it arrives, which is faster and cheaper. This guide migrates a bot from one to the other.
What you need
- A working bot and token
- A public HTTPS endpoint (a deployed server or a tunnel like ngrok)
- A valid TLS certificate (Telegram requires HTTPS)
Step 1: Understand the trade-off
| Aspect | Polling | Webhook |
|---|---|---|
| Setup | Zero config | Needs a public HTTPS URL |
| Latency | Up to a few seconds | Near instant |
| Best for | Local dev | Production |
| Server | Can run anywhere | Must be reachable from internet |
Step 2: Build an HTTP endpoint
Telegram POSTs each update as JSON to your URL. Here is a minimal Express handler that reads the message and replies through sendMessage.
import express from "express";
const app = express();
app.use(express.json());
const token = process.env.TELEGRAM_BOT_TOKEN;
app.post("/webhook", async (req, res) => {
const msg = req.body.message;
if (msg?.text) {
await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chat_id: msg.chat.id,
text: `You said: ${msg.text}`,
}),
});
}
res.sendStatus(200);
});
app.listen(3000, () => console.log("Listening on :3000"));Step 3: Register the webhook
Tell Telegram where to send updates with setWebhook. Adding a secret token header lets you verify that incoming requests really come from Telegram.
Step 4: Confirm and roll back if needed
Check the active webhook with getWebhookInfo. To go back to polling later, call deleteWebhook first, otherwise the two methods conflict.
curl "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/getWebhookInfo"
# to revert:
curl "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/deleteWebhook"Result
Telegram now delivers updates straight to your server with almost no delay, and the secret header keeps the endpoint safe from spoofed requests.
Watch related tutorials
1:42:18
28:14
41:09
9:47
8:23
52:31