Skip to main content

Widgets and tools for Aiogram bots

Project description

AiogramX

PyPI version License: MIT Downloads GitHub stars

⭐ If you find this project useful, please consider starring the repository! It helps others discover the project and motivates me to keep improving it with new features and updates.

AiogramX (Aiogram eXtensions) is a modular collection of widgets and tools for building advanced Telegram bots using Aiogram. It simplifies the creation of user interfaces with inline keyboards, time selectors, calendars, paginators, and checkboxes — all with a clean API and optional callback handling.


Quick Links

AiogramX Logo

✨ Features

  • Paginator with lazy loading support
  • Interactive calendar with date selection
  • Versatile checkbox component
  • Time selection widgets (grid and modern)
  • Static class-style reply keyboard builder
  • Easy integration and custom callbacks
  • Full compatibility with aiogram 3.x

🚀 Why AiogramX?

AiogramX is designed with performance and scalability in mind. Unlike other widget libraries, it avoids common architectural pitfalls that can degrade your bot’s performance over time.

✅ Efficient Callback Handling

Most other libraries create a new callback handler per widget instance, which leads to:

  • 📈 Handler bloat: Thousands of handlers pile up as users interact with widgets
  • 🐢 Slowdowns: Aiogram has to iterate over a large handler list on every callback
  • 🗑️ Memory waste: Unused handlers remain registered, even after widgets are discarded

🧠 AiogramX does it differently

AiogramX uses an internal LRU (Least Recently Used) storage mechanism (from FlipCache) to manage widget instances:

  • 🔁 Single callback handler per widget type (e.g. TimeSelector, Paginator)
  • 🧹 Old widget instances are automatically evicted from memory after a limit (default: 1000)
  • 🧵 Cleaner, more predictable handler lifecycle
  • Improved performance and faster dispatching of callbacks

This architecture keeps your bot responsive, even under heavy usage.

📦 Installation

pip install aiogramx

📚 Components

📄 Paginator

Basic usage example

from aiogramx import Paginator

Paginator.register(dp)

def get_buttons():
    return [
        InlineKeyboardButton(text=f"Element {i}", callback_data=f"elem {i}")
        for i in range(10_000)
    ]


@dp.message(Command("pages"))
async def pages_handler(m: Message):
    pg = Paginator(per_page=15, per_row=2, data=get_buttons())
    await m.answer(text="Pagination Demo", reply_markup=await pg.render_kb())


@dp.callback_query(F.data.startswith("elem "))
async def handle_buttons(c: CallbackQuery):
    await c.message.edit_text(text=f"Selected elem with callback '{c.data}'")

Example with on_select and on_back callback functions

from aiogramx import Paginator

Paginator.register(dp)

def get_buttons():
    return [
        InlineKeyboardButton(text=f"Element {i}", callback_data=f"elem {i}")
        for i in range(10_000)
    ]


@dp.message(Command("pages"))
async def pages_handler(m: Message):
    async def on_select(c: CallbackQuery, data: str):
        await c.answer(text=f"Selected '{data}'")

    async def on_back(c: CallbackQuery):
        await c.message.edit_text("Ok")

    pg = Paginator(
        per_page=15, per_row=2, data=get_buttons(), on_select=on_select, on_back=on_back
    )
    await m.answer(text="Pagination Demo", reply_markup=await pg.render_kb())

Example using lazy functions

from aiogramx import Paginator

Paginator.register(dp)

async def get_buttons_lazy(cur_page: int, per_page: int) -> list[InlineKeyboardButton]:
    results = fetch_results_from_somewhere(cur_page, per_page)

    return [
        InlineKeyboardButton(text=row["value"], callback_data=f"id|{row['id']}")
        for row in results
    ]


async def get_count_lazy() -> int:
    async with pool.acquire() as conn:
        return await conn.fetchval("SELECT COUNT(*) FROM test_data")


async def handle_data_select(c: CallbackQuery, data: str):
    await c.message.edit_text(text=f"Selected callback '{data}'")


async def handle_back(c: CallbackQuery):
    await c.message.edit_text("Pagination closed")


@dp.message(Command("pages"))
async def pages_handler(m: Message):
    p = Paginator(
        per_page=11,
        per_row=3,
        lazy_data=get_buttons_lazy,
        lazy_count=get_count_lazy,
        on_select=handle_data_select,
        on_back=handle_back,
    )

    await m.answer(text="Pagination Demo", reply_markup=await p.render_kb())

📅 Calendar

Usage example

from aiogramx import Calendar

Calendar.register(dp)

@dp.message(Command("calendar"))
async def calendar_handler(m: Message):
    async def on_select(cq: CallbackQuery, date_obj: date):
        await cq.message.edit_text(
            text="Selected date: " + date_obj.strftime("%Y-%m-%d")
        )

    async def on_back(cq: CallbackQuery):
        await cq.message.edit_text(text="Canceled")

    c = Calendar(
        max_range=timedelta(weeks=12),
        show_quick_buttons=True,
        on_select=on_select,
        on_back=on_back,
    )
    await m.answer(text="Calendar Demo", reply_markup=await c.render_kb())

☑️ Checkbox

Basic usage

from aiogramx import Checkbox

