Build your Telegram bots
in a Django way.¶
Decorator routing. Edit messages and keyboards from the admin. Fully typed.
from dj_telegram_bot.core.handlers import command_handler
@command_handler("start")
def start(ctx):
ctx.bot.send_message(ctx.chat_id, "Hello!")
Start here¶
New to the library? Read these three pages in order. Everything else can wait until you need it.
- Getting started: install, connect the webhook and answer
/start. - Handlers: reply to commands, messages and buttons.
- Examples: copy a working example for what you are building.
What's inside¶
-
Decorator routing
Commands, messages, callbacks, inline queries, steps and
*wildcards. Write functions or classes. -
Built-in guards
@staff_required,@superuser_required,@sponsor_required(make users join your channels first), and@before_handlerfor your own checks. -
Edit from the admin
Messages and keyboards live in the database. Build them visually, change them without a redeploy.
-
Fully typed
Every Telegram type is a Pydantic model. Autocomplete and type safety everywhere.
-
Maintenance mode
Pause the bot and show a custom message, from the admin or from code.
-
Optional user model
Let the bot create users for you, bring your own model, or use none at all.
-
Proxy and local server
SOCKS5 proxy and local Bot API server (TDLib) support.
How it works¶
Telegram ──webhook──▶ TelegramWebhookView
│
Dispatcher picks a handler class by update type
│
before handlers your checks, run on every update
│
Router.find(...) exact → step → wildcard
│
your function or method
- Telegram POSTs an update to
/telegram/webhook/. - The
Dispatcherwraps it in a handler object for its update type (command, message, callback, inline query or chosen inline result). - Every
@before_handlerruns. If one returnsTrue, processing stops. - The
Routerlooks for a matching route and calls it.