Skip to content

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.

    → Learn More

  • Handlers


    Command handlers and message processing.

    → Learn More

Quick Start

1. Get a Bot Token

  1. Message @BotFather on Telegram
  2. Send /newbot and follow the prompts
  3. Copy the token

2. Configure Environment

Add to .env:

TELEGRAM_BOT_TOKEN=your-bot-token

3. Start the Bot

make bot-dev

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