Русская версия / Russian version
A comprehensive educational guide for creating Telegram bots using two popular libraries: aiogram and python-telegram-bot.
This repository contains a complete course on Telegram bot development from simple to complex, with examples for both frameworks.
-
Example 1: Echo Bot - Simplest bot
- Basic structure
- Command and message handlers
- Running with polling
-
Example 2: Inline Keyboard - Interactive buttons
- Creating inline keyboards
- Handling callback queries
- Editing messages
-
Example 3: Reply Keyboard - Regular buttons
- ReplyKeyboardMarkup - persistent keyboard
- Requesting location and contact
- Multi-level menus
-
Example 4: FSM States - State machine
- Multi-step dialogs
- Storing data between messages
- User input validation
-
Example 5: Database - Working with databases
- SQLite integration
- CRUD operations
- Middleware (aiogram) / global object (PTB)
-
Example 6: Telegram Mini Apps (WebApp) - Web applications in Telegram
- Interactive web interfaces
- WebAppInfo and WebApp buttons
- Receiving data via web_app_data
- Telegram WebApp JavaScript API
-
Example 7: File Upload - File uploads for AI
- Receiving photos, documents, audio, video
- Downloading files to server
- Integration with OCR, Speech-to-Text, NLP
-
Example 8: Image Generation - Image generation
- Different ways to send images
- Working with PIL/Pillow
- Integration with Stable Diffusion, DALL-E, Matplotlib
-
Example 9: Media Group Albums - Albums (multiple uploads)
- Sending albums with multiple images
- MediaGroupBuilder (aiogram)
- Handling albums from users
- Grouping by media_group_id
-
Example 10: Telegram Payments - Payments with Telegram Stars
- Creating invoices for payment
- Pre-checkout query and successful payment
- Refunds
- Monetizing AI features
-
Example 11: WebHook Deployment - Production deployment with webhook
- WebHook instead of polling
- HTTPS and SSL setup
- Nginx as reverse proxy
- Docker deployment
- VPS, Heroku, Railway, Render
- Python 3.9+
- pip
Choose one of the frameworks:
# For aiogram
pip install -r requirements-aiogram.txt
# For python-telegram-bot
pip install -r requirements-ptb.txt
# Or install both
pip install -r requirements.txt- Find @BotFather in Telegram
- Send the command
/newbot - Follow the instructions to get your token
- Save the token as an environment variable:
export BOT_TOKEN="your_bot_token_here"# aiogram version
python examples/example_01_echo_bot/aiogram/bot.py
# python-telegram-bot version
python examples/example_01_echo_bot/python_telegram_bot/bot.pyTelegramBot/
├── LECTURE.md # Main lecture materials
├── README.md # This file
├── requirements.txt # All dependencies
├── requirements-aiogram.txt # Only aiogram
├── requirements-ptb.txt # Only python-telegram-bot
│
└── examples/
├── example_01_echo_bot/
│ ├── README.md # Example description
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_02_inline_keyboard/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_03_reply_keyboard/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_04_fsm_states/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_05_database/
│ ├── README.md
│ ├── aiogram/
│ │ ├── bot.py
│ │ ├── database.py
│ │ └── users.db (created automatically)
│ └── python_telegram_bot/
│ ├── bot.py
│ ├── database.py
│ └── users.db (created automatically)
│
├── example_06_mini_apps/
│ ├── README.md
│ ├── webapp/
│ │ └── index.html
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_07_file_upload/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_08_image_generation/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_09_media_group_albums/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
├── example_10_telegram_payments/
│ ├── README.md
│ ├── aiogram/
│ │ └── bot.py
│ └── python_telegram_bot/
│ └── bot.py
│
└── example_11_webhook/
├── README.md
├── aiogram/
│ └── bot_webhook.py
└── python_telegram_bot/
└── bot_webhook.py
- Official API Documentation
- BotFather - create bots
- Telegram Bots Guide
After learning the basics, we recommend exploring:
- Webhook instead of polling
- Docker for deployment
- Redis for state storage
- PostgreSQL for production databases
- Logging and monitoring
- Bot testing
- CI/CD pipeline
- Paid Media - paid content (images, video)
- Inline mode - inline queries
- Bot API Server - self-hosted Bot API server
- Webhook - webhook for deployment
If you found an error or want to improve the material:
- Create an Issue describing the problem
- Suggest a Pull Request with fixes
- Share your ideas
This educational material is provided "as is" for free use in educational purposes.
This educational guide was developed for students of HSE University, AI program.
Good luck learning Telegram bot development! 🚀🤖