Telegram bot webhook vs polling: how to choose and set it up
Polling asks Telegram for new messages again and again. A webhook lets Telegram push each message to your server. Here is how to choose and set up both.
Direct answer
Use polling while you are building and testing a Telegram bot, and use a webhook once the bot has real users and needs fast replies. Polling is simpler because your bot asks Telegram for new messages on a regular schedule, and it works on your own computer. A webhook is faster at scale because Telegram pushes each new message to your server the moment it arrives, and it needs a public HTTPS URL that is always online. Most bots start with polling and move to a webhook later, and the switch takes a few careful steps.
What is polling?
Polling is a method where your bot asks Telegram, again and again, whether anything new has arrived. The bot sends a request, Telegram replies with any new messages, and the bot repeats the request a moment later.
Polling is easy to start because it needs no public server address and no domain name. You can run it on your laptop while you write the code, and it works behind a home router. The cost is a small delay and extra requests. Replies arrive a second or two after the user sends a message, and the bot keeps asking even when nobody is talking to it.
What is a webhook?
A webhook is a method where Telegram sends each new message to your server as soon as it arrives. You give Telegram a public HTTPS address, called the webhook URL, and Telegram posts every update to that address.
A webhook is faster than polling because there is no waiting between checks. It also uses fewer requests, because Telegram only contacts your server when something actually happens. The cost is the setup. Your server needs a public HTTPS URL, a valid certificate, and a process that stays online to receive the posts.
How do polling and webhooks compare?
| Polling | Webhook | |
|---|---|---|
| How it works | Your bot asks Telegram for new messages on a loop | Telegram sends new messages to your server |
| Speed | A short delay between checks, usually 1 to 2 seconds | Near instant, Telegram pushes the update |
| Server needed | No public address, works on a laptop | Public HTTPS URL, always online |
| Setup | Simple, a few lines of code | More work, HTTPS, a domain, and a web server |
| Traffic | Requests keep coming even with no messages | Requests arrive only when a message arrives |
| Best for | Learning, testing, small bots, and prototypes | Live bots, busy bots, and fast replies |
| If the server stops | The bot misses messages until it restarts | Telegram retries for a while, then updates can be lost |
Which one should I use?
Use polling if you are learning, testing a new feature, or running a bot for a small group. It is the right choice when you want to change code often and restart the bot without touching a server. It is also the right choice when you have no domain and no public server yet.
Use a webhook if the bot has real users, handles bookings or payments, or needs replies within a second. A webhook suits a bot that gets steady traffic all day, because it avoids thousands of empty checks. The code that answers a message does not change. Only the way Telegram delivers the message changes.
How do I set up polling with python-telegram-bot?
Polling with python-telegram-bot takes a few steps. The library handles the loop for you, so you write handlers and call run_polling.
- Create the bot with BotFather. Open Telegram, search for @BotFather, send /newbot, and pick a display name and a username that ends in bot. BotFather replies with a token.
- Keep the token safe. Put it in an environment variable, and never paste it into code that you commit to a public repo. If it leaks, revoke it in BotFather and get a new one.
- Install the library in a virtual environment with pip install python-telegram-bot, and save it in a requirements file.
- Write a handler for /start and a handler for plain text.
- Build the application with your token, add the handlers, and call run_polling. Run the file with python bot.py, then send /start to your bot in Telegram.
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters
TOKEN = "YOUR_BOT_TOKEN"
async def start(update: Update, context):
await update.message.reply_text("Hello! Send me a message and I will echo it back.")
async def echo(update: Update, context):
await update.message.reply_text(update.message.text)
def main():
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
app.run_polling()
if __name__ == "__main__":
main()
Test polling by sending a few messages in a row, then a message while the bot is stopped. The first messages should get replies. The last one should get no reply until you restart the bot. For a closer look at keeping a bot online, see the guide to Telegram bot hosting.
How do I set up a webhook?
A webhook needs a public server first. Get a domain, point it at your server, and set up HTTPS with a valid certificate. Telegram will not accept a plain HTTP address, and it will not accept a self-signed certificate for a webhook.
- Deploy your bot code to a server that stays online, and make sure the web framework can receive POST requests at one path, such as /webhook.
- Choose a secret token, a long random string that only you and Telegram know. Telegram sends it in a header with every update, and your server checks it before it trusts the message.
- Start the web server, then tell Telegram the webhook URL. With python-telegram-bot, call set_webhook with the full HTTPS address and the secret token, or set it once from a small script.
- Send a test message to the bot. Check the server logs for the incoming POST request, and check that the bot replies in Telegram.
from telegram.ext import Application
TOKEN = "YOUR_BOT_TOKEN"
WEBHOOK_URL = "https://example.com/webhook"
SECRET_TOKEN = "YOUR_SECRET_TOKEN"
async def main():
app = Application.builder().token(TOKEN).build()
await app.bot.set_webhook(url=WEBHOOK_URL, secret_token=SECRET_TOKEN)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
Replace example.com with your own domain, and replace YOUR_SECRET_TOKEN with a long random string. Keep both values in environment variables, and never commit them. If the webhook stops working, check the certificate first, then check that the server is reachable, then check the logs for errors.
How do I switch from polling to webhook safely?
Switching is a common point of failure, because polling and a webhook fight each other if both run at once. Telegram delivers an update once, so a message picked up by polling never reaches the webhook, and a message sent to the webhook never reaches polling.
- Stop the polling process completely, and confirm it has stopped before you do anything else.
- Delete the old webhook if one was set, or set the new webhook URL, and confirm Telegram accepted it.
- Start the webhook server, and send a test message. Check the logs for the incoming update and the outgoing reply.
- Watch the logs for the first hour. Look for duplicate replies, missing replies, and errors from handlers that worked under polling.
Switch outside peak hours if the bot handles orders or bookings, so a short gap affects as few customers as possible.
What mistakes should I avoid?
The most common mistake is running polling and a webhook at the same time. One process wins each update, and the other sees nothing, so replies arrive at random or not at all. Run one method at a time, and stop the old one before you start the new one.
The next mistake is a webhook without HTTPS. Telegram rejects plain HTTP, and it rejects expired or self-signed certificates. Use a valid certificate, and renew it before it expires.
Another mistake is setting the webhook and then redeploying without setting it again. Some deploys reset the webhook, and the bot goes quiet even though the server is running. Check the webhook status after every deploy.
No error handling is a quieter mistake. A single bad message should never crash the bot, so wrap handlers in error handling, log the failure, and reply with a short apology. Without logging, you will not know what broke or when. BDBOTS builds bots with Python, Pyrogram and Telethon, React and Node.js, MongoDB and PostgreSQL, and Docker, with hosting at 99.9% uptime.
What is the risk when AI builds the bot?
Veracode's 2025 GenAI Code Security Report found AI-generated code introduced a security flaw in 45% of cases tested. Missing input checks, open databases, and hardcoded tokens show up a lot in that kind of code. AI can also recommend libraries that do not exist, and installing one may install a malicious package registered under that made-up name.
AI-generated code has no architect, so each new feature gets stacked on the last, with duplicated logic, until every change risks breaking the bot. Without hosting, auto-restart, logging, and monitoring, an AI-built bot dies quietly when the laptop closes or the server restarts. Use AI to learn and to draft, and have a human review, test, and own the code for a customer-facing bot.
Every project built by BDBOTS comes with lifetime free support. If something breaks, if Telegram changes its API, or if a payment flow stops working, there is a team to call without a new support contract or a surprise bill for basic help. BDBOTS was founded in 2019, has completed more than 1000 projects for over 500 clients, and offers a free consultation with a custom-priced quote within 24 hours. Simple bots can be ready in 3 to 7 days, and complex platforms typically take 2 to 6 weeks. Learn more at BDBOTS or write to contact@bdbots.org or message @bdbots_bot on Telegram.
Frequently asked questions
Can I use polling and a webhook at the same time?
No. Telegram delivers each update once, so polling and a webhook fight over the same messages. Run one method at a time, and stop the old one before you start the new one.
Does a webhook need HTTPS?
Yes. Telegram only accepts a public HTTPS webhook URL with a valid certificate. Plain HTTP and self-signed certificates are rejected, and the bot will not receive updates.
How long does it take to switch from polling to a webhook?
The switch itself takes a few minutes, once the server, domain, and HTTPS are ready. Allow an hour to test it, check the logs, and confirm replies arrive, and switch outside peak hours if the bot handles orders or bookings.