A clean and structured template project for building Telegram bots using aiogram framework (version 3.21+).
- ποΈ Clean Architecture - Well-organized project structure with separation of concerns
- π§ Configuration Management - Easy configuration through INI files
- π― Filter System - Built-in user filtering (admin/non-admin)
- π¨ Keyboard Support - Ready-to-use keyboard utilities (inline and reply)
- π Middleware Support - Environment middleware for dependency injection
- π Logging - Structured logging with admin notification utilities
- π State Management - FSM (Finite State Machine) support for conversation flows
aiogram-template/
βββ config.ini.example # Example configuration file
βββ requirements.txt # Python dependencies
βββ LICENSE # License file
βββ README.md # This file
βββ src/
βββ __init__.py
βββ main.py # Application entry point
βββ config.py # Configuration loader
βββ filters/ # Custom filters
β βββ __init__.py
β βββ user.py # User filter (admin/non-admin)
βββ handlers/ # Message and callback handlers
β βββ __init__.py
β βββ user.py # User-related handlers
βββ keyboards/ # Keyboard builders
β βββ __init__.py
β βββ reply.py # Reply keyboard utilities
β βββ user/ # User-specific keyboards
β βββ __init__.py
β βββ inline.py # Inline keyboard builders
β βββ reply.py # Reply keyboard builders
β βββ util.py # Keyboard utilities
βββ middlewares/ # Custom middlewares
β βββ __init__.py
β βββ environment.py # Environment middleware for DI
βββ misc/ # Miscellaneous utilities
β βββ __init__.py
β βββ logs.py # Logging utilities
β βββ singleton.py # Singleton pattern implementation
β βββ states.py # FSM state definitions
βββ models/ # Data models
β βββ __init__.py
βββ services/ # External service integrations
βββ __init__.py
- Python 3.10 or higher
- pip package manager
- Clone the repository:
git clone <repository-url>
cd aiogram-template- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Configure the bot:
cp config.ini.example config.iniEdit config.ini and add your bot token and admin IDs:
[bot]
token = your_bot_token_here
admin_ids = 123456789, 987654321To get a bot token:
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions - Copy the token provided by BotFather
To find your admin user ID:
- Search for @userinfobot on Telegram
- Start a conversation and it will show your user ID
- Add multiple admin IDs separated by commas
The project uses an INI file for configuration. Create a config.ini file in the project root:
[bot]
token = your_bot_token_here
admin_ids = 123456789, 987654321token(required): Your Telegram bot token obtained from BotFatheradmin_ids(optional): Comma-separated list of Telegram user IDs that should have admin privileges
Start the bot with:
python -m src.mainOr:
python src/main.pyThe bot will start polling for updates from Telegram.
Add new handlers in the src/handlers/ directory:
from aiogram import Router
from aiogram.filters import Command
from aiogram.types import Message
router = Router()
@router.message(Command("start"))
async def cmd_start(message: Message):
await message.answer("Hello! Welcome to the bot.")Then register the router in src/main.py:
from src.handlers.your_handler import router as your_router
dp.include_router(your_router)The template includes a UserFilter that can be used to restrict handlers to non-admin users:
from src.filters.user import UserFilter
@router.message(Command("user_command"), UserFilter())
async def user_only_handler(message: Message):
await message.answer("This command is available only to regular users.")The UserFilter returns True if the user is not an admin, allowing you to filter out admin users from specific handlers.
Use the EnvironmentMiddleware to inject dependencies into handlers:
from src.middlewares.environment import EnvironmentMiddleware
config = load_config()
dp.message.middleware(EnvironmentMiddleware(config=config))Access injected data in handlers through the data parameter:
async def handler(message: Message, data: dict):
config = data['config']- Handlers: Place all message and callback handlers in
src/handlers/ - Filters: Create custom filters in
src/filters/ - Keyboards: Build keyboard layouts in
src/keyboards/ - Services: Add external service integrations in
src/services/ - Models: Define data models in
src/models/
Add new Python packages to requirements.txt:
aiogram~=3.21
your-package==1.0.0Then install:
pip install -r requirements.txtThe UserFilter class checks if a user is not an admin. It accesses the configuration from the bot's data dictionary and compares the user's ID against the admin IDs list.
The EnvironmentMiddleware allows you to inject dependencies (like configuration, database connections, etc.) into all handlers without explicitly passing them.
The send_logs_to_admins function in src/misc/logs.py can be used to send log messages to all configured admin users.
A singleton metaclass is available in src/misc/singleton.py for creating singleton classes when needed.
- Verify
config.iniexists and contains a valid token - Check that all dependencies are installed:
pip install -r requirements.txt - Ensure the bot token is correct and hasn't been revoked
- Verify admin IDs are correctly formatted in
config.ini(comma-separated integers) - Check that the configuration is properly loaded and injected via middleware
- Make sure you're running from the project root directory
- Verify your Python path includes the project root
- Check that all
__init__.pyfiles are present in package directories
See the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
For issues, questions, or contributions, please open an issue on the repository.