β
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.
β
β
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)
β
β
/start- Start the bot/language- Change language/help- Help/profile- Open your Mini App profile/admin- Command for administrators/dialog- Demo dialog usingaiogram-dialog/fsm- Demo finite state machine form
β
β
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!
β
β
ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
β Telegram User βββββββΆβ Your Bot βββββββΆβ PostgreSQL β
β (Mobile App) β β (aiogram 3.x) β β + Redis β
ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββ
β β
β Opens Mini App β
βΌ βΌ
ββββββββββββββββββββ ββββββββββββββββββββ
β React Mini App βββββββΆβ FastAPI Backend β
β (TypeScript) β β (/api/...) β
ββββββββββββββββββββ ββββββββββββββββββββ
β β
ββββββββββΆ nginx ββββββββββ
(reverse proxy)
β
β
π 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.
β
β
π 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.
β
β
Full documentation is available in the docs/ folder:
Getting Started:
Bot Development:
Mini Apps Development:
Deployment:
Reference:
β
β
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).
β
β
β
-
Clone the repository and navigate into the project directory:
git clone https://github.com/MrConsoleka/aiogram-miniapp-template.git cd aiogram-miniapp-template -
Configure environment variables:
cp .env.example .env cp webapp/.env.example webapp/.env # Edit .env and webapp/.env with your settings -
Start all services with Docker Compose:
docker compose up -d
β
β
-
Clone the repository and navigate into the project directory:
git clone https://github.com/MrConsoleka/aiogram-miniapp-template.git cd aiogram-miniapp-template -
Ensure you have
uvinstalled. If not, you can install it 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
-
Configure environment variables:
cp .env.example .env cp webapp/.env.example webapp/.env # Edit .env and webapp/.env with your settings -
Run the bot:
make run
-
(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:3000in.env.
β
β
Create and apply migrations with Alembic:
make migration MESSAGE="create users"
uv run alembic upgrade headβ
β
Run tests locally:
uv run pytest tests/β
β
pre-commit install
pre-commit run --all-filesβ
β
make dev-up
make dev-downβ
β
Backend:
- aiogram-3x - Asynchronous framework for the Telegram Bot API.
- aiogram-dialog - Dialog manager for building interactive flows.
- dishka - Dependency injection container.
- fastapi & uvicorn - Webhook server and Mini Apps API.
- pydantic & pydantic-settings - Data validation and configuration management.
- postgresql with sqlalchemy and asyncpg - Database layer.
- redis - In-memory data store for FSM and caching.
- loguru - Logging library.
- alembic - Database migrations.
Frontend (Mini Apps):
- react - UI library.
- typescript - Type safety.
- vite - Build tool and dev server.
- telegram-webapp-sdk - Telegram Mini Apps API.
- i18next - Internationalization.
- zustand - State management.
DevOps:
- docker & docker-compose - Containerization and orchestration.
- nginx - Reverse proxy and web server.
- ruff - Fast Python linter.
- mypy - Static type checker.
- pre-commit - Git hooks framework.
β
β
- aiogram-bot-template - My scalable template for Telegram bots on aiogram without Mini Apps.
β
β Β© Roman Alekseev
