On this page
The essentials
- A bot is a program. If it stops, the bot goes silent: Telegram keeps incoming updates for no longer than 24 hours.
- Updates arrive either by polling with getUpdates or through a webhook. The two do not work at the same time.
- A webhook needs a public HTTPS address on port 443, 80, 88 or 8443, and a check of the secret header.
- On your own server the bot runs as a systemd service with automatic restart, and the token sits in a separate file readable only by its owner.
- If the token leaks, issue a new one in BotFather with the /token command.
The bot answered messages briskly all evening. In the morning the laptop was closed, its owner got on the metro — and the bot fell silent. Users keep writing, the bot does not react, and by evening some of their questions will never be seen.
This is not a Telegram failure. A bot is a program that runs on someone's computer, and Telegram merely passes messages to it. While the program runs, the bot is alive. So "running a bot 24/7" means answering three questions: where the process lives, who brings it back after a crash and where the token is kept.
- 24 hours
- How long Telegram keeps updates the bot has not yet received.
- 4 ports
- Supported for webhooks: 443, 80, 88 and 8443.
- 1 instance
- How many copies of a bot should receive updates on one token.
How it works
Why the bot falls asleep
Telegram does not run bot code. It accepts users' messages and holds them for your program, and the program collects them and replies. If the program is not running, messages pile up on Telegram's side, but for no longer than 24 hours. Whatever the bot has not collected within a day is lost.
Hence a simple rule: a bot needs a computer that never switches off, and a program that restarts itself after a crash.
How a bot gets messages
Polling or webhook
Long polling (getUpdates) | Webhook (setWebhook) | |
|---|---|---|
Who starts | The bot asks Telegram whether there are new messages | Telegram sends messages to your address |
What you need | Any computer with internet access | A public HTTPS address on port 443, 80, 88 or 8443 |
Pros | Simple: no domain or certificate needed | No need to hold a constant connection; suits platforms that wake code on request |
Cons | The process must run all the time | Needs HTTPS, a domain and a check that the request came from Telegram |
The two methods are mutually exclusive: while a bot has a webhook set, getUpdates does not work. To go back to polling, the webhook is removed with deleteWebhook. The bot from "How to create a Telegram bot: BotFather, your first Python code, and one rule about the token" uses long polling — that is the one we will move to a server.

Options
Where to host it
Option | Suitable mode | Pros | Cons |
|---|---|---|---|
Your own computer | Polling | Free and simple | Works only while the computer is on and not asleep |
A virtual server (VPS) | Polling or webhook | Runs round the clock, full control | You have to set up the system, updates and autostart |
An app platform or serverless functions | Webhook | No server to administer | The platform's own limits; HTTPS required |
For a first bot with long polling, a small Linux virtual server is convenient. Next: how to make it run there without you.
Step by step on a VPS
A systemd service: automatic restart
- Copy bot.py to the server, for example to /opt/bot, and run it as a separate user without administrator rights.
- Put the token in a file /etc/bot/token.env as a single line TELEGRAM_BOT_TOKEN=your_token, and lock it down with chmod 600: only its owner can read it.
- Describe the service in the file /etc/systemd/system/bot.service.
[Unit]
Description=Telegram bot
After=network-online.target
Wants=network-online.target
[Service]
User=botuser
WorkingDirectory=/opt/bot
EnvironmentFile=/etc/bot/token.env
ExecStart=/usr/bin/python3 /opt/bot/bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target- Enable the service and watch the log.
sudo systemctl daemon-reload
sudo systemctl enable --now bot
journalctl -u bot -fRestart=always restarts the bot after any stop, RestartSec=5 adds a five-second pause between attempts, and enable starts the service at every server boot. The token never ends up in the code or in your command history.
The second mode
Webhook: when you need an address
With a webhook, your server or platform receives Telegram's requests over HTTPS. When setting it up with setWebhook, you pass the address and a secret. Every Telegram request then arrives with the X-Telegram-Bot-Api-Secret-Token header, and anything that comes without it is discarded.
import hmac
import os
WEBHOOK_SECRET = os.environ["WEBHOOK_SECRET"]
def setup_webhook(call, url):
# call is the Bot API helper from the bot-creation article
call("setWebhook", url=url, secret_token=WEBHOOK_SECRET, allowed_updates=["message"])
def is_from_telegram(headers):
received = headers.get("X-Telegram-Bot-Api-Secret-Token", "")
return hmac.compare_digest(received, WEBHOOK_SECRET)Keep the secret the same way as the token: in an environment variable, not in the code.
Security
The token: the key to the bot
The token from BotFather is full access to the bot: with it, anyone can control the bot, receive what people send it and reply in its name. So it lives only on the server, in a file with restricted permissions, and never goes into a repository, logs or screenshots.
If the token does leak, open BotFather and issue a new one with the /token command, then replace it in the file on the server and restart the service. What to do after a leak is covered in detail in the "If the token leaks" section of "How to create a Telegram bot: BotFather, your first Python code, and one rule about the token".
Checklist
Before launch
Questions
Can I keep the bot on my home computer?
You can, if the computer is on round the clock and never sleeps. But any power or internet outage stops the bot, and messages older than 24 hours are lost. For constant operation a server is more reliable.
What happens to messages while the bot is down?
Telegram keeps them for up to 24 hours. When the bot starts again, it receives the accumulated updates and replies to them. Anything older than a day the bot will never see.
Why do two copies of the bot get in each other's way?
With long polling, only one program can collect updates. If you start a second copy with the same token, they begin to conflict, and some messages reach the wrong copy. For testing, create a separate bot in BotFather with its own token.
A bot works for as long as its process runs — that is all there is to know about hosting. The rest is choosing a computer that never sleeps and teaching it to bring the bot back after any crash.
A systemd service with automatic restart and the token in a locked file solve both on an ordinary virtual server. A webhook will come in handy later, when the bot moves to a platform that wakes code on request.


