Callback Button#
- class pywa.types.CallbackButton#
Represents a callback button (Incoming update when user clicks on
Buttonor onQuickReplyButton’s template).CallbackButtonis a generic class, so when providing afactoryparameter in callback handlers, you can specify the type of the factory to get autocomplete in thedataattribute.Here is an example:
>>> from pywa.types import CallbackData >>> import dataclasses # Use dataclass to get free ordered __init__ >>> @dataclasses.dataclass(frozen=True, slots=True) # Do not use kw_only=True >>> class UserData(CallbackData): # Subclass CallbackData ... id: int ... name: str ... admin: bool
>>> from pywa import WhatsApp >>> from pywa.types import Button, CallbackButton >>> 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="david", admin=True), ... ) ... ], ... ) # Here ^^^ we use the UserData class as the callback data
>>> @wa.on_callback_button( ... factory=UserData ... ) # Use the factory parameter to convert the callback data ... def on_user_data( ... _: WhatsApp, btn: CallbackButton[UserData] ... ): # For autocomplete ... if btn.data.admin: ... print(btn.data.id) # Access the data object as an attribute
- Variables:
id – The ID of the message.
metadata – The metadata of the message (to which phone number it was sent).
type (pywa.types.others.MessageType) – The message type (
MessageType.INTERACTIVEforButtonpresses orMessageType.BUTTONforQuickReplyButtonclicks).from_user – The user who sent the message.
timestamp – The timestamp when the message was sent (in UTC).
reply_to_message (pywa.types.others.ReplyToMessage | None) – The message to which this callback button is a reply to.
Nonewhen a templateQuickReplyButtonis clicked (WhatsApp omits thecontextfield for templatebuttonmessages).data (pywa.types.callback._CallbackDataT) – The data of the button (the
callback_dataparameter you provided inButtonorQuickReplyButton).title (str) – The title of the button.
shared_data – Shared data between handlers.
- property is_quick_reply: bool#
Check if the callback button is click at
QuickReplyButton(template button).- Returns:
True if the callback button is a quick reply button, False otherwise.
- Return type: