An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

image

Aiogram Bot Template

Static Badge
Static Badge
Static Badge


Static Badge

## ๐Ÿ“Œ 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**