# 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).

Source: https://livops.dev/en/guides/python-telegram-bot-hosting

By Livops team, the people who build and run Livops. Updated 19 November 2026.

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.

```text
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

```python
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:

- `listen` is `127.0.0.1`[1], which nothing outside the container can reach. Use `0.0.0.0`.
- With `secret_token` set, a request without the header, or with the wrong one, is answered 403 Forbidden[1].
- It **always calls `set_webhook`**. With no `webhook_url`, it builds one from `listen`, `port` and `url_path`[3] — `http://0.0.0.0:…`, which is no address Telegram can use. Always pass `webhook_url`.

```python
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"],
)
```

```text
python-telegram-bot[webhooks,job-queue]>=22,<23
```

## Deploy a python-telegram-bot bot on Livops

1. Push a repository with `bot.py` and `requirements.txt`, or upload the folder as a `.zip`, and create a project from it in the cabinet.
2. Under **Settings → Variables**, add `BOT_TOKEN` and mark it as a secret: its value is never shown again.
3. 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`:

```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 `PORT` within 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 means `python-telegram-bot` is missing from `requirements.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](https://livops.dev/en/docs/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

1. [python-telegram-bot — Application (v22.8)](https://docs.python-telegram-bot.org/en/stable/telegram.ext.application.html). Checked 19 November 2026.
2. [python-telegram-bot source — telegram/ext/_application.py](https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/src/telegram/ext/_application.py). Checked 19 November 2026.
3. [python-telegram-bot source — telegram/ext/_updater.py](https://raw.githubusercontent.com/python-telegram-bot/python-telegram-bot/master/src/telegram/ext/_updater.py). Checked 19 November 2026.
4. [python-telegram-bot — PicklePersistence (v22.8)](https://docs.python-telegram-bot.org/en/stable/telegram.ext.picklepersistence.html). Checked 19 November 2026.
5. [python-telegram-bot on PyPI](https://pypi.org/project/python-telegram-bot/). Checked 19 November 2026.
6. [Telegram Bot API — Getting updates](https://core.telegram.org/bots/api). Checked 19 November 2026.
7. [python-telegram-bot wiki — Transition guide to version 20.0](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Transition-guide-to-Version-20.0). Checked 19 November 2026.
