Skip to content

Telegram Bot Template: a flexible Python aiogram boilerplate. Features modular architecture, SQLite/MySQL support, Redis caching, i18n, and anti-spam. Configurable via environment variables for rapid development and deployment

License

Notifications You must be signed in to change notification settings

kitty-ilnazik/telegram-bot-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

# 🌸 Telegram Bot Template

A modern, modular, and elegant boilerplate for building Telegram bots with aiogram.

anime-banner

Stars Forks Issues License


🐾 Overview

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.


🌍 Translations

🌐 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

πŸ› οΈ Tools and Libraries

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

πŸ“ Project Structure

// 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

πŸ“„ Description of Important Files

πŸ“‚ 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.

πŸš€ Setup and Running

Installing Redis

Redis is used for caching and rate limiting.

For Windows:

  1. Download the .msi installer from the Microsoft's repository: https://github.com/microsoftarchive/redis/releases
  2. Run the .msi file 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

Running without Docker

Using uv (recommended)

  1. Install uv:

    curl -LsSf https://astral.sh/uv/install.sh | sh

    Or for Windows:

    irm https://astral.sh/uv/install.ps1 | iex
  2. Clone the repository and navigate to the folder:

    git clone https://github.com/Kitty-Ilnazik/telegram-bot-template.git
    cd telegram-bot-template
  3. Configure environment variables: Copy the example .env file and fill it with your data:

    cp src/.env.example src/.env

    Open src/.env and enter values for TOKEN_BOT, REDIS_URL, and DB_URL.

  4. Install dependencies and run:

    uv run start

Without uv

  1. Clone the repository and navigate to the folder:

    git clone https://github.com/Kitty-Ilnazik/telegram-bot-template.git
    cd telegram-bot-template
  2. Configure environment variables: Copy the example .env file and fill it with your data:

    cp src/.env.example src/.env

    Open src/.env and enter values for TOKEN_BOT, REDIS_URL, and DB_URL.

  3. Create and activate a virtual environment:

    • Windows:
      python -m venv .venv
      .venv/Scripts/activate
    • macOS/Linux:
      python3 -m venv .venv
      . .venv/bin/activate
  4. Install dependencies:

    pip install -r requirements.txt
  5. Run the bot:

    python src/run.py

Running with Docker

  1. 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.
  2. 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-template to 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 name my-telegram-bot to the running container.
    • --env-file src/.env: Instructs Docker to use environment variables from src/.env inside the container.
    • telegram-bot-template: The name of the Docker image to run.

πŸ—„ Using DB Migrations (Alembic)

  1. Alembic Configuration: Copy the example Alembic configuration file:

    cp example.alembic.ini alembic.ini

    Open alembic.ini and change the path to the .env file in the [alembic] section to src/.env.

  2. Create and apply a migration:

    uv run migrate commit "Initial migration"

🌐 Using PyBabel for Localization

  1. Extract strings for translation:

    uv run babel extract
  2. Initialize a new language (e.g., Ukrainian):

    uv run babel init -l uk
  3. Update existing translations:

    uv run babel update
  4. Compile translations:

    uv run babel compile
  5. Update and compile translations:

    uv run babel update-compile

🧹 Using Ruff

Ruff is used for code formatting and error checking.

uv run ruff check .
uv run ruff format .

About

Telegram Bot Template: a flexible Python aiogram boilerplate. Features modular architecture, SQLite/MySQL support, Redis caching, i18n, and anti-spam. Configurable via environment variables for rapid development and deployment

Topics

Resources

License

Stars

Watchers

Forks