Aiogram 3: Basics and Handlers for Telegram Bots — Learning with AI on ASI Biont

Introduction: Why aiogram 3 is the Standard for Telegram Bot Development

In June 2026, Telegram Bot Development is not just a hobby but a powerful tool for business and automation. If you want to create reliable, scalable bots, aiogram 3 is your choice. This library offers an asynchronous architecture, flexible handlers, and advanced features like Finite State Machine (FSM) and integration with Telegram Web Apps. On the ASI Biont platform, we combine AI learning with practical cases so you can master bot development from scratch to production.

In this article, we'll break down key aspects: handlers, keyboards, middleware, and filters, and show how to apply them in real projects. Ready to dive in? Let's go!

Basics of aiogram 3: Handlers and Their Types

Handlers are the heart of any bot. They process commands, messages, callbacks, and even payments. In aiogram 3, a decorator approach is used, making the code clean and readable.

Main Types of Handlers:

  • MessageHandler — for text messages, commands, and media.
  • CallbackQueryHandler — for handling inline button presses.
  • PreCheckoutQueryHandler — for working with payments via Telegram Stars.
  • MyChatMemberHandler — for administration (e.g., when a bot is added to a group).

Example of a simple handler:

from aiogram import Router, types
from aiogram.filters import Command

router = Router()

@router.message(Command("start"))
async def cmd_start(message: types.Message):
    await message.answer("Hello! I'm a bot created with aiogram 3.")

Finite State Machine (FSM): Managing Dialogs

FSM is a way to organize multi-step scenarios, such as registration or order placement. In aiogram 3, FSM is implemented via State and StatesGroup.

Example Scenario:

  1. User enters their name.
  2. Bot requests an email.
  3. Data is saved to the database (DB).

Code:

from aiogram.fsm.state import State, StatesGroup

class Form(StatesGroup):
    name = State()
    email = State()

@router.message(Form.name)
async def process_name(message: types.Message, state: FSMContext):
    await state.update_data(name=message.text)
    await state.set_state(Form.email)
    await message.answer("Enter your email:")

FSM is ideal for Telegram Bot Development in business: from lead collection to service customization.

Keyboards: Inline and Reply

Keyboards are the user interaction interface. In aiogram 3, two types are supported:
- ReplyKeyboardMarkup — standard buttons below the input field.
- InlineKeyboardMarkup — buttons inside a message (for callbacks and links).

Comparison Table:

Type Application Example Usage
Reply Simple commands Buttons "Menu", "Contacts"
Inline Actions within chat Buttons "Buy", "Details"

Example of an inline keyboard:

from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

kb = InlineKeyboardMarkup(inline_keyboard=[
    [InlineKeyboardButton(text="Buy", callback_data="buy")],
    [InlineKeyboardButton(text="Help", callback_data="help")]
])

Middleware and Filters: Flexible Logic

Middleware allows you to execute code before or after a handler. For example, logging requests or checking channel subscription.

Example Middleware for Access Control:

from aiogram import BaseMiddleware

class AccessMiddleware(BaseMiddleware):
    async def __call__(self, handler, event, data):
        user_id = event.from_user.id
        if user_id in ALLOWED_USERS:
            return await handler(event, data)
        await event.answer("Access denied.")

Filters (aiogram.filters) are quick condition checks. For example, Command("start") or ChatTypeFilter(chat_type="private"). Combine them for precise routing.

Integration: Payments, Telegram Web Apps, and DB

A modern Telegram bot cannot do without external services. Let's look at three key areas:

1. Payments via Telegram Stars

Bots can accept payments directly. Use PreCheckoutQueryHandler and Message.successful_payment. This is convenient for selling digital goods.

2. Telegram Web Apps

Embed web applications into the bot interface. For example, an order form or calculator. The Web App sends data back to the bot via WebAppData.

3. Databases (DB)

Use SQLite, PostgreSQL, or Redis to store users and states. In aiogram 3, it's easy to integrate ORM (e.g., SQLAlchemy).

Example of saving data:

async def save_user(user_id: int, name: str):
    async with async_session() as session:
        session.add(User(id=user_id, name=name))
        await session.commit()

Practice: Creating a Bot for Business

Suppose you are developing a bot for an online store. Here's a typical scenario:
1. User clicks "Catalog" (handler with keyboard).
2. Bot shows products via inline buttons (callback handler).
3. User places an order (FSM with address input).
4. Payment via Telegram Stars (payment handler).
5. Admin receives notification (middleware for logging).

This approach scales from simple chatbots to complex ERP systems.

Conclusion

Aiogram 3 is a mature ecosystem for Telegram Bot Development. By mastering handlers, FSM, keyboards, and middleware, you can create bots of any complexity. In the ASI Biont course, we delve into each topic: from basics to integration with payments and web applications. Learning with AI helps speed up the process: the neural network generates examples and suggests optimal solutions.

Ready to start? Take the "Telegram Bot Development" course on ASI Biont and create your first bot today!

← All posts

Comments