Skip to content

MrConsoleka/aiogram-bot-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Aiogram Bot Template

Static Badge Static Badge Static Badge
Static Badge

πŸ“Œ Description

β €

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.

β €

πŸ”¨ Functions

β €

  • /start - Start the bot
  • /language - Change language
  • /help - Help
  • /admin - Command for administrators

β €

πŸ—‚οΈ Template structure

β €

πŸ“ 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

β €

πŸ“‹ 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/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 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.
  • πŸ“ source/telegram/states/ - aiogram FSM states.
  • πŸ“ source/utils/ - Helper functions and utilities.
  • πŸ“„ source/__main__.py - Main entry point within the source package.
  • πŸ“„ .env.example - Example file for sensitive data (.env).

β €

βš™οΈ 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
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).

β €

πŸ’» 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-template-bot
  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-template-bot
  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

β €

πŸ“‹ Todo List

β €

  • touch the grass
  • Alembic
  • Aiogram-dialogs
  • .github/workflows

β €

πŸ—ƒοΈ Stack of Technologies

β €

  • 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.

β €

πŸ’Ό Credits

β €

β €

πŸ‘€ Author of Aiogram Template Bot

β € Β© Roman Alekseev

Releases

No releases published

Packages

No packages published