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.ButtonUrl#

Represents a button in the bottom of the message that opens a URL.

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

  • url (str) – The URL to open when the user clicks on the button.

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 choice row in a section rows.

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.FlowButton#

Represents a button that opens a flow.

Variables:
  • title (str) – Text on the CTA button. e.g SignUp, Up to 20 characters, no emojis)

  • flow_id (str | int) – Unique ID of the Flow provided by WhatsApp.

  • flow_token (str) – Flow token generated by the business to serve as an identifier for data exchange.

  • flow_message_version (int | float | str | Literal[<Version.FLOW_MSG: '3'>]) – Version of the flow message. Default is the latest version.

  • flow_action_type (Literal[FlowActionType.NAVIGATE, FlowActionType.DATA_EXCHANGE] | None) – Type of action to be performed when the user clicks on the CTA button.

  • flow_action_screen (str | None) – The ID of the first Screen. Required when flow_action_type is FlowActionType.NAVIGATE (default).

  • flow_action_payload (dict[str, Any] | None) – The payload to send when the user clicks on the button (optional, only when flow_action_type is FlowActionType.NAVIGATE).

  • mode (Literal[FlowStatus.PUBLISHED, FlowStatus.DRAFT]) – The mode of the flow. FlowStatus.PUBLISHED (default) or FlowStatus.DRAFT (for testing).

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.Enum that inherits from str (e.g class State(str, Enum)). You can also use all the types as Optional or Union (e.g Optional[int] or Union[int, None]). the Union length must be 2 at most, and one of the types must be None. All fields can be with default values.

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.types import CallbackData
>>> 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
...     name: str | None
...     admin: bool = False
>>> from pywa import WhatsApp
>>> from pywa.types import Button
>>> 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, name='John', 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 callback_data parameter in Button and SectionRow and for tracker’s

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

pywa.handlers.CallbackDataFactoryT#

Type hint for the callback data factory.


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).