Keyboard#

class pywa.types.callback.Button#

Represents a button in the button list.

Variables:
  • title (str) – The title of the button (up to 20 characters).

  • callback_data (pywa.types.callback.CallbackDataT) – The data to send when the user clicks on the button (up to 256 characters, for complex data You can use CallbackData).

class pywa.types.callback.SectionList#

Represents a section list in an interactive message.

Variables:
  • button_title (str) – The title of the button that opens the section list (up to 20 characters).

  • sections (Iterable[pywa.types.callback.Section]) – The sections in the section list (at least 1, no more than 10).

class pywa.types.callback.Section#

Represents a section in a section list.

Variables:
  • title (str) – The title of the section (up to 24 characters).

  • rows (Iterable[pywa.types.callback.SectionRow]) – The rows in the section (at least 1, no more than 10).

class pywa.types.callback.SectionRow#

Represents a row in a section.

Variables:
  • title (str) – The title of the row (up to 24 characters).

  • callback_data (pywa.types.callback.CallbackDataT) – The payload to send when the user clicks on the row (up to 200 characters, for complex data You can use CallbackData).

  • description (str | None) – The description of the row (optional, up to 72 characters).

class pywa.types.callback.CallbackData#

Base class for all callback data classes. Subclass this class to create a type-safe callback data class.

If you use dataclasses, which is the recommended way (You get free ordered __init__ and extra features), you should not use kw_only=True. This is because we are limited to 200 characters in the callback data, so we need to use positional arguments. So object like User(id=123, name='John') will be converted to 123:John.

Currently, the following types are supported: str, int, bool, float and Enum that inherits from str (e.g class State(str, Enum)). You probably won’t need more than that to pass state and context in the program.

The characters : and ; cannot be used when sending callbacks, because they are used as separators. You can change the separators by overriding __callback_data_sep__ (for individual objects) and CallbackData.__callback_sep__ (In the base class level, affects all child classes).

When providing subclassed CallbackData as a factory parameter in callback handlers, a basic matching filter (startswith(callback_id)) will be added automatically. So no need to create one yourself.

Example

>>> from pywa import WhatsApp
>>> from pywa.types import CallbackData, Button
>>> from dataclasses import dataclass # Use dataclass to get free ordered __init__
>>> @dataclass(frozen=True, slots=True) # Do not use kw_only=True
>>> class UserData(CallbackData): # Subclass CallbackData
...     id: int
...     admin: bool
>>> wa = WhatsApp(...)
>>> wa.send_message(
...     to='972987654321',
...     text='Click the button to get the user',
...     buttons=[Button(title='Get user', callback_data=UserData(id=123, admin=True))]
... )
>>> @wa.on_callback_button(factory=UserData) # Use the factory parameter to convert the callback data
... def on_user_data(client: WhatsApp, btn: CallbackButton[UserData]): # For autocomplete
...    if btn.data.admin: print(btn.data.id) # Access the data object as an attribute
class pywa.types.callback.CallbackDataT#

Type hint for factory parameter in callback handlers.

alias of TypeVar(β€˜CallbackDataT’, bound=Union[CallbackData, Iterable[type[CallbackData]], str, Callable[[str], Any]])


class pywa.types.others.ProductsSection#

Represents a section in a section list.

Variables:
  • title (str) – The title of the products section (up to 24 characters).

  • skus (Iterable[str]) – The SKUs of the products in the section (at least 1, no more than 30).