python-telegram-bot hosting: deploying a v22 bot
A python-telegram-bot bot runs as one Application: run_polling() for polling, run_webhook() with the webhooks extra for a webhook. Both stop cleanly on SIGTERM. On Livops it runs from a push with bot.py and requirements.txt for 19 credits a month (19,000 so'm).
By Livops team, the people who build and run LivopsUpdated
python-telegram-bot (PTB) is the oldest of the big Python Telegram libraries, and since v20 it is fully asyncio: everything hangs off one Application, which polls or serves a webhook and shuts itself down on a signal. This page covers what a PTB v22 bot needs before it goes on a server — polling or webhook, the extras, persistence, shutdown — and then exactly how Livops builds and runs one.
What does a python-telegram-bot bot need in production?
The current major version
The current release is 22.8, from 12 June 2026, and it needs Python 3.10 or newer[5]. The only required dependency is httpx; webhooks, the job queue and the rate limiter are optional extras[5]. Pin the major version you wrote for: v20 moved the library to asyncio and replaced Dispatcher with Application[7], so code built on Dispatcher is older than that and needs its own pin.
python-telegram-bot[job-queue]>=22,<23
One way of receiving updates
Polling and a webhook are mutually exclusive in the Bot API[6]. PTB handles the switch for you in one direction: run_polling() always calls delete_webhook before it starts[3], so a bot that used to run on a webhook somewhere else starts polling cleanly.
A clean exit
run_polling() and run_webhook() stop on SIGINT, SIGTERM and SIGABRT on Linux by default[2]. On the way out PTB stops the updater, then the application, then calls shutdown(), which flushes persistence[2]. You do not need signal handlers of your own; use post_stop or post_shutdown for cleanup[1].
Data that outlives the process
PicklePersistence saves user_data, chat_data and the rest to a file, by default every 60 seconds (update_interval)[4]. That file must be on a disk that survives a redeploy — not next to the code.
A production PTB bot, polling
import os
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes, PicklePersistence
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
context.user_data["seen"] = context.user_data.get("seen", 0) + 1
await update.message.reply_text(f"Hello! Visit number {context.user_data['seen']}.")
def main() -> None:
persistence = PicklePersistence(filepath=os.path.join(os.environ["DATA_DIR"], "bot.pickle"))
app = Application.builder().token(os.environ["BOT_TOKEN"]).persistence(persistence).build()
app.add_handler(CommandHandler("start", start))
app.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()
The same bot on a webhook
run_webhook() needs the webhooks extra, which installs tornado[1][5]. Three of its defaults matter on a server:
listenis127.0.0.1[1], which nothing outside the container can reach. Use0.0.0.0.- With
secret_tokenset, a request without the header, or with the wrong one, is answered 403 Forbidden[1]. - It always calls
set_webhook. With nowebhook_url, it builds one fromlisten,portandurl_path[3] —http://0.0.0.0:…, which is no address Telegram can use. Always passwebhook_url.
app.run_webhook(
listen="0.0.0.0",
port=int(os.environ["PORT"]),
url_path=os.environ["WEBHOOK_PATH"],
secret_token=os.environ["WEBHOOK_SECRET"],
webhook_url=os.environ["WEBHOOK_URL"],
)
python-telegram-bot[webhooks,job-queue]>=22,<23
Deploy a python-telegram-bot bot on Livops
- Push a repository with
bot.pyandrequirements.txt, or upload the folder as a.zip, and create a project from it in the cabinet. - Under Settings → Variables, add
BOT_TOKENand mark it as a secret: its value is never shown again. - Deploy. The log shows the install and the start.
Livops finds python-telegram-bot in requirements.txt (or in your imports — import telegram — when there is no requirements file) and makes the project a Telegram bot (Python). It installs into a virtualenv and starts bot.py, or, without one, main.py, app.py, run.py, src/<package>/__main__.py or a file with if __name__ == "__main__". To name it yourself, add livops.json:
{
"kind": "bot",
"start": ".venv/bin/python bot.py"
}
Polling is the default, and the code above runs as it is: no port, no domain. DATA_DIR is set for you, and /app/data is kept across restarts and deploys, so bot.pickle survives a push. For a webhook, open Settings → Connection to Telegram, choose Webhook and press Save and redeploy. The platform generates WEBHOOK_SECRET, sets PORT, WEBHOOK_PATH and WEBHOOK_URL, tells Telegram the address with the secret once the bot answers, and refuses requests to the path without it. Because PTB calls set_webhook itself, pass it WEBHOOK_URL and WEBHOOK_SECRET as above: it then repeats what the platform already set. In webhook mode the token must be in BOT_TOKEN.
What happens after that:
- On a push, a polling bot is stopped first — SIGTERM, then ten seconds before it is killed, the window PTB’s shutdown and last persistence flush have to fit in — and the new version starts after it. A webhook bot’s new copy must answer on
PORTwithin a minute and takes the updates before the old one stops. - If the bot exits in its first 30 seconds, the deploy is marked failed with its last lines in the log. A
No module named 'telegram'there meanspython-telegram-botis missing fromrequirements.txt. - If it crashes later, it is started again; ten crashes inside 15 minutes and it is stopped, and you are notified with the last lines.
- The container’s clock runs on UTC. Give JobQueue times an explicit zone,
datetime.time(9, 0, tzinfo=ZoneInfo("Asia/Tashkent")), rather than relying on the clock.
A bot costs 19 credits a month (19,000 so’m), polling or webhook. One credit is 1,000 so’m; see Pricing.
Livops or a VPS?
A VPS gives you root and room for many bots on one machine. You also get the work: Python, a systemd unit with Restart=always, deploys by hand, a certificate if you use a webhook, and system updates. On Livops the push is the deploy and restarts are the platform’s, but there is no root; a bot that needs system software of its own fits a VPS or its own Dockerfile better.
Questions
Does python-telegram-bot v13 code run?
Only if you pin it. Code built on Dispatcher is v13 or older; v20 replaced Dispatcher with Application and moved the library to asyncio. Pin the version you wrote for in requirements.txt, or port the bot to Application.
Why does my webhook bot call setWebhook itself?
run_webhook always calls set_webhook at start. Give it webhook_url from the WEBHOOK_URL variable and secret_token from WEBHOOK_SECRET, and it repeats exactly what the platform already told Telegram.
Do I need python-telegram-bot[webhooks]?
Only for run_webhook. It installs tornado, the web server PTB uses. A polling bot needs the plain package; add [job-queue] if you use JobQueue.
Where does PicklePersistence keep its file on Livops?
Wherever you point it. Put it in /app/data, the DATA_DIR variable, which survives restarts and deploys. PTB writes it every 60 seconds by default and once more on a clean shutdown.
How do I see why the bot stopped?
The project's Logs page has what it printed. If it crashes ten times inside 15 minutes it is stopped, and you are notified with its last lines, in Telegram if your account is linked.
How much does it cost?
19 credits a month, 19,000 so'm, the same for polling and webhook. There is no free tier for bots; the first bot on an account that has not paid runs for 15 minutes on a zero balance.
Sources
- python-telegram-bot — Application (v22.8) (docs.python-telegram-bot.org). Checked .
- python-telegram-bot source — telegram/ext/_application.py (raw.githubusercontent.com). Checked .
- python-telegram-bot source — telegram/ext/_updater.py (raw.githubusercontent.com). Checked .
- python-telegram-bot — PicklePersistence (v22.8) (docs.python-telegram-bot.org). Checked .
- python-telegram-bot on PyPI (pypi.org). Checked .
- Telegram Bot API — Getting updates (core.telegram.org). Checked .
- python-telegram-bot wiki — Transition guide to version 20.0 (github.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your botRelated
- Telegram bot webhook vs polling: which one to usePolling means your bot asks Telegram for updates in a loop; a webhook means Telegram POSTs each update to your HTTPS address. Polling is simpler and needs no address; a webhook needs HTTPS but no always-running loop. A bot uses one or the other, never both. On Livops both run for the same 19 credits a month.
- aiogram bot hosting: how to deploy an aiogram 3 botAn aiogram 3 bot needs one process that never stops, the token in an environment variable, state kept outside memory and a clean exit on SIGTERM. On Livops you push a repository with bot.py and requirements.txt, and it runs for 19 credits a month (19,000 so'm), polling or webhook.
- Telegraf bot hosting: deploying a Telegraf 4 bot on Node.jsA Telegraf bot is one Node.js process: bot.launch() for polling, or bot.webhookCallback() behind an HTTP server for a webhook, with bot.stop() on SIGTERM. On Livops a repository with telegraf in package.json is run as a bot from every push, for 19 credits a month (19,000 so'm).
- grammY bot deploy: polling, webhooks and hostingDeploy a grammY bot as one always-on Node.js process: bot.start() for polling, or webhookCallback() behind an HTTP server, with bot.stop() on SIGTERM. On Livops a repository with grammy in package.json runs as a bot from every push, for 19 credits a month (19,000 so'm).