Telegram Bot¶
Build Telegram bots with aiogram and the factory pattern.
Overview¶
The Telegram bot uses aiogram for async bot development with handlers and commands.
Architecture¶
Telegram API
│
▼
┌─────────────┐
│ Bot │
│ (aiogram) │
└──────┬──────┘
│
▼
┌─────────────┐
│ Dispatcher │
│ (Router) │
└──────┬──────┘
│
▼
┌─────────────┐
│ Handlers │
│ (Commands) │
└─────────────┘
Topics¶
-
Bot & Dispatcher Factories
How the bot and dispatcher are created and configured.
-
Handlers
Command handlers and message processing.
Quick Start¶
1. Get a Bot Token¶
- Message @BotFather on Telegram
- Send
/newbotand follow the prompts - Copy the token
2. Configure Environment¶
Add to .env:
3. Start the Bot¶
4. Test It¶
Message your bot with /start or /id.
Configuration¶
| Variable | Type | Default | Description |
|---|---|---|---|
TELEGRAM_BOT_TOKEN |
SecretStr |
Required | Bot token from BotFather |
TELEGRAM_BOT_PARSE_MODE |
str |
HTML |
Default message parse mode |
Entry Point¶
The bot is started via delivery/bot/__main__.py:
from aiogram import Bot, Dispatcher
from core.configs.infrastructure import configure_infrastructure
from ioc.container import get_container
def main() -> None:
configure_infrastructure(service_name="bot")
container = get_container()
bot = container.resolve(Bot)
dispatcher = container.resolve(Dispatcher)
dispatcher.run_polling(bot)
if __name__ == "__main__":
main()
Available Commands¶
| Command | Description |
|---|---|
/start |
Welcome message |
/id |
Get user and chat IDs |
Related Topics¶
- Your First Bot Command — Tutorial
- aiogram Documentation — Official docs