Common filters#

pywa.filters.new(func, name=None)#

Factory function to create a filter from a function (sync or async).

Return type:

Filter

filters.message = <pywa.filters.message object>#
filters.callback_button = <pywa.filters.callback_button object>#
filters.callback_selection = <pywa.filters.callback_selection object>#
filters.message_status = <pywa.filters.message_status object>#
filters.flow_completion = <pywa.filters.flow_completion object>#
filters.chat_opened = <pywa.filters.chat_opened object>#
filters.call_connect = <pywa.filters.call_connect object>#
filters.call_terminate = <pywa.filters.call_terminate object>#
filters.call_status = <pywa.filters.call_status object>#
filters.call_permission_update = <pywa.filters.call_permission_update object>#
filters.user_marketing_preferences = <pywa.filters.user_marketing_preferences object>#
filters.template_status = <pywa.filters.template_status object>#
filters.template_category = <pywa.filters.template_category object>#
filters.template_quality = <pywa.filters.template_quality object>#
filters.template_components = <pywa.filters.template_components object>#
filters.phone_number_change = <pywa.filters.phone_number_change object>#
filters.identity_change = <pywa.filters.identity_change object>#
pywa.filters.sent_to(*, display_phone_number=None, phone_number_id=None)#

Filter for updates that are sent to the given phone number.

  • Use this filter when you choose not filter updates (e.g. WhatsApp(..., filter_updates=False)) so you can still filter for messages that are sent to specific phone numbers.

>>> sent_to(display_phone_number="+1 555-555-5555")
>>> sent_to(phone_number_id="123456789")
Return type:

Filter

filters.sent_to_me = <pywa.filters.sent_to_me object>#
pywa.filters.from_users(*numbers)#

Filter for updates that are sent from the given numbers.

>>> from_users("+1 555-555-5555", "972123456789")
Return type:

Filter

pywa.filters.from_countries(*prefixes)#

Filter for updates that are sent from the given country codes.

It is always recommended to restrict the countries that can use your bot. remember that you pay for every conversation that you reply to.

>>> from_countries("972", "1") # Israel and USA
Return type:

Filter

pywa.filters.matches(*strings, ignore_case=False)#

Filter for messages that are matching (==) any of the given strings.

The strings will be checked against the following fields:
  • Message: text, caption

  • CallbackButton: data

  • CallbackSelection: data

  • MessageStatus: tracker

  • FlowCompletion: token, body

>>> matches("Hello", "Hi")
Parameters:
  • *strings (str) – The strings to match.

  • ignore_case (bool) – Whether to ignore case when matching.

Return type:

Filter

pywa.filters.contains(*words, ignore_case=False)#

Filter for updates that contain any of the given words.

The words will be checked against the following fields:
  • Message: text, caption

  • CallbackButton: data

  • CallbackSelection: data

  • MessageStatus: tracker

  • FlowCompletion: token, body

>>> contains("Hello", "Hi", ignore_case=True)
Parameters:
  • *words (str) – The words to match.

  • ignore_case (bool) – Whether to ignore case when matching.

Return type:

Filter

pywa.filters.startswith(*prefixes, ignore_case=False)#

Filter for updates that start with any of the given prefixes.

The prefixes will be checked against the following fields:
  • Message: text, caption

  • CallbackButton: data

  • CallbackSelection: data

  • MessageStatus: tracker

  • FlowCompletion: token, body

>>> startswith("Hello", "Hi", ignore_case=True)
Parameters:
  • *prefixes (str) – The prefixes to match.

  • ignore_case (bool) – Whether to ignore case when matching.

Return type:

Filter

pywa.filters.endswith(*suffixes, ignore_case=False)#

Filter for updates that end with any of the given suffixes.

The suffixes will be checked against the following fields:
  • Message: text, caption

  • CallbackButton: data

  • CallbackSelection: data

  • MessageStatus: tracker

  • FlowCompletion: token, body

>>> endswith("Hello", "Hi", ignore_case=True)
Parameters:
  • *suffixes (str) – The suffixes to match.

  • ignore_case (bool) – Whether to ignore case when matching.

Return type:

Filter

pywa.filters.regex(*patterns, flags=0)#

Filter for updates that match any of the given regex patterns.

The patterns will be checked against the following fields:
  • Message: text, caption

  • CallbackButton: data

  • CallbackSelection: data

  • MessageStatus: tracker

  • FlowCompletion: token, body

>>> regex(r"Hello|Hi")
Parameters:
  • *patterns (str | Pattern) – The regex patterns to match.

  • flags (int) – The regex flags to use.

Return type:

Filter