https://github.com/mrconsoleka/aiogram-bot-template
Template for creating scalable bots with aiogram
https://github.com/mrconsoleka/aiogram-bot-template
aiogram aiogram-i18n aiogram3 aiohttp asyncpg docker loguru makefile postgresql pyproject python python3 ruff template uv
Last synced: 4 months ago
JSON representation
Template for creating scalable bots with aiogram
- Host: GitHub
- URL: https://github.com/mrconsoleka/aiogram-bot-template
- Owner: MrConsoleka
- License: mit
- Created: 2025-04-28T13:13:00.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2025-04-29T06:42:21.000Z (12 months ago)
- Last Synced: 2025-04-29T19:53:44.452Z (12 months ago)
- Topics: aiogram, aiogram-i18n, aiogram3, aiohttp, asyncpg, docker, loguru, makefile, postgresql, pyproject, python, python3, ruff, template, uv
- Language: Python
- Homepage:
- Size: 88.9 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
README

Aiogram Bot Template

## ๐ 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 using `aiogram-dialog`
* `/fsm` - Demo finite state machine form
โ
## ๐๏ธ Template structure
โ
```
๐ aiogram_bot_template/
โโโโโ ๐ .github/
โ โโโโโ ๐ ISSUE_TEMPLATE/
โ โโโ bug_report.md
โ โโโ feature_request.md
โ
โโโโโ ๐ source/
โ โโโโโ ๐ config/
โ โ โโโ __init__.py
โ โ โโโ config_reader.py
โ โ
โ โโโโโ ๐ constants/
โ โ โโโ __init__.py
โ โ โโโ throttling.py
โ โ โโโ logging.py
โ โ
โ โโโโโ ๐ data/
โ โ โโโ __init__.py
โ โ โโโ ๐ error_logs/
โ โ โโโ ๐ full_logs/
โ โ
โ โโโโโ ๐ database/
โ โ โโโโโ ๐ core/
โ โ โ โโโ __init__.py
โ โ โ โโโ manager.py
โ โ โ
โ โ โโโโโ ๐ models/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ user.py
โ โ โ
โ โ โโโโโ ๐ repositories/
โ โ โ โโโ __init__.py
โ โ โ โโโ base.py
โ โ โ โโโ user.py
โ โ โ
โ โ โโโโโ ๐ tools/
โ โ โ โโโ __init__.py
โ โ โ โโโ mixin.py
โ โ โ โโโ uow.py
โ โ โ
โ โ โโโ __init__.py
โ โ
โ โโโโโ ๐ enums/
โ โ โโโ __init__.py
โ โ โโโ roles.py
โ โ
โ โโโโโ ๐ factory/
โ โ โโโ __init__.py
โ โ โโโ bot.py
โ โ โโโ dispatcher.py
โ โ โโโ dishka.py
โ โ โโโ server.py
โ โ
โ โโโโโ ๐ locales/
โ โ โโโโโ ๐ en/
โ โ โ โโโ buttons.ftl
โ โ โ โโโ messages.ftl
โ โ โโโโโ ๐ ru/
โ โ โโโ buttons.ftl
โ โ โโโ messages.ftl
โ โ
โ โโโโโ ๐ services/
โ โ โโโ __init__.py
โ โ โโโ user_service.py
โ โ
โ โโโโโ ๐ telegram/
โ โ โโโโโ ๐ filters/
โ โ โ โโโ __init__.py
โ โ โ โโโ admin_protect.py
โ โ โ โโโ chat_type.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
โ โ โ โโโ builder.py
โ โ โ โโโ callback_factory.py
โ โ โ โโโ inline.py
โ โ โ โโโ reply.py
โ โ โ
โ โ โโโโโ ๐ middlewares/
โ โ โ โโโ __init__.py
โ โ โ โโโ callback_throttling.py
โ โ โ โโโ message_throttling.py
โ โ โ โโโ reporting.py
โ โ โ
โ โ โโโโโ ๐ states/
โ โ โ โโโ __init__.py
โ โ โ โโโ dialog.py
โ โ โ โโโ form.py
โ โ โ
โ โ โโโโโ ๐ dialogs/
โ โ โโโ __init__.py
โ โ โโโ dialog.py
โ โ
โ โโโโโ ๐ utils/
โ โ โโโ __init__.py
โ โ โโโ logger.py
โ โ โโโ set_commands.py
โ โ โโโ translator.py
โ โ
โ โโโ ๐ __main__.py
โ
โโโ .env.example
โโโ .dockerignore
โโโ .gitignore
โโโ Dockerfile
โโโ LICENSE
โโโ Makefile
โโโ SECURITY.md
โโโ docker-compose.yml
โโโ pyproject.toml
โโโ README.md
```
โ
## ๐ 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 and `aiogram`.
* `๐ source/telegram/filters/` - Custom `aiogram` filters.
* `๐ 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/` - `aiogram` middlewares (throttling, error reporting).
* `๐ source/telegram/states/` - `aiogram` FSM states.
* `๐ source/telegram/dialogs/` - Dialog windows built with `aiogram-dialog`.
* `๐ source/utils/` - Helper utilities (logger setup, commands, translations).
* `๐ source/__main__.py` - Main entry point within the `source` package.
* `๐ .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:
```shell
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
โ
1. Clone the repository and navigate into the project directory:
```shell
git clone https://github.com/MrConsoleka/aiogram-bot-template.git
cd aiogram-bot-template
```
2. Ensure you have `uv` installed. If not, you can install it, for example, using `pip`:
```shell
pip install uv
```
3. Create a virtual environment:
```shell
make venv
```
4. Activate the virtual environment:
```shell
# For Linux or macOS:
source .venv/bin/activate
# For Windows:
.venv\Scripts\activate
```
5. Install dependencies:
```shell
make install
```
6. To run the bot, use the command:
```shell
make run
```
โ
### ๐ฆ Using Docker
โ
1. Clone the repository and navigate into the project directory:
```shell
git clone https://github.com/MrConsoleka/aiogram-bot-template.git
cd aiogram-bot-template
```
2. Build the Docker Image:
```shell
make docker-build
```
3. Run the Project with Docker Compose:
```shell
make docker-up
```
4. Verify Bot is Running (Optional):
```shell
make docker-logs
```
or
```shell
make docker-logs SERVICE=bot
```
5. Stop the Project:
```shell
make docker-down
```
โ
## ๐๏ธ Migrations
โ
Create and apply migrations with Alembic:
```shell
make migration MESSAGE="create users"
uv run alembic upgrade head
```
โ
## ๐งช Testing
โ
Run tests locally:
```shell
uv run pytest tests/
```
โ
## ๐งฐ Pre-commit
โ
```shell
pre-commit install
pre-commit run --all-files
```
โ
## ๐งฉ Development Services
โ
```shell
make dev-up
make dev-down
```
โ
## ๐ Todo List
โ
- [x] touch the grass
- [x] Alembic
- [x] Aiogram-dialog
- [x] .github/workflows
โ
## ๐๏ธ Stack of Technologies
โ
* [aiogram-3x](https://github.com/aiogram/aiogram) - Asynchronous framework for the Telegram Bot API.
* [aiogram-dialog](https://github.com/aiogram/aiogram-dialog) - Dialog manager for building interactive flows.
* [dishka](https://github.com/arslnk/dishka) - Dependency injection container.
* [fastapi](https://github.com/tiangolo/fastapi) & [uvicorn](https://github.com/encode/uvicorn) - Webhook server stack.
* [pydantic](https://github.com/pydantic/pydantic) & [pydantic-settings](https://github.com/pydantic/pydantic-settings) - Data validation and configuration management.
* [postgresql](https://github.com/postgres/postgres) with [sqlalchemy](https://github.com/sqlalchemy/sqlalchemy) and [asyncpg](https://github.com/MagicStack/asyncpg?tab=readme-ov-file) - Database layer.
* [redis](https://redis.io/) - In-memory data store for FSM and caching.
* [loguru](https://github.com/Delgan/loguru) - Logging library.
* [prometheus-client](https://github.com/prometheus/client_python) - Metrics exporter (optional).
* [cachetools](https://github.com/tkem/cachetools) & [fluentogram](https://github.com/Arustinal/fluentogram) - Caching and localization helpers.
* [Ruff](https://github.com/astral-sh/ruff), [Mypy](https://github.com/python/mypy), [Pre-commit](https://github.com/pre-commit/pre-commit), [Isort](https://github.com/pycqa/isort), [Black](https://github.com/psf/black) - Code quality and formatting tools.
โ
## ๐ผ Credits
โ
- [aiogram_template](https://github.com/Lems0n/aiogram_template) - Inspired by Abdullah's project, many thanks to him <3
โ
## ๐ค Author of Aiogram Template Bot
โ
**ยฉ Roman Alekseev**