Skip to main content

Binary CallbackData serialization for aiogram 3.x

Project description

aiogram-bincallback

Побитовая сериализация CallbackData для aiogram 3.x вместо текстового формата prefix:field1:field2. Каждое поле занимает фиксированное число бит - в лимит Telegram (64 байта) помещается больше данных, размер payload известен на этапе определения класса.

Установка

pip install aiogram-bincallback

Зависимости: aiogram>=3.0,<4.0, pydantic>=2.0,<3.0, Python 3.8+.

from enum import Enum, IntEnum

from aiogram_bincallback import BinaryCallbackData, bfield


class CbPrefix(IntEnum):
    ORDER = 1
    HOME = 2


class Status(str, Enum):
    PENDING = "pending"
    SHIPPED = "shipped"


class OrderCb(BinaryCallbackData, prefix=CbPrefix.ORDER.value, version=1):
    order_id: int = bfield(bits=16, signed=False)
    status: Status = bfield(bits=4)

prefix и version - аргументы наследования класса, не поля модели. Оба обязательны.

cb = OrderCb(order_id=42, status=Status.PENDING)
packed = cb.pack()
restored = OrderCb.unpack(packed)

OrderCb - одновременно pydantic.BaseModel и aiogram.CallbackData, используется в фильтрах как обычный CallbackData:

from aiogram import Router

router = Router()


@router.callback_query(OrderCb.filter())
async def handle_order(callback, callback_data: OrderCb):
    ...

Документация

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_bincallback-0.3.1.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

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

aiogram_bincallback-0.3.1-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file aiogram_bincallback-0.3.1.tar.gz.

File metadata

  • Download URL: aiogram_bincallback-0.3.1.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aiogram_bincallback-0.3.1.tar.gz
Algorithm Hash digest
SHA256 46790a5d9ba2511211eb7f5aefb522fa37b3100158f89157171d920435a08bec
MD5 38688386368b37da1b35c863da1efcea
BLAKE2b-256 d5137d9ac2f60f39408530390886085e1f2507f1590760120b4d6a5053eca63c

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiogram_bincallback-0.3.1.tar.gz:

Publisher: release.yml on boykopovar/aiogram-bincallback

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiogram_bincallback-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aiogram_bincallback-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a3657525b6fee96dd6acf45b979868c3a7104ce2026428c5bfd6212a20cfde9e
MD5 59012de70858c7b80e0316a5d9e94bc5
BLAKE2b-256 254d9d0c117093566fbfb8de94e9a2eb0cd6dc574002c675549eb043cba732db

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiogram_bincallback-0.3.1-py3-none-any.whl:

Publisher: release.yml on boykopovar/aiogram-bincallback

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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