A modern, modular, and elegant boilerplate for building Telegram bots with aiogram.
Telegram Bot Template is a flexible boilerplate for creating Telegram bots in Python using aiogram. It features a modular architecture, support for SQLite and MySQL databases, Redis for caching, an internationalization (i18n) system, and anti-spam protection. The project is easily configurable via environment variables, providing a convenient foundation for rapid development and deployment.
| π Language | π· Code | π Link |
|---|---|---|
| Russian | ru |
README.ru.md |
| Ukrainian | uk |
README.uk.md |
| Tatar | tt |
README.tt.md |
| Uzbek | uz |
README.uz.md |
| Kazakh | kk |
README.kk.md |
| English | en |
README.md |
- βοΈ aiogram β an asynchronous library for developing Telegram bots in Python.
- π§© pydantic-settings β settings management using Pydantic.
- πΎ sqlalchemy + aiosqlite β async ORM and DB driver.
- π redis β a client for Redis, used for caching.
- π babel β a tool for internationalization and localization.
- π§± alembic β a database migration tool.
- β¨ ruff β an extremely fast Python linter and formatter.
- π uv β an extremely fast package manager and bundler for Python.
// Directory tree (3 levels, limited to 200 entries)
βββ .gitignore
βββ .python-version
βββ Dockerfile
βββ LICENSE
βββ README.md
βββ babel.cfg
βββ example.alembic.ini
βββ images\
β βββ heart.webp
β βββ hello.jpg
βββ locales\
β βββ en\
β β βββ LC_MESSAGES\
β βββ messages.pot
β βββ ru\
β βββ LC_MESSAGES\
βββ pyproject.toml
βββ ruff.toml
βββ src\
β βββ .env.example
β βββ alembic\
β β βββ README
β β βββ env.py
β β βββ script.py.mako
β β βββ versions\
β βββ callbacks\
β β βββ __init__.py
β β βββ common.py
β β βββ lang.py
β βββ config.py
β βββ database\
β β βββ __init__.py
β β βββ base.py
β β βββ models\
β β βββ repositories\
β βββ handlers\
β β βββ __init__.py
β β βββ commands\
β βββ init_bot.py
β βββ middlewares\
β β βββ i18n.py
β β βββ rate_limit.py
β βββ run.py
β βββ schemas\
β β βββ __init__.py
β β βββ user.py
β βββ services\
β β βββ i18n_service.py
β β βββ messenger_service.py
β β βββ redis_service.py
β β βββ user_service.py
β βββ utils\
β βββ babel_locale.py
β βββ command_runner.py
β βββ i18n_aiogram.py
β βββ keyboards\
β β βββ __init__.py
β β βββ builders.py
β β βββ inline.py
β β βββ reply.py
β βββ logger.py
β βββ migration_database.py
β βββ settings_bot.py
βββ uv.lock
| π File / Folder | π§ Description |
|---|---|
pyproject.toml |
Project definition and its dependencies. |
src/.env.example |
Example environment variables file. |
src/run.py |
Entry point of the application, starts the bot. |
src/config.py |
Project configuration, loads environment variables. |
src/init_bot.py |
Initializes the aiogram bot and dispatcher, registers middleware and routers. |
src/database/ |
DB models, repositories, initialization |
src/handlers/ |
Telegram command/message handlers |
src/middlewares/ |
Internationalization & rate limiting |
src/services/ |
Logic services (Redis, i18n, etc.) |
src/alembic/ |
Database migrations |
src/utils/ |
Keyboards, logging, migrations |
locales/ |
Localization files for different languages. |
Redis is used for caching and rate limiting.
For Windows:
- Download the
.msiinstaller from the Microsoft's repository: https://github.com/microsoftarchive/redis/releases - Run the
.msifile and follow the installer instructions.
For Linux:
- Arch Linux:
sudo pacman -S redis sudo systemctl start redis sudo systemctl enable redis - Debian/Ubuntu:
sudo apt update sudo apt install redis-server sudo systemctl enable redis-server sudo systemctl start redis-server - Fedora:
sudo dnf install redis sudo systemctl start redis sudo systemctl enable redis
For macOS:
brew install redis
brew services start redis-
Install
uv:curl -LsSf https://astral.sh/uv/install.sh | shOr for Windows:
irm https://astral.sh/uv/install.ps1 | iex
-
Clone the repository and navigate to the folder:
git clone https://github.com/Kitty-Ilnazik/telegram-bot-template.git cd telegram-bot-template -
Configure environment variables: Copy the example
.envfile and fill it with your data:cp src/.env.example src/.env
Open
src/.envand enter values forTOKEN_BOT,REDIS_URL, andDB_URL. -
Install dependencies and run:
uv run start
-
Clone the repository and navigate to the folder:
git clone https://github.com/Kitty-Ilnazik/telegram-bot-template.git cd telegram-bot-template -
Configure environment variables: Copy the example
.envfile and fill it with your data:cp src/.env.example src/.env
Open
src/.envand enter values forTOKEN_BOT,REDIS_URL, andDB_URL. -
Create and activate a virtual environment:
- Windows:
python -m venv .venv .venv/Scripts/activate
- macOS/Linux:
python3 -m venv .venv . .venv/bin/activate
- Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Run the bot:
python src/run.py
-
Install Docker:
- Arch Linux:
sudo pacman -S docker sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER # Add user to docker group newgrp docker # Apply group changes
- Debian/Ubuntu:
sudo apt update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER newgrp docker
- Fedora:
sudo dnf install docker sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER newgrp docker
- Windows/macOS: Install Docker Desktop from the official Docker website.
- Arch Linux:
-
Build and run the Docker image:
docker build -t telegram-bot-template .docker build: Command to build a Docker image.-t telegram-bot-template: Assigns the tag (name)telegram-bot-templateto the created image..: Indicates that the Dockerfile is in the current directory.
docker run -d --name my-telegram-bot --env-file src/.env telegram-bot-template
docker run: Command to run a Docker container.-d: Runs the container in detached mode.--name my-telegram-bot: Assigns the namemy-telegram-botto the running container.--env-file src/.env: Instructs Docker to use environment variables fromsrc/.envinside the container.telegram-bot-template: The name of the Docker image to run.
-
Alembic Configuration: Copy the example Alembic configuration file:
cp example.alembic.ini alembic.ini
Open
alembic.iniand change the path to the.envfile in the[alembic]section tosrc/.env. -
Create and apply a migration:
uv run migrate commit "Initial migration"
-
Extract strings for translation:
uv run babel extract
-
Initialize a new language (e.g., Ukrainian):
uv run babel init -l uk
-
Update existing translations:
uv run babel update
-
Compile translations:
uv run babel compile
-
Update and compile translations:
uv run babel update-compile
Ruff is used for code formatting and error checking.
uv run ruff check .
uv run ruff format .