Checkbox.register(dp)

@dp.message(Command("checkbox2"))
async def checkbox2_handler(m: Message):
    ch = Checkbox(["Option 1", "Option 2", "Option 3"])
    await m.answer(text="Checkbox Demo 2", reply_markup=await ch.render_kb())

Advanced usage with callback functions

from aiogramx import Checkbox

Checkbox.register(dp)

@dp.message(Command("checkbox"))
async def checkbox_handler(m: Message):
    async def on_select(cq: CallbackQuery, data: dict):
        flag_map = {True: "✅", False: "❌"}

        await cq.message.edit_text(
            text=str(
                "".join([f"{k}: {flag_map[v['flag']]}\n" for k, v in data.items()])
            )
        )

    async def on_back(cq: CallbackQuery):
        await cq.message.edit_text(text="You pressed the back button!")

    options = {
        "video_note": {
            "text": "🎞",
            "flag": True,
        },
        "voice": {
            "text": "🔉",
            "flag": False,
        },
        "test": None,
        "other": {},
    }

    ch = Checkbox(
        options=options,
        on_select=on_select,
        on_back=on_back,
    )
    await m.answer(text="Checkbox Demo", reply_markup=await ch.render_kb())

⏰ Time Selectors

Basic usage

from aiogramx import TimeSelectorGrid

TimeSelectorGrid.register(dp)

@dp.message(Command("grid"))
async def grid_kb_handler(m: Message):
    ts_grid = TimeSelectorGrid()
    await m.answer(text="Time Selector Grid", reply_markup=ts_grid.render_kb())

Advanced usage with callback functions

from aiogramx import TimeSelectorModern

TimeSelectorModern.register(dp)

@dp.message(Command("modern"))
async def modern_ts_handler(m: Message):
    async def on_select(c: CallbackQuery, time_obj: time):
        await c.message.edit_text(text=f"Time selected: {time_obj.strftime('%H:%M')}")
        await c.answer()

    async def on_back(c: CallbackQuery):
        await c.message.edit_text(text="Operation Canceled")
        await c.answer()

    ts_modern = TimeSelectorModern(
        allow_future_only=True,
        on_select=on_select,
        on_back=on_back,
        lang=m.from_user.language_code,
    )

    await m.answer(
        text="Time Selector Modern",
        reply_markup=ts_modern.render_kb(offset_minutes=5),
    )

🔘 Static Reply Keyboard Builder

A convenient way to define static reply menus using class-style syntax.

Usage example:

class ExampleKB(metaclass=ReplyKeyboardMeta):
    B1 = "Button 1"
    B2 = "Button 2"
    B3 = "Button 3"
    B4 = "Button 4"
    HELP = "🆘 Help"

    __LAYOUT__ = [
        [B1, B2],
        [HELP],
        [B4, B3],
    ]


@dp.message(Command("keyboard"))
async def reply_keyboard(m: Message):
    await m.answer("📋 Reply Keyboard Example", reply_markup=ExampleKB.kb)


@dp.message(F.text.in_(ExampleKB))
async def example_kb_handler(m: Message):
    if m.text == ExampleKB.B1:
        await m.answer("B1 is pressed!")

    elif m.text == ExampleKB.B2:
        await m.answer(f"'{ExampleKB.B2}' is pressed!")

    elif m.text == ExampleKB.B3:
        await m.answer(f"{ExampleKB.B3!r} is pressed!")

    elif m.text == ExampleKB.B4:
        await m.answer("B4 is pressed!")

    elif m.text == ExampleKB.HELP:
        await m.answer("Help message")

Features:

  • Button labels are defined as class attributes.
  • Optional __LAYOUT__ controls button arrangement, defaults to single button per row
  • Access ExampleKB.kb to get the ready-to-use ReplyKeyboardMarkup.
  • Iterate or check membership via in, for, or indexing (ExampleKB[0]).

For more usage examples and details, see examples

🧪 Contributing

Contributions are welcome! If you'd like to add new widgets or improve existing ones, feel free to open issues or submit pull requests.

📜 License

This project is licensed under the MIT License. See the LICENSE file for more information.

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

aiogramx-3.1.3.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

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

aiogramx-3.1.3-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file aiogramx-3.1.3.tar.gz.

File metadata

  • Download URL: aiogramx-3.1.3.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for aiogramx-3.1.3.tar.gz
Algorithm Hash digest
SHA256 3710f93103b61dd5698b240b3ec1c17a3e898395127d83c3010443439acb8901
MD5 02276714d6f81ec7d3c6ce33d6df3e2b
BLAKE2b-256 295478c4794ab8f648efcd695f96039f72a66f177ced7930a70dfa946bba0da0

See more details on using hashes here.

File details

Details for the file aiogramx-3.1.3-py3-none-any.whl.

File metadata

  • Download URL: aiogramx-3.1.3-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for aiogramx-3.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9c072ac91643e2e8254f4d702c71ec067ac891aa83579c1f800e33b861dd175e
MD5 c65b205b7a551a93fc6307ec8b5a50e3
BLAKE2b-256 b7d142dd6e860e2db8f3d7f66010e37314fd87cc1edf8c992b50a09d28788542

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