Skip to content

MrConsoleka/aiogram-miniapp-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

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

Repository files navigation

image

Aiogram Bot Template with Mini Apps

Production-ready Telegram Bot + Mini Apps template with FastAPI backend and React frontend

Static Badge Static Badge Static Badge Static Badge Static Badge
Static Badge Static Badge

πŸ“Œ Description

β €

Aiogram Bot Template with Mini Apps β€” This template helps you quickly bootstrap Telegram bots with Mini Apps on the aiogram 3.x stack. It includes a ready project structure, command and message handlers, PostgreSQL/Redis integration, logging with loguru, internationalization, aiogram-dialog, dependency injection via dishka, webhook handling on FastAPI, plus a React Mini App frontend with secure authentication and REST API backend. The template is designed to remove routine setup and let you focus on your bot's logic and user experience.

β €

✨ Features

β €

Telegram Bot Foundation:

  • Built on aiogram 3.x with async handlers and modern router setup
  • Dependency Injection via Dishka for clean architecture
  • PostgreSQL + Redis with SQLAlchemy ORM and async access
  • Alembic migrations for database versioning
  • i18n support with Fluent/Fluentogram
  • aiogram-dialog for complex multi-step flows
  • FSM support (Finite State Machine) for forms and wizards

Mini Apps:

  • React 18 + TypeScript frontend with Vite
  • Telegram WebApp SDK integration (theme, haptics, main button)
  • Secure authentication: HMAC-SHA256 validation of initData
  • Replay attack protection with 1-hour token expiration
  • Rate limiting: 100 requests/minute per IP
  • Full i18next localization (English/Russian)
  • Responsive, mobile-first UI

Security Hardened:

  • CORS restricted to Telegram domains
  • Security headers via nginx (CSP, X-Frame-Options, etc.)
  • Backend runs as non-root user
  • SQL injection protection via ORM
  • XSS protection via React auto-escaping

DevOps Ready:

  • Docker Compose for one-command deployment
  • Multi-stage Dockerfiles for optimized images
  • nginx reverse proxy for API + WebApp
  • Health checks for bot, PostgreSQL, Redis, webapp, and nginx
  • Pre-commit hooks (Ruff, Mypy, Black, isort)

β €

πŸ”¨ Functions

β €

  • /start - Start the bot
  • /language - Change language
  • /help - Help
  • /profile - Open your Mini App profile
  • /admin - Command for administrators
  • /dialog - Demo dialog using aiogram-dialog
  • /fsm - Demo finite state machine form

β €

🌐 Mini Apps

β €

This template includes a React Mini App for user profiles:

  • Frontend: React 18 + TypeScript + Vite
  • Backend: FastAPI REST API with secure Telegram authentication
  • Features: User profiles, bio editing, i18n support, Telegram theme integration
  • Security: HMAC-SHA256 validation, replay attack protection, CORS restrictions, rate limiting

Try it: Send /profile to your bot!

β €

πŸ—οΈ Architecture

β €

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Telegram User  │─────▢│    Your Bot      │─────▢│   PostgreSQL     β”‚
β”‚   (Mobile App)   β”‚      β”‚  (aiogram 3.x)   β”‚      β”‚   + Redis        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                         β”‚
         β”‚ Opens Mini App          β”‚
         β–Ό                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  React Mini App  │◀────▢│  FastAPI Backend β”‚
β”‚   (TypeScript)   β”‚      β”‚   (/api/...)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                         β”‚
         └────────▢ nginx β—€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              (reverse proxy)

β €

πŸ—‚οΈ Template structure

β €

πŸ“ aiogram-miniapp-template/
β”œβ”€β”€ πŸ“‚ .github/              # CI/CD workflows, issue templates
β”œβ”€β”€ πŸ“‚ docs/                 # Documentation
β”œβ”€β”€ πŸ“‚ migrations/           # Alembic database migrations
β”œβ”€β”€ πŸ“‚ nginx/                # nginx reverse proxy configs
β”œβ”€β”€ πŸ“‚ scripts/              # Helper scripts
β”œβ”€β”€ πŸ“‚ source/               # Main Python source code
β”‚   β”œβ”€β”€ πŸ“‚ api/             # FastAPI backend for Mini Apps
β”‚   β”œβ”€β”€ πŸ“‚ config/          # Configuration
β”‚   β”œβ”€β”€ πŸ“‚ database/        # Models, repositories, UoW
β”‚   β”œβ”€β”€ πŸ“‚ schemas/         # Pydantic schemas for API
β”‚   β”œβ”€β”€ πŸ“‚ services/        # Business logic
β”‚   β”œβ”€β”€ πŸ“‚ telegram/        # Bot handlers, keyboards, filters
β”‚   └── πŸ“‚ utils/           # Utilities
β”œβ”€β”€ πŸ“‚ tests/                # Test suite
β”œβ”€β”€ πŸ“‚ webapp/               # React Mini App frontend
β”‚   β”œβ”€β”€ πŸ“‚ public/locales/  # i18n translations
β”‚   └── πŸ“‚ src/             # React components, hooks, API client
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ webapp.Dockerfile
└── pyproject.toml

