Django Telegram Bot
Build your Telegram bots in a Django way.
Decorator routing · Messages and keyboards in the admin · Fully typed
Documentation · Getting started · Examples · Migration guide
https://github.com/user-attachments/assets/5307e4c1-4f2a-4c2f-94e7-b05d9fec2788
Why dj_telegram_bot?
The fastest way to build a production-ready Telegram bot on Django. You write plain functions, decorate them with a trigger, and the library does the routing. Your texts and keyboards live in the database, so you can change them from the admin without a redeploy.
from dj_telegram_bot.core.guards import staff_required
from dj_telegram_bot.core.handlers import callback_handler, command_handler
@command_handler("start")
def start(ctx):
ctx.bot.send_message(ctx.chat_id, "Hello!")
@callback_handler("buy_*")
def buy(ctx):
item = ctx.callback_data.removeprefix("buy_")
ctx.bot.answer_callback_query(ctx.callback_query.id, text=f"Added {item}")
@command_handler("stats")
@staff_required
def stats(ctx):
ctx.bot.send_message(ctx.chat_id, "Staff only!")
Features
| 🧭 Decorator routing | @command_handler, @message_handler, @callback_handler, @inline_handler, @chosen_handler, plus step handlers and * wildcards |
| 🧩 Function or class handlers | Use whichever style you like. Classes inherit from UpdateHandler, and everything is imported from dj_telegram_bot.core.handlers |
| ⌨️ Edit from the admin | Build messages and keyboards visually in the Django admin. No redeploy |
| 🛡️ Built-in guards | @sponsor_required, @staff_required, @superuser_required, and @before_handler for your own checks |
| 👤 Optional user model | Let the bot create users, bring your own model, or use none at all |
| 🔒 Fully typed | The whole Telegram Bot API is modeled with Pydantic, with autocomplete everywhere |
| 🛠️ Maintenance mode | Pause the bot and show a custom message, from the admin or from code |
| 🌐 Proxy and local server | SOCKS5 proxy and local Bot API server (TDLib) support |
Install
pip install dj_telegram_bot
or with uv:
uv add dj_telegram_bot
Upgrading from 0.1.x? Read the Migration guide.
Quick start
1. Settings
# settings.py
INSTALLED_APPS = [..., "dj_telegram_bot.contrib"]
BOT_TOKEN = "your-telegram-bot-token"
TM_WEBHOOK_URL = "https://api.telegram.org" # or your local Bot API server
2. URLs and migrations
# urls.py
urlpatterns = [..., path("telegram/", include("dj_telegram_bot.contrib.urls"))]
python manage.py migrate
3. Set the webhook (once)
curl "https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=https://yourdomain.com/telegram/webhook/"
4. Write your first handler
# your_app/bot_handlers/start.py (auto-discovered)
from dj_telegram_bot.core.handlers import command_handler
@command_handler("start")
def start(ctx):
ctx.bot.send_message(ctx.chat_id, "Hello!")
Open your bot in Telegram and send /start. It answers Hello! 🎉
How it works
Telegram ──webhook──▶ Dispatcher ──▶ before handlers ──▶ Router ──▶ your function
Every update is wrapped in a handler object, passes through your @before_handler checks, and the router picks the first match: exact trigger, then the user's step, then wildcards.
Documentation
| I want to... | Read |
|---|---|
| reply to commands, messages and buttons | Handlers |
| copy a working example | Examples |
| build a conversation | Steps and User model |
| limit a command or require joining a channel | Access control |
| block users or pause the bot | Before handlers |
| edit texts and keyboards without redeploying | Messages & keyboards |
| upgrade from 0.1.x | Migration guide |
Full guide: https://zankoan.github.io/dj_telegram_bot/
Contributing
Contributions are welcome! Found a bug, have an idea, or spotted a typo in the docs? Open an issue or send a pull request.
Please keep pull requests small and focused
License
Metadata
Release files for dj-telegram-bot 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dj_telegram_bot-0.2.0.tar.gz | 130.6 kB | Without provenance | Details for dj_telegram_bot-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dj_telegram_bot-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for dj_telegram_bot-0.2.0-py3-none-any.whl |
Total release size: 205.6 kB
Release files / dj_telegram_bot-0.2.0.tar.gz
| Download URL | dj_telegram_bot-0.2.0.tar.gz |
|---|---|
| Size | 130.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64d309a65ea921ebf214937f69302adfd53ac1a771aae7b148146d3d6aa4ca33
|
|
BLAKE2b-256 checksum How to use checksums |
ee44c69b5ff29337432e0c5c0761314321a2d1531eb331a23e3b7cef2a82adda
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.11
|
Release files / dj_telegram_bot-0.2.0-py3-none-any.whl
| Download URL | dj_telegram_bot-0.2.0-py3-none-any.whl |
|---|---|
| Size | 75.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3e5e4fb6dcb3fe9e327d6d3d0f59d21d1dd3cac51a6d19aa50debd3e1b3a8ffd
|
|
BLAKE2b-256 checksum How to use checksums |
4e11a055e60b5b5bf9aa66b212d7144dbf408c7550610d0d3286fa951f0add75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.11
|
Security for dj-telegram-bot 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.