Skip to main content

django-aiogram

PyPI Python CI License

Run aiogram next to Django: write handlers as ordinary Django app code, and send Telegram messages from anywhere in the project.

One container runs the bot. Every other process — web, Celery, a management command — hands the call to a broker and returns, so a request never waits on Telegram. Four transports can carry it, and BROKER says which:

                              ┌─ Redis list ────┐
  web, celery ──bot.send()──▶ ├─ Redis Streams ─┤ ──▶ start_tgbot ──▶ Telegram
                              ├─ RabbitMQ ──────┤
                              └─ Kafka ─────────┘
transport BROKER extra its own required settings
Redis list (default) django_aiogram.broker.redis_list.RedisListBroker [redis]
Redis Streams django_aiogram.broker.redis_streams.RedisStreamsBroker [redis] REDIS_STREAM_KEY
RabbitMQ django_aiogram.broker.rabbitmq.RabbitMQBroker [rabbitmq] RABBITMQ_URL, RABBITMQ_QUEUE
Kafka django_aiogram.broker.kafka.KafkaBroker [kafka] KAFKA_BOOTSTRAP, KAFKA_TOPIC

Your code does not change with the row: the same bot.send(), handlers, event log and manage.py start_tgbot. What differs is what becomes of a message whose worker was killed mid-send, and what recovery is — a command, a clock, or the broker's own doing. Delivery compares them; each transport has a page of its own below.

Install

pip install 'django-aiogram[redis]'                 # Redis list, the default, or Redis Streams
pip install 'django-aiogram[rabbitmq,redis]'        # RabbitMQ
pip install 'django-aiogram[kafka,redis]'           # Kafka

One extra per transport, so a deployment downloads only the queue driver it uses. redis is in the other two lines for the FSM store, not the queue: FSM_STORAGE defaults to aiogram's Redis store, so a bot keeping chat state needs that driver whichever transport carries its messages, and FSM_STORAGE: 'memory' is what drops it.

Nothing is inferred from what happens to be installed: BROKER names the transport, and a base pip install django-aiogram imports and runs manage.py but carries no message. manage.py check names every extra that is missing, with the pip install line; Installation has the exceptions.

# settings.py
import os

INSTALLED_APPS = [..., 'django_aiogram']

TELEGRAM_BOT = {
    'TOKEN': os.environ.get('TELEGRAM_BOT_TOKEN', ''),
    # unset, BROKER resolves to RedisListBroker; the table above has the other three,
    # and each transport reads its own settings on top of these two
    'REDIS_URL': os.environ.get('REDIS_URL', ''),
}

Both may be empty: nothing connects or validates credentials at import time, so tests and migrations run without them. Requires Python 3.10–3.14, Django 5.2+ and aiogram 3.30+; each transport then pins its own driver and asks for its own server — Installation has both, per row.

Use it

# myapp/tg_router.py — imported automatically from every installed app
from aiogram import F, types

from django_aiogram import bot


@bot.message(F.text == '/start')
async def start(message: types.Message) -> None:
    await message.answer('hi')
# anywhere else in the project
from django_aiogram import bot

bot.send(chat_id=CHAT_ID, text='Order approved')
python manage.py start_tgbot

A router module, a call, and one process running the bot. That process gets Django's between-requests connection handling without having any requests — every update is bracketed with close_old_connections(), so a database that restarts under a long-running bot does not leave every handler raising InterfaceError until somebody notices. Nothing to configure; Deployment says what the healthcheck can and cannot see about it.

Everything else — rate limits, per-process opt-out, healthchecks — is configuration, documented rather than required. Webhook mode is the one alternative that also asks for a URL route: Webhook has the four steps.

Documentation

The wiki is the documentation. Pages live in docs/wiki/, so they are reviewed in the same pull request as the code they describe and published from master.

Installation install, configure, run
Settings every setting, with defaults and check ids
Handlers routers, filters, FSM, the async ORM
Sending messages routes, keyboards, files, errors
Testing your suite without a broker, asserting what was queued
API the instance, its internals, and what stays public
Delivery how queued messages reach Telegram
Redis list the default transport: what it guarantees, and why the worker's name matters
Redis Streams the same server, a consumer group, and no worker identity to keep
RabbitMQ a broker that tracks its own consumers, and one thread per connection
Kafka offsets settle a prefix, ordering is per partition, a refusal rewinds
Webhook receiving updates over HTTP instead of polling
Rate limits staying inside Telegram's published limits
Deployment compose recipes, healthchecks, per-process opt-out
Logging the logger and its structured fields
Event log recording what the bot did to a table, and a signal to count it without one
Serialization what can be queued
Troubleshooting symptoms and their usual causes
Upgrading what each major release changed, and what you must do
AI assistants the brief to hand a coding agent

Upgrading from 3.x: the distribution is django-aiogram and the import path is django_aiogram, the driver is an extra you now name, and the event log has a table of its own. The upgrading page walks it in order, migrate included.

Contributing

CONTRIBUTING.md for the workflow, AGENTS.md for the same ground in the form coding agents read. Changes are in CHANGELOG.md; security reports go through SECURITY.md.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_aiogram-4.0.0.tar.gz (696.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_aiogram-4.0.0-py3-none-any.whl (268.8 kB view details)

Uploaded Python 3

File details

Details for the file django_aiogram-4.0.0.tar.gz.

File metadata

  • Download URL: django_aiogram-4.0.0.tar.gz
  • Upload date:
  • Size: 696.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_aiogram-4.0.0.tar.gz
Algorithm Hash digest
SHA256 f150b2901152ac2592b9430247a0bae4a822d26b2e532c2b24326cf6724713e4
MD5 e57db97838532b88c0c84eac15adfc7a
BLAKE2b-256 72774634c031b275f84283e8d0b4e0c577ab4125694a3bc3501497eb7f349010

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_aiogram-4.0.0.tar.gz:

Publisher: publish.yml on CorneiZeR/django-aiogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_aiogram-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_aiogram-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 268.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for django_aiogram-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7880d5bbe7d96e746724688c27e33acb44f4fc20a3b681cce5b3e8985fa8248
MD5 c8e5e7de6a19883991d7bd7b8f3ecde3
BLAKE2b-256 1a83d27a9f6aa588919dc83bc8dcd658c9daec882f37b9bd7c3ae5caa7261b58

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_aiogram-4.0.0-py3-none-any.whl:

Publisher: publish.yml on CorneiZeR/django-aiogram

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

4.1.0

2 files

4.0.0.post1

2 files

This release

4.0.0 This release

2 files

4.0.0.dev0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page