Skip to main content

aiogram-buffered-router

Debounced message batching for aiogram 3. Consecutive messages from the same chat are collected into one batch and passed to a single handler.

Install

uv add aiogram-buffered-router

Usage

from aiogram import Bot, Dispatcher, F
from aiogram.types import Message
from aiogram_buffered_router import BufferedRouter

router = BufferedRouter(name="chat", interval=1.0)


@router.buffered(F.text)
async def handle_batch(messages: list[Message], data: dict[str, object]) -> None:
    bot = data["bot"]
    texts = [message.text for message in messages if message.text]
    print(bot, texts)


dispatcher = Dispatcher()
dispatcher.include_router(router)

On shutdown flush the pending batches:

await router.aclose()

Options

  • interval — debounce window in seconds, restarted on nothing; the batch is dispatched interval seconds after the first message.
  • max_size — dispatch immediately once the batch reaches this size, 0 disables the limit.
  • keychat_key (bot + chat) or thread_key (bot + chat + topic, default), or any callable returning a hashable.

Handler exceptions are logged by the aiogram_buffered_router.buffer logger and never break polling.

Joining a batch that is already open

Sometimes only the first message of a batch is recognisable. A command takes an argument and the rest of the thought arrives as ordinary messages:

/system answer briefly
and in Russian

The second message matches no command filter, so it never reaches the buffer. Ask the buffer whether it is already collecting for that message's key and register a second entry into the same buffer:

buffer = router.attach(handle_system, key=thread_key)


async def collect(message: Message, **data: object) -> None:
    await buffer.add(message, data)


router.message.register(collect, Command("system"))
router.message.register(collect, buffer.is_buffering)

is_buffering uses the buffer's own key, and is true only while a batch actually holds messages — a dispatched batch lingers in memory for one idle window, and that does not count. Use attach rather than buffered here: buffered returns the handler, attach returns the buffer.

Keep in mind that the window is fixed from the first message of a batch and is not restarted by later ones, so a continuation only joins if it arrives inside interval.

License

MIT

Download files

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

Source Distribution

aiogram_buffered_router-0.2.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

aiogram_buffered_router-0.2.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_buffered_router-0.2.0.tar.gz.

File metadata

  • Download URL: aiogram_buffered_router-0.2.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","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":true}

File hashes

Hashes for aiogram_buffered_router-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b75ef7b1501e1956b9baa92c61fd2f52ad66821df7b1198974f4be67ce09dcf5
MD5 89a511d6799e095f38c238cc6e1476ed
BLAKE2b-256 52daff0dc040075980da1214a8a94ab0e0e6c77c8e540b6b7fc15385dcdf3995

See more details on using hashes here.

File details

Details for the file aiogram_buffered_router-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: aiogram_buffered_router-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","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":true}

File hashes

Hashes for aiogram_buffered_router-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abd04a5942e9a73df3487d904bd855906f5a089c92477456274f6a4a9bbe2e5b
MD5 4d46a20b19e918a64a6492d8811987b9
BLAKE2b-256 010aa5a5442f0cc5644398e58d807143cf83a83c6812ab04e7281bb9acdb7077

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 Sentry Error logging StatusPage Status page