Handler Objects#
- WhatsApp.add_handlers(*handlers: Handler) None#
Add handlers programmatically instead of using decorators.
Example
>>> from pywa.handlers import MessageHandler, CallbackButtonHandler >>> from pywa import filters as fil >>> print_message = lambda _, msg: print(msg) >>> wa = WhatsApp(...) >>> wa.add_handlers( ... MessageHandler(print_message, fil.text), ... CallbackButtonHandler(print_message), ... )
- Parameters:
handlers β The handlers to add.
- WhatsApp.add_flow_request_handler(handler: FlowRequestHandler) FlowRequestCallbackWrapper#
Add a flow request handler.
Example
>>> from pywa.handlers import FlowRequestHandler >>> from pywa import filters as fil >>> wa = WhatsApp(...) >>> wa.add_flow_request_handler( ... FlowRequestHandler( ... endpoint="/flow", ... callback=lambda _, flow: ..., ... ) ... ).add_handler(lambda _, flow: ..., screen="RECOMMENDED")
- Parameters:
handler β The flow request handler to add.
- Returns:
A wrapper to help split the logic of the handler.
- WhatsApp.remove_handlers(*handlers: Handler, silent: bool = False) None#
Remove handlers programmatically (not flow handlers).
If you registered callback with decorator (so uou donβt have reference to the handler object), you can use the
remove_callbacks()method to remove the callback.
Example
>>> from pywa.handlers import MessageHandler, CallbackButtonHandler >>> from pywa import filters as fil >>> print_message = lambda _, msg: print(msg) >>> wa = WhatsApp(...) >>> message_handler = MessageHandler(print_message, fil.text) >>> wa.add_handlers(message_handler) >>> wa.remove_handlers(message_handler)
- Parameters:
handlers β The handlers to remove.
silent β Whether to suppress the error if the handler is not registered (default:
False).
- Raises:
ValueError β If the handler is not registered and
silentisFalse.
- class pywa.handlers.MessageHandler(callback: _MessageCallback, filters: Filter = None, priority: int = 0)#
Handler for
Messageupdates (Text, media, etcβ¦).You can use the
on_message()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp, filters >>> wa = WhatsApp(...) >>> print_text_messages = lambda _, msg: print(msg) >>> wa.add_handlers(MessageHandler(print_text_messages, filters.text))
- class pywa.handlers.CallbackButtonHandler(callback: _CallbackButtonCallback, filters: Filter = None, factory: type[CallbackData] | None = None, priority: int = 0)#
Handler for
CallbackButtonupdates (user clicks on aButtonorQuickReplyButton).You can use the
on_callback_button()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp, filters >>> wa = WhatsApp(...) >>> print_btn = lambda _, btn: print(btn) >>> wa.add_handlers(CallbackButtonHandler(print_btn, filters.startswith('id:')))
- Parameters:
callback β The callback function. (Takes a
WhatsAppinstance and aCallbackButtonas positional arguments)filters β The filters to apply to the handler
factory β The
CallbackDataconstructor to use to construct the callback data.priority β The priority of the handler (default:
0)
- class pywa.handlers.CallbackSelectionHandler(callback: _CallbackSelectionCallback, filters: Filter = None, factory: type[CallbackData] | None = None, priority: int = 0)#
Handler for
CallbackSelectionupdates (user selects anSectionRowfrom aSectionList.).You can use the
on_callback_selection()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp, filters >>> wa = WhatsApp(...) >>> print_selection = lambda _, sel: print(sel) >>> wa.add_handlers(CallbackSelectionHandler(print_selection, filters.startswith('id:')))
- Parameters:
callback β The callback function. (Takes a
WhatsAppinstance and aCallbackSelectionas positional arguments)filters β The filters to apply to the handler
factory β The
CallbackDataconstructor to use to construct the callback data.priority β The priority of the handler (default:
0)
- class pywa.handlers.FlowCompletionHandler(callback: _FlowCompletionCallback, filters: Filter = None, priority: int = 0)#
Handler for
FlowCompletionupdates (Flow completion updates).You can use the
on_flow_completion()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_flow_completion = lambda _, msg: print(msg) >>> wa.add_handlers(FlowCompletionHandler(print_flow_completion))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aFlowCompletionas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.FlowRequestHandler(callback: _FlowRequestHandlerT, *, endpoint: str, acknowledge_errors: bool = True, private_key: str | None = None, private_key_password: str | None = None, request_decryptor: utils.FlowRequestDecryptor | None = None, response_encryptor: utils.FlowResponseEncryptor | None = None)#
A handler for Flow Data Exchange requests.
- Parameters:
callback β The function to call when a request is received (Takes a
pywa.WhatsAppinstance and apywa.types.FlowRequestas arguments and returns apywa.types.FlowResponse.endpoint β The endpoint to listen to (The endpoint uri you set to the flow. e.g
/feedback_flow).acknowledge_errors β Whether to acknowledge errors (The return value of the callback will be ignored, and pywa will acknowledge the error automatically).
private_key β The private key to use to decrypt the requests (Override the global
business_private_key).private_key_password β The password to use to decrypt the private key (Override the global
business_private_key_password).request_decryptor β The function to use to decrypt the requests (Override the global
flows_request_decryptor)response_encryptor β The function to use to encrypt the responses (Override the global
flows_response_encryptor)
- class pywa.handlers.MessageStatusHandler(callback: _MessageStatusCallback, filters: Filter = None, factory: type[CallbackData] | None = None, priority: int = 0)#
Handler for
MessageStatusupdates (Message status updates, e.g.sent,delivered,read).You can use the
on_message_status()decorator to register a handler for this type.
DO NOT USE THIS HANDLER WITHOUT FILTERS TO SEND MESSAGES, IT WILL CAUSE AN INFINITE LOOP!
Example
>>> from pywa import WhatsApp, types, filters >>> wa = WhatsApp(...) >>> print_failed_messages = lambda _, msg: print(msg) >>> wa.add_handlers(MessageStatusHandler(print_failed_messages, filters.failed))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aMessageStatusas positional arguments).filters β The filters to apply to the handler
factory β The
CallbackDataconstructor to use to construct thetrackerdata.priority β The priority of the handler (default:
0)
- class pywa.handlers.ChatOpenedHandler(callback: _ChatOpenedCallback, filters: Filter = None, priority: int = 0)#
Handler for
ChatOpenedupdates (Chat is opened).You can use the
on_chat_opened()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_chat_opened = lambda _, msg: print(msg) >>> wa.add_handlers(ChatOpenedHandler(print_chat_opened))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aChatOpenedas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.PhoneNumberChangeHandler(callback: _PhoneNumberChangeCallback, filters: Filter = None, priority: int = 0)#
Handler for
PhoneNumberChangeupdates (user changes their phone number).You can use the
on_phone_number_change()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_phone_number_change = lambda _, msg: print(msg) >>> wa.add_handlers(PhoneNumberChangeHandler(print_phone_number_change))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aPhoneNumberChangeas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.IdentityChangeHandler(callback: _IdentityChangeCallback, filters: Filter = None, priority: int = 0)#
Handler for
IdentityChangeupdates (user changes their identity).You can use the
on_identity_change()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_identity_change = lambda _, msg: print(msg) >>> wa.add_handlers(IdentityChangeHandler(print_identity_change))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aIdentityChangeas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.CallConnectHandler(callback: _CallConnectCallback, filters: Filter = None, priority: int = 0)#
Handler for
CallConnectupdates (Call connect updates).You can use the
on_call_connect()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_call_connect = lambda _, msg: print(msg) >>> wa.add_handlers(CallConnectHandler(print_call_connect))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aCallConnectas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.CallTerminateHandler(callback: _CallTerminateCallback, filters: Filter = None, priority: int = 0)#
Handler for
CallTerminateupdates (Call terminate updates).You can use the
on_call_terminate()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_call_terminate = lambda _, msg: print(msg) >>> wa.add_handlers(CallTerminateHandler(print_call_terminate))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aCallTerminateas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.CallStatusHandler(callback: _CallStatusCallback, filters: Filter = None, factory: type[CallbackData] | None = None, priority: int = 0)#
Handler for
CallStatusupdates (Call status updates, e.g.ringing,accepted,rejected, etc.).You can use the
on_call_status()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_call_status = lambda _, msg: print(msg) >>> wa.add_handlers(CallStatusHandler(print_call_status))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aCallStatusas positional arguments)filters β The filters to apply to the handler
factory β The
CallbackDataconstructor to use to construct thetrackerdata.priority β The priority of the handler (default:
0)
- class pywa.handlers.CallPermissionUpdateHandler(callback: _CallPermissionUpdateCallback, filters: Filter = None, priority: int = 0)#
Handler for
CallPermissionUpdateupdates (Call permission updates, e.g. when a user grants or revokes permission to receive calls).You can use the
on_call_permission_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_call_permission_update = lambda _, msg: print(msg) >>> wa.add_handlers(CallPermissionUpdateHandler(print_call_permission_update))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aCallPermissionUpdateas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.UserMarketingPreferencesHandler(callback: _UserMarketingPreferencesCallback, filters: Filter = None, priority: int = 0)#
Handler for
UserMarketingPreferencesupdates (User marketing preferences updates).You can use the
on_user_marketing_preferences()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_user_marketing_preferences = lambda _, msg: print(msg) >>> wa.add_handlers(UserMarketingPreferencesHandler(print_user_marketing_preferences))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aUserMarketingPreferencesas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.TemplateStatusUpdateHandler(callback: _TemplateStatusUpdateCallback, filters: Filter = None, priority: int = 0)#
Handler for
TemplateStatusUpdateupdates (Template status updates, e.g.approved,rejectedetc.).You can use the
on_template_status_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_template_status_update = lambda _, msg: print(msg) >>> wa.add_handlers(TemplateStatusUpdateHandler(print_template_status_update))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aTemplateStatusUpdateas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.TemplateCategoryUpdateHandler(callback: _TemplateCategoryUpdateCallback, filters: Filter = None, priority: int = 0)#
Handler for
TemplateCategoryUpdateupdates (Template category updates, e.g. fromUTILITYtoMARKETING).You can use the
on_template_category_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_template_category_update = lambda _, msg: print(msg) >>> wa.add_handlers(TemplateCategoryUpdateHandler(print_template_category_update))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aTemplateCategoryUpdateas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.TemplateQualityUpdateHandler(callback: _TemplateQualityUpdateCallback, filters: Filter = None, priority: int = 0)#
Handler for
TemplateQualityUpdateupdates (Template quality updates, e.g.GREENtoRED).You can use the
on_template_quality_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_template_quality_update = lambda _, msg: print(msg) >>> wa.add_handlers(TemplateQualityUpdateHandler(print_template_quality_update))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aTemplateQualityUpdateas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.TemplateComponentsUpdateHandler(callback: _TemplateComponentsUpdateCallback, filters: Filter = None, priority: int = 0)#
Handler for
TemplateComponentsUpdateupdates (Template components updates, e.g. when a template component is added or removed).You can use the
on_template_components_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_template_components_update = lambda _, msg: print(msg) >>> wa.add_handlers(TemplateComponentsUpdateHandler(print_template_components_update))
- Parameters:
callback β The callback function (Takes a
WhatsAppinstance and aTemplateComponentsUpdateas positional arguments)filters β The filters to apply to the handler
priority β The priority of the handler (default:
0)
- class pywa.handlers.RawUpdateHandler(callback: _RawUpdateCallback, filters: Filter = None, priority: int = 0)#
Handler for raw updates (dicts) from the webhook.
You can use the
on_raw_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_raw_update = lambda _, update: print(update) >>> wa.add_handlers(RawUpdateHandler(print_raw_update))