GitHunt
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
โ”‚   โ””โ”€โ”€โ”€โ” ๐Ÿ“‚ 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 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:

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:

     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:

    pip install uv
  3. Create a virtual environment:

    make venv
  4. Activate the virtual environment:

    # For Linux or macOS:
    source .venv/bin/activate
    
    # For Windows:
    .venv\Scripts\activate
  5. Install dependencies:

    make install
  6. To run the bot, use the command:

    make run

โ €

๐Ÿ“ฆ Using Docker

โ €

  1. Clone the repository and navigate into the project directory:

    git clone https://github.com/MrConsoleka/aiogram-bot-template.git
    cd aiogram-bot-template
  2. Build the Docker Image:

    make docker-build
  3. Run the Project with Docker Compose:

    make docker-up
  4. Verify Bot is Running (Optional):

    make docker-logs

    or

    make docker-logs SERVICE=bot
  5. 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

โ €

โ €

๐Ÿ’ผ Credits

โ €

โ €

๐Ÿ‘ค Author of Aiogram Template Bot

โ €
ยฉ Roman Alekseev

MrConsoleka/aiogram-bot-template | GitHunt