Handler Objects#
- WhatsApp.add_handlers(*handlers: Handler)#
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), ... )
- class pywa.handlers.MessageHandler(handler: Callable[[WhatsApp, Message], Any], *filters: Callable[[WhatsApp, Message], bool])#
Handler for incoming
pywa.types.Message.You can use the
on_message()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp, filters as fil >>> wa = WhatsApp(...) >>> print_text_messages = lambda _, msg: print(msg) >>> wa.add_handlers(MessageHandler(print_text_messages, fil.text))
- Parameters:
handler β The handler function (gets the
pywa.WhatsAppinstance and apywa.types.Messageas arguments)*filters β The filters to apply to the handler (gets a
pywa.WhatsAppinstance and apywa.types.Messageand returns abool)
- class pywa.handlers.CallbackButtonHandler(handler: Callable[[WhatsApp, CallbackButton], Any], *filters: Callable[[WhatsApp, CallbackButton], bool], factory: CallbackDataT = <class 'str'>)#
Handler for callback buttons (User clicks on a
pywa.types.Button).You can use the
on_callback_button()decorator to register a handler for this type.
IMPORTANT: If
factoryprovided, The filters have no access to the constructed callback data, only the raw string (When subclassingpywa.types.CallbackDataas the factory, a matching filter is automatically added, so there is no need to add it manually).Example
>>> from pywa import WhatsApp, filters as fil >>> wa = WhatsApp(...) >>> print_btn = lambda _, btn: print(btn) >>> wa.add_handlers(CallbackButtonHandler(print_btn, fil.callback.data_startswith('id:')))
- Parameters:
handler β The handler function (gets the WhatsApp instance and the callback as arguments)
*filters β The filters to apply to the handler (gets a
pywa.WhatsAppinstance and apywa.types.CallbackButtonand returns abool)factory β The constructor/s to use to construct the callback data (default:
str).
- class pywa.handlers.CallbackSelectionHandler(handler: Callable[[WhatsApp, CallbackSelection], Any], *filters: Callable[[WhatsApp, CallbackSelection], bool], factory: CallbackDataT = <class 'str'>)#
Handler for callback selections (User selects an option from
pywa.types.SectionList).You can use the
on_callback_selection()decorator to register a handler for this type.
IMPORTANT: If
factoryprovided, The filters have no access to the constructed callback data, only the raw string (When subclassingpywa.types.CallbackDataas the factory, a matching filter is automatically added, so there is no need to add it manually).Example
>>> from pywa import WhatsApp, filters as fil >>> wa = WhatsApp(...) >>> print_selection = lambda _, sel: print(sel) >>> wa.add_handlers(CallbackSelectionHandler(print_selection, fil.callback.data_startswith('id:')))
- Parameters:
handler β The handler function. (gets a
pywa.WhatsAppinstance and apywa.types.CallbackSelectionas arguments)*filters β The filters to apply to the handler. (gets a
pywa.WhatsAppinstance and apywa.types.CallbackSelectionand returns abool)factory β The constructor/s to use to construct the callback data (default:
str).
- class pywa.handlers.MessageStatusHandler(handler: Callable[[WhatsApp, MessageStatus], Any], *filters: Callable[[WhatsApp, MessageStatus], bool])#
Handler for
pywa.types.MessageStatusupdates (Message is sent, delivered, read, failed, etcβ¦).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, filters as fil >>> wa = WhatsApp(...) >>> print_failed_messages = lambda _, msg: print(msg) >>> wa.add_handlers(MessageStatusHandler(print_failed_messages, fil.message_status.failed))
- Parameters:
handler β The handler function (gets a
pywa.WhatsAppinstance and apywa.types.MessageStatusas arguments)*filters β The filters to apply to the handler (gets a
pywa.WhatsAppinstance and apywa.types.MessageStatusand returns abool)
- class pywa.handlers.TemplateStatusHandler(handler: Callable[[WhatsApp, TemplateStatus], Any], *filters: Callable[[WhatsApp, TemplateStatus], bool])#
Handler for
pywa.types.TemplateStatusupdates (Template message is approved, rejected etcβ¦).You can use the
on_template_status()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp, filters as fil >>> wa = WhatsApp(...) >>> print_template_status = lambda _, msg: print(msg) >>> wa.add_handlers(TemplateStatusHandler( ... print_template_status, ... fil.template_status.on_event(TemplateStatus.TemplateEvent.APPROVED) ... ))
- Parameters:
handler β The handler function (gets a
pywa.WhatsAppinstance and apywa.types.TemplateStatusas arguments)*filters β The filters to apply to the handler (gets a
pywa.WhatsAppinstance and apywa.types.TemplateStatusand returns abool)
- class pywa.handlers.RawUpdateHandler(handler: Callable[[WhatsApp, dict], Any], *filters: Callable[[WhatsApp, dict], bool])#
A raw update handler.
This handler will be called for EVERY update received from WhatsApp, even if itβs not sent to the client phone number.
You can use the
on_raw_update()decorator to register a handler for this type.
Example
>>> from pywa import WhatsApp >>> wa = WhatsApp(...) >>> print_updates = lambda _, data: print(data) >>> wa.add_handlers(RawUpdateHandler(print_updates))