AiogramX backdrop
20254 min read–
AiogramX logo

AiogramX

Drop-in inline-keyboard widgets for Telegram bots, one callback handler per widget type, with widget state kept on the server in a bounded LRU from FlipCache.

Status
Open source on PyPI
Widgets
5 inline + reply keyboards
Callback handlers
1 per widget type
Stack
Pythonaiogram 3FlipCacheGitHub Actions

Telegram bots don't have forms. If you want a user to pick a date, you build a calendar out of inline buttons: a row per week, arrows for months, a callback for every tap, and state to remember which month they're looking at.

I built that calendar by hand for the Tahrirchi bot's post scheduler, along with a time picker. In 2025 I rebuilt both for aiogram 3, added a paginator and checkboxes, and published them as AiogramX: open-source widgets you drop into a bot, hook a callback to, and you're done. It's on PyPI, and I wrote and maintain it alone.

What it looks like#

from datetime import date, timedelta

from aiogram import Router
from aiogram.filters import Command
from aiogram.types import CallbackQuery, Message
from aiogramx import Calendar

router = Router()
Calendar.register(router)


@router.message(Command("book"))
async def book(m: Message):
    async def on_select(cq: CallbackQuery, picked: date):
        await cq.message.edit_text(f"Booked for {picked:%d %B}")

    calendar = Calendar(
        max_range=timedelta(weeks=12),
        show_quick_buttons=True,
        on_select=on_select,
    )
    await m.answer("Pick a day", reply_markup=calendar.render_kb())

That's a working date picker: month navigation, a 12-week limit, quick buttons for today and tomorrow, and your function gets a real date.

The set:

  • Paginator: a static list or an async loader that fetches one page at a time. At most 94 items per page, because Telegram caps a keyboard at 100 buttons and the paginator needs 6 for navigation.
  • Calendar: month and year navigation, a maximum range, past or future dates blocked.
  • Checkbox: multi-select that hands back which options were ticked.
  • Time selector: two layouts, steps of 1 and 10 for hours and minutes, optional carry-over from minutes into hours, and a future-only mode.
  • ReplyKeyboardMeta: reply menus declared as class attributes instead of nested lists.

One handler per widget type#

What happens when a user taps an AiogramX buttonView

The interesting part is where the state lives. Telegram's callback data is tiny, 64 bytes at most, so it can't carry a calendar's state, let alone your on_select function.

So each widget instance stays on the server, and the button only carries a 4-character key. Calling register() once per widget class adds a single callback handler for that class. When a tap comes in, the handler looks the key up in a bounded LRUDict (from my FlipCache library), and the instance updates its keyboard or calls your function. Your callbacks can be closures over the message that created the widget, like on_select above.

A few details I like:

  • Mistakes fail early. A metaclass checks that every widget declares its callback data with a key field. If one doesn't, you get an error when the class is defined, not when a user taps a button in production.
  • The two time-selector layouts share one handler and one store, so both can live in the same bot behind one callback prefix.
  • Widgets work without registration too. Call process_cb yourself and you get a plain result back, for bots that want to own their routing.
  • Buttons that do nothing, like a page counter, answer with a cache time, so Telegram stops asking.

The tradeoffs#

Keeping state in memory is simple and fast, and it has costs I'd rather say out loud:

  • A restart forgets every open widget. Users who tap an old keyboard get a short "this widget has expired" message, and the keyboard is removed.
  • It's per process. If you run several copies of a bot, a tap has to reach the process that created the widget.
  • It's bounded. Each widget type keeps the 1,000 most recently used instances. Past that, the oldest expire.

For a bot that runs as one process, with menus that live for minutes, that's the right trade. For a big bot behind a load balancer, it isn't. Moving the storage to Redis through FlipCache would be the obvious next step.

What I fixed while writing this#

Rereading the code for this page turned up real problems:

  • The calendar crashed for most of the world. Its "expired" message looked up the user's Telegram language with no fallback, so anyone outside English, Russian and Uzbek got a KeyError instead of a message. It now falls back to English like the other widgets.
  • The docs were wrong. The README awaited Calendar.render_kb() and Checkbox.render_kb(), which are synchronous, so copy-pasted examples crashed. One example imported timedelta from asyncpg.

It also got its first tests, 94 of them, covering the language fallback for every widget.

Releases#

3.0 in May 2025, then 3.1 with the calendar and checkboxes, 3.1.2 with fixes and a demo bot, and 3.1.3 in June with reply keyboards, carry-over for the time selector, and automatic publishing to PyPI when I push a version tag. The version numbers line up with aiogram 3.

Stack#

Python, aiogram 3, FlipCache, GitHub Actions, pytest.

The best bot UI is one you only had to write once.

Need a similar system?

If you are building a bot, automation workflow, or platform feature, I can help with architecture and implementation.

Discuss a project

More projects

Related systems, decisions, and outcomes.