β
Aiogram Bot Template β This is a template for quickly creating Telegram bots using the aiogram
library. It provides a basic project structure including command and message handling, optional database integration, logging, and other useful features. The template helps developers avoid repetitive setup tasks and focus on the bot's core logic.
β
β
/start
- Start the bot/language
- Change language/help
- Help/admin
- Command for administrators
β
β
π aiogram_bot_template/
βββββ π .github/
β βββββ π ISSUE_TEMPLATE/
β β βββ bug_report.md
β β βββ feature_request.md
β β
β βββββ π workflows/
β
βββββ π 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
β β βββ run.py
β β
β βββββ π locales/
β β βββ π en/
β β β βββ bot.ftl
β β βββ π ru/
β β βββ bot.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
β β β β
β β β βββ __init__.py
β β β
β β βββββ π keyboards/
β β β βββ __init__.py
β β β βββ builder.py
β β β βββ callback_factory.py
β β β βββ inline.py
β β β βββ reply.py
β β β
β β βββββ π middlewares/
β β β βββ __init__.py
β β β βββ callback_throttling.py
β β β βββ db.py
β β β βββ message_throttling.py
β β β βββ reporting.py
β β β
β β βββββ π states/
β β β βββ __init__.py
β β β βββ registration.py
β β β
β β βββ __init__.py
β β
β βββββ π utils/
β β βββ __init__.py
β β βββ logging.py
β β βββ set_commands.py
β β
β βββ __init__.py
β βββ __main__.py
β
βββ .env.example
βββ .gitignore
βββ .dockerignore
βββ Dockerfile
βββ docker-compose.yml
βββ pyproject.toml
βββ Makefile
βββ LICENSE
βββ README.md
βββ SECURITY.md
β
β
π 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/tools/
- Helper tools for working with the database.π source/enums/
- Enum definitions.π source/factory/
- Factory for creating key objects (bot, dispatcher).π 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 andaiogram
.π source/telegram/filters/
- Customaiogram
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.π source/telegram/states/
-aiogram
FSM states.π source/utils/
- Helper functions and utilities.π source/__main__.py
- Main entry point within thesource
package.π .env.example
- Example file for sensitive data (.env).
β
β
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
β
β
Environment Variable Name | Description |
---|---|
TG__BOT_TOKEN | Your Telegram bot token, obtained from @BotFather in Telegram. |
TG__ADMIN_IDS | List of Telegram user IDs (in JSON list format or comma-separated) who will have administrator rights in the bot. |
WEBHOOK__USE | Boolean value (True /False ) indicating whether to use webhooks (True ) or long polling (False ). |
WEBHOOK__URL | The public URL where Telegram will send updates if webhooks are enabled. |
WEBHOOK__HOST | The host or IP address where the webhook server will listen for incoming connections. Usually 0.0.0.0 to listen on all interfaces. |
WEBHOOK__PORT | The port on which the webhook server will listen for incoming connections. |
WEBHOOK__PATH | The specific path on WEBHOOK__URL where Telegram will send POST requests with updates. |
WEBHOOK__SECRET | A secret token that Telegram may include in webhook request headers to verify their 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 | The name of the database to connect to. |
REDIS__HOST | Redis server host. Used for FSM (states) 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 | The Redis database index to use (a number from 0 to 15, default is 0). |
β
β
β
-
Clone the repository and navigate into the project directory:
git clone https://github.com/MrConsoleka/aiogram-bot-template.git cd aiogram-template-bot
-
Ensure you have
uv
installed. If not, you can install it, for example, usingpip
:pip install uv
-
Create a virtual environment:
make venv
-
Activate the virtual environment:
# For Linux or macOS: source .venv/bin/activate # For Windows: .venv\Scripts\activate
-
Install dependencies:
make install
-
To run the bot, use the command:
make run
β
β
-
Clone the repository and navigate into the project directory:
git clone https://github.com/MrConsoleka/aiogram-bot-template.git cd aiogram-template-bot
-
Build the Docker Image:
make docker-build
-
Run the Project with Docker Compose:
make docker-up
-
Verify Bot is Running (Optional):
make docker-logs
or
make docker-logs SERVICE=bot
-
Stop the Project:
make docker-down
β
β
- touch the grass
- Alembic
- Aiogram-dialogs
- .github/workflows
β
β
- aiogram-3x - A fully asynchronous framework for the Telegram Bot API.
- aiogram_i18n - Library for Telegram bot internationalization.
- postgresql - PostgreSQL database.
- asyncpg - Library for working with PostgreSQL databases.
- redis - High-performance in-memory data structure store.
- loguru - Library for logging.
- aiohttp - Library for creating and running an asynchronous web server.
- Ruff, Mypy, Pre-commit, Isort, Black - Tools for code quality and formatting.
β
β
- aiogram_template - Inspired by Abdullah's project, many thanks to him <3
β
β Β© Roman Alekseev