Full project structure is documented in docs/reference/project-structure.md.

β €

πŸ“‹ About the content

β €

  • πŸ“ source/ - Main application source code.
  • πŸ“ source/api/ - FastAPI backend for Mini Apps (routes, middlewares, utils).
  • πŸ“ source/config/ - Application configuration settings.
  • πŸ“ source/constants/ - Project constants.
  • πŸ“ source/data/ - Data generated by the application (e.g., logs).
  • πŸ“ source/database/ - Database interaction logic (models, repositories, UoW).
  • πŸ“ source/schemas/ - Pydantic schemas for API request/response validation.
  • πŸ“ source/services/ - Business logic layer.
  • πŸ“ source/telegram/ - Bot components (handlers, keyboards, filters, middlewares, states, dialogs).
  • πŸ“ source/utils/ - Helper utilities (logger, i18n, validators).
  • πŸ“ webapp/ - React Mini App frontend with TypeScript.
  • πŸ“ webapp/src/ - React components, hooks, pages, API client.
  • πŸ“ webapp/public/locales/ - i18n translation files for Mini App.
  • πŸ“ nginx/ - nginx configuration for reverse proxy.
  • πŸ“ docs/ - Development and deployment documentation.
  • πŸ“ scripts/ - Helper scripts for development tasks.
  • πŸ“ tests/ - Test suite.

β €

πŸ“š Documentation

β €

Full documentation is available in the docs/ folder:

Getting Started:

Bot Development:

Mini Apps Development:

Deployment:

Reference:

β €

πŸ”“ Environment Variables

β €

Backend & Bot (.env):

Environment Variable Name Description
ENVIRONMENT Application environment (development, test, production).
TG__WEBHOOK_USE Boolean value (True/False) indicating whether to use webhooks (True) or long polling (False).
TG__WEBHOOK_PATH Path for Telegram to send webhook updates (appended to WEBHOOK__URL).
TG__BOT_TOKEN Your Telegram bot token, obtained from @BotFather in Telegram.
TG__ADMIN_IDS List of Telegram user IDs (JSON list or comma-separated) who will have administrator rights in the bot.
WEBHOOK__URL Public URL where Telegram will send updates if webhooks are enabled.
WEBHOOK__HOST Host or IP address where the webhook server will listen for incoming connections (usually 0.0.0.0).
WEBHOOK__PORT Port on which the webhook server will listen for incoming connections.
WEBHOOK__SECRET Secret token that Telegram includes in webhook request headers to verify 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 Name of the database to connect to.
REDIS__HOST Redis server host used for FSM 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 Redis database index to use (a number from 0 to 15, default is 0).
API__HOST FastAPI host for Mini Apps API (usually 0.0.0.0).
API__PORT FastAPI port (default 8000).
API__DEBUG Debug mode for API (true/false).
WEBAPP__URL Public URL where Mini App is hosted (e.g., https://your-domain.com).

Mini App Frontend (webapp/.env):

Environment Variable Name Description
VITE_API_URL Base URL for the Mini App API (default /api).

Note: docker-compose maps WEBHOOK__PORT to the host (defaults to 8080 if not set).

β €

πŸ’» Bot Setup

β €

πŸ“¦ Using Docker (Recommended)

β €

  1. Clone the repository and navigate into the project directory:

    git clone https://github.com/MrConsoleka/aiogram-miniapp-template.git
    cd aiogram-miniapp-template
  2. Configure environment variables:

    cp .env.example .env
    cp webapp/.env.example webapp/.env
    # Edit .env and webapp/.env with your settings
  3. Start all services with Docker Compose:

    docker compose up -d

β €

πŸ“¦ Using UV (Local Development)

β €

  1. Clone the repository and navigate into the project directory:

    git clone https://github.com/MrConsoleka/aiogram-miniapp-template.git
    cd aiogram-miniapp-template
  2. Ensure you have uv installed. If not, you can install it 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. Configure environment variables:

    cp .env.example .env
    cp webapp/.env.example webapp/.env
    # Edit .env and webapp/.env with your settings
  7. Run the bot:

    make run
  8. (Optional) Run Mini App frontend separately:

    # In a separate terminal
    cd webapp
    npm install
    npm run dev

    If running Vite dev server, set WEBAPP__URL=http://localhost:3000 in .env.

β €

πŸ—„οΈ Migrations

β €

Create and apply migrations with Alembic:

make migration MESSAGE="create users"
uv run alembic upgrade head

β €

πŸ§ͺ Testing

β €

Run tests locally:

uv run pytest tests/

β €

🧰 Pre-commit

β €

pre-commit install
pre-commit run --all-files

β €

🧩 Development Services

β €

make dev-up
make dev-down

β €

πŸ—ƒοΈ Stack of Technologies

β €

Backend:

Frontend (Mini Apps):

DevOps:

β €

πŸ’Ό Credits

β €

β €

πŸ‘€ Author

β € Β© Roman Alekseev