Payload Data

Payload Data is used to generate a structured payload using the pydantic model.

class aiosend.PayloadData(**data)

Base payload data class.

classmethod filter(rule=None)

Generate a filter for payload with rule.

Parameters:

rule (MagicFilter | None) – magic rule

Return type:

PayloadDataFilter

pack()

Generate payload data string.

Return type:

str

classmethod unpack(value)

Parse payload data string.

Return type:

Self

Usage example

Define a subclass of PayloadData. The keyword prefix is required to specify the prefix, and the sep argument can be provided to define the separator (default is :).

class MyData(PayloadData, prefix="md"):
    foo: str
    bar: int

Now you can create an instance of this class, pack it into a string, and then pass it in the Invoice payload.

my_data = MyData(foo="foo", bar=123).pack()
await cp.create_invoice(1, "USDT", payload=my_data)

To handle an invoice payment event, you need to declare a handler with a PayloadData filter. You can access an instance of your PayloadData via the handler’s named argument payload_data.

@cp.invoice_paid(MyData.filter())
async def handler(invoice: Invoice, payload_data: MyData) -> None:
    print(invoice.amount, payload_data.foo)

Also you can filter events by specific rules using magic filters from aiogram 3.x.

from magic_filter import F # or from aiogram import F

@cp.invoice_paid(MyData.filter(F.bar == 123))
async def handler(invoice: Invoice, payload_data: MyData) -> None:
    print(invoice.amount, payload_data.foo)