Skip to main content

Declarative UI routing framework for aiogram 3 — define Telegram bot interfaces through Pydantic schemas

Project description

aiogram-ui-router

Declarative UI routing framework for aiogram 3. Define complex Telegram bot interfaces through Pydantic schemas instead of writing handler code.

Features

  • Declarative scenes — describe screens, keyboards, content and navigation as data
  • Scene navigation — tree-based navigation with history, back button support
  • Dynamic content — text templates with flags, variables and conditional rendering
  • Inline & reply keyboards — static and dynamic keyboards with pagination
  • Rule engine — declarative conditions for handlers and content
  • Variables — bot/user/chat-scoped state with typed values and TTL
  • Events — internal pub/sub event bus with scheduled events (cron, interval)
  • Localization — multi-language support via Mozilla Fluent
  • Global handlers — priority-based handlers that work across all scenes
  • Input validation — custom validators for user text input
  • Analytics — track scene views, button clicks, navigation flows
  • Schema validation — detect broken references, unreachable scenes, missing functions
  • Hot reload — update UI schema without restarting the bot
  • Multi-router — multiple independent UI routers per bot

Installation

pip install aiogram-ui-router

With structured logging:

pip install aiogram-ui-router[logging]

Quick Start

import asyncio
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.types import Message

from ui_router import (
    UIRouter, UIRouterExecutor, Scene, Handler, ActionInstruction,
    ActionType, EventType, TransitionType, MessageContent,
    Keyboard, Button, Flag, FlagGetter, DynamicContent, ContentTemplate,
    SharedServices, InMemoryNavigationStorage, InMemoryVariableRepository,
    EventBus, InMemoryEventScheduler,
)


async def get_user_name(context) -> str:
    return context.event_data["event_from_user"].first_name


schema = UIRouter(
    name="my_bot",
    version="1.0.0",
    initial_scene="welcome",
    scenes=[
        Scene(
            id="welcome",
            name="Welcome",
            flags=[
                Flag(
                    name="name",
                    getter=FlagGetter(type="function", function="get_user_name"),
                ),
            ],
            default_content=MessageContent(
                text=DynamicContent(
                    type="template",
                    template=ContentTemplate(
                        template="Hello, {name}!",
                        flags=["name"],
                    ),
                ),
            ),
            default_keyboard=Keyboard(
                is_inline=True,
                buttons=[
                    [Button(text="About", callback_action="goto_about")],
                ],
            ),
            handlers=[
                Handler(
                    name="goto_about",
                    event_type=EventType.CALLBACK,
                    actions=[
                        ActionInstruction(
                            type=ActionType.GOTO_SCENE,
                            scene_id="about",
                            transition=TransitionType.EDIT_SMART,
                        ),
                    ],
                ),
            ],
        ),
        Scene(
            id="about",
            name="About",
            default_content=MessageContent(text="Built with aiogram-ui-router"),
            default_keyboard=Keyboard(
                buttons=[[Button(text="Back", callback_action="go_back")]],
            ),
            handlers=[
                Handler(
                    name="go_back",
                    event_type=EventType.CALLBACK,
                    actions=[ActionInstruction(type=ActionType.BACK)],
                ),
            ],
        ),
    ],
)


async def main() -> None:
    bot = Bot(token="YOUR_TOKEN")
    dp = Dispatcher(storage=MemoryStorage())

    shared = SharedServices(
        navigation_storage=InMemoryNavigationStorage(),
        variable_repository=InMemoryVariableRepository(),
        event_bus=EventBus(),
        event_scheduler=InMemoryEventScheduler(event_callback=lambda e: None),
    )

    executor = UIRouterExecutor(schema=schema, shared=shared)
    executor.registry.getters.register("get_user_name", get_user_name)

    dp.include_router(executor.get_router())

    @dp.message(CommandStart())
    async def start(message: Message, **kwargs) -> None:
        await executor.start(message.from_user.id, message, **kwargs)

    await dp.start_polling(bot)


if __name__ == "__main__":
    asyncio.run(main())

Core Concepts

Scenes

A scene is a screen in your bot UI. Each scene has default content, a keyboard, flags (dynamic data) and handlers (reactions to user actions).

Handlers

Handlers respond to events within a scene. Each handler has a list of sequential actions: navigate to another scene, send/edit messages, save input, call business logic, emit events, etc.

Flags

Flags provide dynamic data for content templates. They can be static values, async function results, FSM state, user input, variables, or conditional (rule-based).

Actions

Over 20 action types: GOTO_SCENE, BACK, SEND_MESSAGE, EDIT_MESSAGE, DELETE_MESSAGE, SAVE_INPUT, SET_VARIABLE, BUSINESS_ACTION, SCHEDULE_EVENT, EMIT_EVENT, ANSWER_CALLBACK, ANSWER_INLINE_QUERY, CUSTOM, and more.

Variables

Typed key-value storage scoped to bot, user or chat. Supports int, float, bool, str, datetime, json types with optional TTL and default values.

Rule Engine

Declarative condition evaluation using operators: eq, ne, gt, gte, lt, lte, in, not_in, contains, starts_with, ends_with. Used for conditional content, flags and handler filtering.

Examples

See the examples/ directory:

Example Description
example.py Basic setup with scenes, flags and navigation
example_advanced.py Shop with cart, forms, validation and business logic

Requirements

  • Python >= 3.13
  • aiogram >= 3.25

License

MIT


aiogram-ui-router (RU)

