This is a template for telegram bots written in python using the aiogram framework
You can learn how to develop telegram bots using the aiogram framework in the following courses (in Russian):
- Python 3.12;
- aiogram 3.x (Asynchronous Telegram Bot framework);
- aiogram_dialog (GUI framework for telegram bot);
- dynaconf (Configuration Management for Python);
- taskiq (Async Distributed Task Manager);
- fluentogram (Internationalization tool in the Fluent paradigm);
- Docker and Docker Compose (containerization);
- PostgreSQL (database);
- NATS (queue and FSM storage);
- Redis (cache, taskiq result backend);
- Alembic (database migrations with raw SQL).
📁 aiogram_bot_template/
├── 📁 alembic/
│ ├── 📁 versinos/
│ │ ├── 1541bb8a3f26_.py
│ │ └── b20e5643d3bd_.py
│ ├── env.py
│ └── script.py.mako
├── 📁 app/
│ ├── 📁 bot/
│ │ ├── 📁 dialogs/
│ │ │ ├── 📁 flows/
│ │ │ │ ├── 📁 settings/
│ │ │ │ │ ├── dialogs.py
│ │ │ │ │ ├── getters.py
│ │ │ │ │ ├── handlers.py
│ │ │ │ │ ├── keyboards.py
│ │ │ │ │ └── states.py
│ │ │ │ ├── 📁 start/
│ │ │ │ │ ├── dialogs.py
│ │ │ │ │ ├── getters.py
│ │ │ │ │ ├── handlers.py
│ │ │ │ │ └── states.py
│ │ │ │ └── __init__.py
│ │ │ └── 📁 widgets/
│ │ │ └── i18n.py
│ │ ├── 📁 enums/
│ │ │ ├── actions.py
│ │ │ └── roles.py
│ │ ├── 📁 filters/
│ │ │ └── dialog_filters.py
│ │ ├── 📁 handlers/
│ │ │ ├── __init__.py
│ │ │ ├── commands.py
│ │ │ └── errors.py
│ │ ├── 📁 i18n/
│ │ │ └── translator_hub.py
│ │ ├── 📁 keyboards/
│ │ │ ├── links_kb.py
│ │ │ └── menu_button.py
│ │ ├── 📁 middlewares/
│ │ │ ├── database.py
│ │ │ ├── get_user.py
│ │ │ ├── i18n.py
│ │ │ └── shadow_ban.py
│ │ ├── 📁 states/
│ │ │ └── states.py
│ │ ├── __init__.py
│ │ └── bot.py
│ ├── 📁 infrastructure/
│ │ ├── 📁 cache/
│ │ │ └── connect_to_redis.py
│ │ ├── 📁 database/
│ │ │ ├── 📁 connection/
│ │ │ │ ├── base.py
│ │ │ │ ├── connect_to_pg.py
│ │ │ │ └── psycopg_connection.py
│ │ │ ├── 📁 models/
│ │ │ │ └── users.py
│ │ │ ├── 📁 query/
│ │ │ │ └── results.py
│ │ │ ├── 📁 tables/
│ │ │ │ ├── 📁 enums/
│ │ │ │ │ ├── base.py
│ │ │ │ │ └── users.py
│ │ │ │ ├── base.py
│ │ │ │ └── users.py
│ │ │ ├── 📁 views/
│ │ │ │ └── views.py
│ │ │ └── db.py
│ │ └── 📁 storage/
│ │ ├── 📁 storage/
│ │ │ └── nats_storage.py
│ │ └── nats_connect.py
│ └── 📁 services/
│ ├── 📁 delay_service/
│ │ ├── 📁 models/
│ │ │ └── delayed_messages.py
│ │ ├── consumer.py
│ │ ├── publisher.py
│ │ └── start_consumer.py
│ └── 📁 scheduler/
│ ├── taskiq_broker.py
│ └── tasks.py
├── 📁 config/
│ ├── config.py
│ └── settings.toml
├── 📁 locales/
│ ├── 📁 en/
│ │ ├── 📁 LC_MESSAGES/
│ │ │ └── txt.ftl
│ │ └── 📁 static/
│ └── 📁 ru/
│ ├── 📁 LC_MESSAGES/
│ │ └── txt.ftl
│ └── 📁 static/
├── 📁 nats_broker/
│ ├── 📁 config/
│ │ └── server.conf
│ └── 📁 migrations/
│ └── create_stream.py
├── .env
├── .env.example
├── .gitignore
├── alembic.ini
├── docker-compose.example
├── docker-compose.yml
├── main.py
├── pyproject.toml
├── README.md
└── uv.lock
- Clone the repository to your local machine via HTTPS:
git clone https://github.com/kmsint/aiogram_bot_template.gitor via SSH:
git clone [email protected]:kmsint/aiogram_bot_template.git-
Create a
docker-compose.ymlfile in the root of the project and copy the code from thedocker-compose.examplefile into it. -
Create a
.envfile in the root of the project and copy the code from the.env.examplefile into it. Replace the required secrets (BOT_TOKEN, ADMINS_CHAT, etc). -
Run
docker-compose.ymlwithdocker compose upcommand. You need docker and docker-compose installed on your local machine. -
Create a virtual environment in the project root and activate it.
-
Install the required libraries in the virtual environment. With
pip:
pip install .or if you use poetry:
poetry install --no-root-
Write SQL code in the
upgradeanddowngradefunctions to create a database schema. See example in filealembic/versions/1541bb8a3f26_.py. -
If required, create additional empty migrations with the command:
alembic revisionand fill them with SQL code.
- Apply database migrations using the command:
alembic upgrade head- Run
create_stream.pyto create NATS stream for delayed messages service:
python3 -m nats_broker.migrations.create_stream- If you want to use the Taskiq broker for background tasks as well as the Taskiq scheduler, add your tasks to the
tasks.pymodule and start the worker first:
taskiq worker app.services.scheduler.taskiq_broker:broker -fsdand then the scheduler:
taskiq scheduler app.services.scheduler.taskiq_broker:scheduler-
Run
main.pyto check the functionality of the template. -
You can fill the template with the functionality you need.
For convenient interaction with nats-server you need to install nats cli tool. For macOS you can do this through the homebrew package manager. Run the commands:
brew tap nats-io/nats-tools
brew install nats-io/nats-tools/natsFor linux:
curl -sf https://binaries.nats.dev/nats-io/natscli/nats@latest | sh
sudo mv nats /usr/local/bin/After this you can check the NATS version with the command:
nats --version- Add mailing service
- Set up a CICD pipeline using Docker and GitHub Actions