MrConsoleka/aiogram-bot-template
Template for creating scalable bots with aiogram
๐ Description
โ
Aiogram Bot Template โ This template helps you quickly bootstrap Telegram bots on the aiogram 3.x stack. It already includes a ready project structure, command and message handlers, optional PostgreSQL/Redis integration, logging with loguru, internationalization, support for aiogram-dialog, and dependency injection via dishka with optional webhook handling on FastAPI. The template is designed to remove routine setup and let you focus on your botโs logic.
โ
๐จ Functions
โ
/start- Start the bot/language- Change language/help- Help/admin- Command for administrators/dialog- Demo dialog usingaiogram-dialog/fsm- Demo finite state machine form
โ
๐๏ธ Template structure
โ
๐ aiogram_bot_template/
โโโโโ ๐ .github/
โ โโโโโ ๐ ISSUE_TEMPLATE/
โ โ โโโ bug_report.md
โ โ โโโ feature_request.md
โ โโโโโ ๐ workflows/
โ โโโ ci.yml
โ โโโ docker.yml
โ
โโโโโ ๐ docs/
โ โโโ getting-started.md
โ โโโ README.md
โ โโโโโ ๐ guides/
โ โโโโโ ๐ reference/
โ โโโโโ ๐ releases/
โ
โโโโโ ๐ migrations/
โ โโโ env.py
โ โโโโโ ๐ versions/
โ
โโโโโ ๐ scripts/
โ โโโ create_migration.sh
โ โโโ db_seed.py
โ โโโ health_check.py
โ
โโโโโ ๐ source/
โ โโโโโ ๐ config/
โ โ โโโ __init__.py
โ โ โโโ config_reader.py
โ โ
โ โโโโโ ๐ constants/
โ โ โโโ __init__.py
โ โ โโโ logging.py
โ โ โโโ throttling.py
โ โ
โ โโโโโ ๐ data/
โ โ โโโ __init__.py
โ โ โโโ ๐ error_logs/
โ โ โโโ ๐ full_logs/
โ โ
โ โโโโโ ๐ database/
โ โ โโโโโ ๐ core/
โ โ โ โโโ __init__.py
โ โ โ
โ โ โโโโโ ๐ models/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ user.py
โ โ โ
โ โ โโโโโ ๐ repositories/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ user.py
โ โ โ
โ โ โโโโโ ๐ specifications/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ user.py
โ โ โ
โ โ โโโโโ ๐ tools/
โ โ โ โโโ __init__.py
โ โ โ โโโ mixin.py
โ โ โ โโโ uow.py
โ โ โ
โ โ โโโ __init__.py
โ โ
โ โโโโโ ๐ domain/
โ โ โโโ __init__.py
โ โ โโโ events.py
โ โ โโโ exceptions.py
โ โ โโโ value_objects.py
โ โ
โ โโโโโ ๐ dto/
โ โ โโโ __init__.py
โ โ โโโ base.py
โ โ โโโ user.py
โ โ
โ โโโโโ ๐ enums/
โ โ โโโ __init__.py
โ โ โโโ roles.py
โ โ
โ โโโโโ ๐ factory/
โ โ โโโ __init__.py
โ โ โโโ bot.py
โ โ โโโ container.py
โ โ โโโ dispatcher.py
โ โ โโโ dishka.py
โ โ โโโ server.py
โ โ
โ โโโโโ ๐ infrastructure/
โ โ โโโ __init__.py
โ โ โโโโโ ๐ cache/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ redis.py
โ โ โ
โ โ โโโโโ ๐ monitoring/
โ โ โโโ __init__.py
โ โ โโโ prometheus.py
โ โ
โ โโโโโ ๐ locales/
โ โ โโโโโ ๐ en/
โ โ โ โโโ buttons.ftl
โ โ โ โโโ messages.ftl
โ โ โโโโโ ๐ ru/
โ โ โโโ buttons.ftl
โ โ โโโ messages.ftl
โ โ
โ โโโโโ ๐ services/
โ โ โโโ __init__.py
โ โ โโโ base.py
โ โ โโโ cache_service.py
โ โ โโโ user_service.py
โ โ
โ โโโโโ ๐ telegram/
โ โ โโโโโ ๐ filters/
โ โ โ โโโ __init__.py
โ โ โ โโโ admin.py
โ โ โ โโโ admin_protect.py
โ โ โ โโโ chat_type.py
โ โ โ โโโ validators.py
โ โ โ
โ โ โโโโโ ๐ handlers/
โ โ โ โโโโโ ๐ admin/
โ โ โ โ โโโ __init__.py
โ โ โ โ โโโ callbacks.py
โ โ โ โ โโโ commands.py
โ โ โ โ โโโ fsm.py
โ โ โ โ โโโ messages.py
โ โ โ โโโโโ ๐ errors/
โ โ โ โ โโโ __init__.py
โ โ โ โ โโโ common.py
โ โ โ โ โโโ orm.py
โ โ โ โ โโโ telegram.py
โ โ โ โโโโโ ๐ user/
โ โ โ โโโ __init__.py
โ โ โ โโโ callbacks.py
โ โ โ โโโ commands.py
โ โ โ โโโ fsm.py
โ โ โ โโโ messages.py
โ โ โ
โ โ โโโโโ ๐ keyboards/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ builder.py
โ โ โ โโโ callback_factory.py
โ โ โ โโโ inline.py
โ โ โ โโโ reply.py
โ โ โ
โ โ โโโโโ ๐ middlewares/
โ โ โ โโโ __init__.py
โ โ โ โโโ auth.py
โ โ โ โโโ base.py
โ โ โ โโโ reporting.py
โ โ โ โโโ throttling.py
โ โ โ
โ โ โโโโโ ๐ states/
โ โ โ โโโ __init__.py
โ โ โ โโโ dialog.py
โ โ โ โโโ form.py
โ โ โ
โ โ โโโโโ ๐ dialogs/
โ โ โโโ __init__.py
โ โ โโโ dialog.py
โ โ
โ โโโโโ ๐ utils/
โ โ โโโ __init__.py
โ โ โโโ logger.py
โ โ โโโ set_commands.py
โ โ โโโ translator.py
โ โ โโโ validators.py
โ โ
โ โโโ ๐ __main__.py
โ
โโโโโ ๐ tests/
โ โโโ __init__.py
โ โโโ conftest.py
โ โโโโโ ๐ e2e/
โ โโโโโ ๐ integration/
โ โโโโโ ๐ unit/
โ
โโโ alembic.ini
โโโ .env.example
โโโ .dockerignore
โโโ .gitignore
โโโ .pre-commit-config.yaml
โโโ docker-compose.dev.yml
โโโ docker-compose.yml
โโโ Dockerfile
โโโ LICENSE
โโโ Makefile
โโโ pyproject.toml
โโโ README.md
โโโ reorganize-docs.sh
โโโ SECURITY.md
โโโ uv.lock
โ
๐ About the content
โ
๐ source/- Main application source code.๐ source/config/- Application configuration settings.๐ source/constants/- Project constants.๐ source/data/- Data generated by the application (e.g., logs).๐ source/data/error_logs/- Log files containing only errors.๐ source/data/full_logs/- Full log files.๐ source/database/- Database interaction logic.๐ source/database/core/- Database core modules (connection, sessions).๐ source/database/models/- Database model definitions.๐ source/database/repositories/- Repositories for database data access.๐ source/database/specifications/- Query specifications.๐ source/database/tools/- Helper tools for working with the database.๐ source/domain/- Domain events and value objects.๐ source/dto/- Data transfer objects.๐ source/enums/- Enum definitions.๐ source/factory/- Factories for creating the bot, dispatcher, webhook server and DI container.๐ source/infrastructure/- External integrations (cache, monitoring).๐ source/locales/- Localization files (translations).๐ source/locales/en/- English language localization.๐ source/locales/ru/- Russian language localization.๐ source/services/- Business logic layer.๐ source/telegram/- Components related to Telegram andaiogram.๐ source/telegram/filters/- Customaiogramfilters.๐ source/telegram/handlers/- Handlers for processing Telegram updates.๐ source/telegram/handlers/admin/- Handlers for administrators.๐ source/telegram/handlers/errors/- Error handlers.๐ source/telegram/handlers/user/- Handlers for users.๐ source/telegram/keyboards/- Telegram keyboards.๐ source/telegram/middlewares/-aiogrammiddlewares (throttling, error reporting).๐ source/telegram/states/-aiogramFSM states.๐ source/telegram/dialogs/- Dialog windows built withaiogram-dialog.๐ source/utils/- Helper utilities (logger setup, commands, translations).๐ source/__main__.py- Main entry point within thesourcepackage.๐ .env.example- Example file for sensitive data (.env).๐ docs/- Development and deployment documentation.๐ scripts/- Helper scripts for development tasks.๐ tests/- Test suite.
โ
โ๏ธ Configuration
โ
Before running the bot, you need to set up your environment variables. Copy the .env.example file to .env and fill in your credentials and settings:
cp .env.example .env
# Then edit the .env file with your configurationsโ
๐ Bot .env Variables
โ
| Environment Variable Name | Description |
|---|---|
| ENVIRONMENT | Application environment (development, test, production). |
| TG__WEBHOOK_USE | Boolean value (True/False) indicating whether to use webhooks (True) or long polling (False). |
| TG__WEBHOOK_PATH | Path for Telegram to send webhook updates (appended to WEBHOOK__URL). |
| TG__BOT_TOKEN | Your Telegram bot token, obtained from @BotFather in Telegram. |
| TG__ADMIN_IDS | List of Telegram user IDs (JSON list or comma-separated) who will have administrator rights in the bot. |
| WEBHOOK__URL | Public URL where Telegram will send updates if webhooks are enabled. |
| WEBHOOK__HOST | Host or IP address where the webhook server will listen for incoming connections (usually 0.0.0.0). |
| WEBHOOK__PORT | Port on which the webhook server will listen for incoming connections. |
| WEBHOOK__PATH | Specific path on the server where Telegram will send POST requests with updates. |
| WEBHOOK__SECRET | Secret token that Telegram includes in webhook request headers to verify authenticity. |
| DB__HOST | Database server host. |
| DB__PORT | Port for connecting to the database. |
| DB__USER | Username for database authentication. |
| DB__PASSWORD | Password for database authentication. |
| DB__NAME | Name of the database to connect to. |
| REDIS__HOST | Redis server host used for FSM and/or caching. |
| REDIS__PORT | Port for connecting to the Redis server. |
| REDIS__USER | Username for Redis authentication (if used). |
| REDIS__PASSWORD | Password for Redis authentication (if used). |
| REDIS__DB | Redis database index to use (a number from 0 to 15, default is 0). |
โ
๐ป Bot Setup
โ
๐ฆ Using UV
โ
-
Clone the repository and navigate into the project directory:
git clone https://github.com/MrConsoleka/aiogram-bot-template.git cd aiogram-bot-template -
Ensure you have
uvinstalled. If not, you can install it, for example, usingpip:pip install uv
-
Create a virtual environment:
make venv
-
Activate the virtual environment:
# For Linux or macOS: source .venv/bin/activate # For Windows: .venv\Scripts\activate
-
Install dependencies:
make install
-
To run the bot, use the command:
make run
โ
๐ฆ Using Docker
โ
-
Clone the repository and navigate into the project directory:
git clone https://github.com/MrConsoleka/aiogram-bot-template.git cd aiogram-bot-template -
Build the Docker Image:
make docker-build
-
Run the Project with Docker Compose:
make docker-up
-
Verify Bot is Running (Optional):
make docker-logs
or
make docker-logs SERVICE=bot
-
Stop the Project:
make docker-down
โ
๐๏ธ Migrations
โ
Create and apply migrations with Alembic:
make migration MESSAGE="create users"
uv run alembic upgrade headโ
๐งช Testing
โ
Run tests locally:
uv run pytest tests/โ
๐งฐ Pre-commit
โ
pre-commit install
pre-commit run --all-filesโ
๐งฉ Development Services
โ
make dev-up
make dev-downโ
๐ Todo List
โ
- touch the grass
- Alembic
- Aiogram-dialog
- .github/workflows
โ
๐๏ธ Stack of Technologies
โ
- aiogram-3x - Asynchronous framework for the Telegram Bot API.
- aiogram-dialog - Dialog manager for building interactive flows.
- dishka - Dependency injection container.
- fastapi & uvicorn - Webhook server stack.
- pydantic & pydantic-settings - Data validation and configuration management.
- postgresql with sqlalchemy and asyncpg - Database layer.
- redis - In-memory data store for FSM and caching.
- loguru - Logging library.
- prometheus-client - Metrics exporter (optional).
- cachetools & fluentogram - Caching and localization helpers.
- Ruff, Mypy, Pre-commit, Isort, Black - Code quality and formatting tools.
โ
๐ผ Credits
โ
- aiogram_template - Inspired by Abdullah's project, many thanks to him <3
โ
๐ค Author of Aiogram Template Bot
โ
ยฉ Roman Alekseev