Декларативный UI-фреймворк для aiogram 3. Описывайте интерфейс Telegram-бота через Pydantic-схемы вместо написания хендлеров вручную.

Возможности

  • Декларативные сцены — экраны, клавиатуры, контент и навигация описываются как данные
  • Навигация — древовидная навигация с историей и кнопкой «Назад»
  • Динамический контент — шаблоны с флагами, переменными и условным рендерингом
  • Клавиатуры — inline и reply, статические и динамические с пагинацией
  • Движок правил — декларативные условия для хендлеров и контента
  • Переменные — типизированное состояние на уровне бота/пользователя/чата с TTL
  • События — внутренняя шина событий с планировщиком (cron, interval)
  • Локализация — мультиязычность через Mozilla Fluent
  • Глобальные хендлеры — обработчики с приоритетами, работающие во всех сценах
  • Валидация ввода — кастомные валидаторы для пользовательского текста
  • Аналитика — отслеживание просмотров сцен, кликов, навигации
  • Валидация схемы — обнаружение битых ссылок, недостижимых сцен, отсутствующих функций
  • Горячая перезагрузка — обновление UI-схемы без перезапуска бота
  • Мульти-роутер — несколько независимых UI-роутеров на одного бота

Установка

pip install aiogram-ui-router

Со структурированным логированием:

pip install aiogram-ui-router[logging]

Быстрый старт

import asyncio
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.types import Message

from ui_router import (
    UIRouter, UIRouterExecutor, Scene, Handler, ActionInstruction,
    ActionType, EventType, TransitionType, MessageContent,
    Keyboard, Button, Flag, FlagGetter, DynamicContent, ContentTemplate,
    SharedServices, InMemoryNavigationStorage, InMemoryVariableRepository,
    EventBus, InMemoryEventScheduler,
)


async def get_user_name(context) -> str:
    return context.event_data["event_from_user"].first_name


schema = UIRouter(
    name="my_bot",
    version="1.0.0",
    initial_scene="welcome",
    scenes=[
        Scene(
            id="welcome",
            name="Приветствие",
            flags=[
                Flag(
                    name="name",
                    getter=FlagGetter(type="function", function="get_user_name"),
                ),
            ],
            default_content=MessageContent(
                text=DynamicContent(
                    type="template",
                    template=ContentTemplate(
                        template="Привет, {name}!",
                        flags=["name"],
                    ),
                ),
            ),
            default_keyboard=Keyboard(
                is_inline=True,
                buttons=[
                    [Button(text="О боте", callback_action="goto_about")],
                ],
            ),
            handlers=[
                Handler(
                    name="goto_about",
                    event_type=EventType.CALLBACK,
                    actions=[
                        ActionInstruction(
                            type=ActionType.GOTO_SCENE,
                            scene_id="about",
                            transition=TransitionType.EDIT_SMART,
                        ),
                    ],
                ),
            ],
        ),
        Scene(
            id="about",
            name="О боте",
            default_content=MessageContent(text="Собран с помощью aiogram-ui-router"),
            default_keyboard=Keyboard(
                buttons=[[Button(text="Назад", callback_action="go_back")]],
            ),
            handlers=[
                Handler(
                    name="go_back",
                    event_type=EventType.CALLBACK,
                    actions=[ActionInstruction(type=ActionType.BACK)],
                ),
            ],
        ),
    ],
)


async def main() -> None:
    bot = Bot(token="YOUR_TOKEN")
    dp = Dispatcher(storage=MemoryStorage())

    shared = SharedServices(
        navigation_storage=InMemoryNavigationStorage(),
        variable_repository=InMemoryVariableRepository(),
        event_bus=EventBus(),
        event_scheduler=InMemoryEventScheduler(event_callback=lambda e: None),
    )

    executor = UIRouterExecutor(schema=schema, shared=shared)
    executor.registry.getters.register("get_user_name", get_user_name)

    dp.include_router(executor.get_router())

    @dp.message(CommandStart())
    async def start(message: Message, **kwargs) -> None:
        await executor.start(message.from_user.id, message, **kwargs)

    await dp.start_polling(bot)


if __name__ == "__main__":
    asyncio.run(main())

Примеры

Смотрите директорию examples/:

Пример Описание
example.py Базовый бот: сцены, флаги, навигация
example_advanced.py Магазин с корзиной, формами, валидацией и бизнес-логикой

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiogram_ui_router-0.0.6.tar.gz (139.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiogram_ui_router-0.0.6-py3-none-any.whl (111.8 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_ui_router-0.0.6.tar.gz.

File metadata

  • Download URL: aiogram_ui_router-0.0.6.tar.gz
  • Upload date:
  • Size: 139.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for aiogram_ui_router-0.0.6.tar.gz
Algorithm Hash digest
SHA256 539ccd9b56049687797dad9250689ddedde8569111aa43a20fb2fa2da9ccdfce
MD5 92d2a4fcdc9abf5cccde8a818e02cd02
BLAKE2b-256 3920cff66819be5e4ca21081d33c92e2886ab52cd525d5af7d9d95ef24a43b03

See more details on using hashes here.

File details

Details for the file aiogram_ui_router-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: aiogram_ui_router-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 111.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for aiogram_ui_router-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 b3e5e16522fd1b6e95912dcb72f7417749032306184900d099fa3e778e6499a5
MD5 3dc1b1b04b070ed6c8127ed0e9d36b8d
BLAKE2b-256 11fde0d59664f7fb47ceb9ff8150addf579cd4ea28faac4ce2296a8fa6a8e7b7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page