A template for rapidly building scalable Telegram bots using aiogram. Designed for developers who want to streamline project setup with a modular structure, database support, caching, and a basic admin panel.
- Description
- Features
- Installation
- Configuration
- Security
- Project Structure
- Usage
- Contributing
- License
- Contact
aiogram-bot-template is a ready-to-use template for developing Telegram bots with Python and the aiogram library. It offers a modular architecture, automated connections to PostgreSQL and Redis, a built-in admin panel, filters, Pydantic-based validation, and CI/CD support. The template is ideal for building scalable bots, from simple prototypes to complex enterprise solutions.
This template simplifies environment setup, reduces boilerplate code, and provides out-of-the-box tools for database management, caching, and logging.
- π Modular Structure: Easily extensible with separated handlers, services, and DAO.
- ποΈ Database: PostgreSQL support with base and custom DAO methods, plus Alembic migrations.
- β‘ Caching: Redis integration for storing user data (telegram_id, language_code, is_admin, is_banned).
- π‘οΈ Admin Panel: Basic panel displaying user statistics (total, new today/weekly/monthly).
- π« Filters: Implemented
is_adminandis_bannedfilters for access control. - π Logging: Structured JSON logging to console and file with rotation, configurable via .env. Details in Logging Documentation.
- β Validation: Pydantic for configuration and data validation.
- π οΈ CI/CD: Linting via GitHub Actions and automated Docker image builds on GHCR.
- π³ Docker: Full Docker and Docker Compose support with configs for Poetry and pip.
- π Scalability: Suitable for large-scale bots thanks to services, caching, and modularity.
- Clone the repository:
git clone https://github.com/vlymar-dev/aiogram-bot-template.git cd aiogram-bot-template - Install dependencies:
Or, if using Poetry:
pip install -r requirements.txt
poetry install
- Configure the
.envfile (see Configuration). - Apply database migrations:
make migration-apply
- Run the bot:
make run-main
- Clone the repository (see above).
- Configure
.env. - Start services:
Or start without rebuilding:
make services-build
make services-up
- For local development with DB and Redis:
make local-up
The project includes a Makefile with handy commands:
make lintβ Run code quality checks (ruff, pre-commit).make migration-create MESSAGE="description"β Create a new migration.make services-downβ Stop services.make storages-clearβ Remove containers and volumes.make helpβ List all commands.
Create a .env file in the project root based on .env.example:
cp .env.example .envEdit .env to configure the bot, database, Redis, and logging. See the example configuration:
View π .env.example
Production Security Policy: Do not expose PostgreSQL (port 5432) or Redis (port 6379) publicly. Use an SSH tunnel or Docker's internal network for access.
aiogram-bot-template/
βββ Dockerfile # Dockerfile
βββ LICENSE # MIT License
βββ Makefile # Convenient commands
βββ README.md # Documentation
βββ alembic/ # Database migrations
βββ docker_compose/ # Docker Compose configurations
βββ docs/ # Additional documentation
βββ src/
β βββ bot/ # Bot logic
β β βββ bot.py # Entry point
β β βββ filters/ # Filters (admin, banned)
β β βββ handlers/ # Command handlers
β β βββ keyboards/ # Keyboards
β β βββ middlewares/ # Middleware for DB and services
β β βββ utils/ # Utilities
β βββ config.py # Pydantic-based configuration
β βββ infrastructure/ # Infrastructure (DB, cache, services)
β βββ logging_config.py # Logging setup
βββ tests/ # Tests
βββ .env.example # Configuration example
βββ poetry.lock # Poetry dependencies
βββ pyproject.toml # Poetry config
βββ requirements.txt # pip dependencies
Once launched, the bot automatically registers users upon the /start command. The main menu, accessible via an inline keyboard, provides the following features:
- π‘οΈ Admin Panel: Displays user statistics (total, new today/weekly/monthly). Accessible to users with
is_admin=Trueor Telegram IDs listed inBOT_ADMIN_ACCESS_IDsin the.envfile. - π€ Profile: Shows cached user data (telegram_id, language_code, is_admin, is_banned) when the "My Profile" button is clicked.
- π Share Bot: Allows sharing the bot link via
switch_inline_queryby clicking the "Share Bot" button.
To access the main menu, send the /start command and interact with the inline keyboard.
We welcome contributions! Want to add new handlers, improve documentation, or fix a bug? Here's how:
- Fork the repository.
- Create a branch (
git checkout -b feature/awesome-feature). - Make changes and commit (
git commit -m "feat: Add awesome feature"). - Push the branch (
git push origin feature/awesome-feature). - Open a Pull Request.
This project is licensed under the MIT License.
Reach out via GitHub or open an issue in the repository.