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.
- 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
isFlowActionType.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
isFlowActionType.NAVIGATE
).mode (Literal[FlowStatus.PUBLISHED, FlowStatus.DRAFT]) β The mode of the flow.
FlowStatus.PUBLISHED
(default) orFlowStatus.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 usekw_only=True
. This is because we are limited to 200 characters in the callback data, so we need to use positional arguments. So object likeUser(id=123, name='John')
will be converted to123:John
.Currently, the following types are supported:
str
,int
,bool
,float
andenum.Enum
that inherits fromstr
(e.gclass State(str, Enum)
). You can also use all the types as Optional or Union (e.gOptional[int]
orUnion[int, None]
). the Union length must be 2 at most, and one of the types must beNone
. 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) andCallbackData.__callback_sep__
(ΒΆ
in the base class level, affects all child classes).When providing subclassed
CallbackData
as afactory
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 inButton
andSectionRow
and fortracker
βsalias of TypeVar(βCallbackDataTβ, bound=
Union
[str
,CallbackData
,Iterable
[CallbackData
|Any
]])
- pywa.handlers.CallbackDataFactoryT#
Type hint for the callback data factory.