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 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,floatandEnumthat inherits fromstr(e.gclass 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) andCallbackData.__callback_sep__(In the base class level, affects all child classes).When providing subclassed
CallbackDataas afactoryparameter 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]])