Common filters#

pywa.filters.all_(*filters: Callable[[_Wa, _T], bool]) Callable[[_Wa, _T], bool]#

Returns True if all the given filters return True. Behaves like and between filters.

>>> all_(startswith("Hello"), endswith("Word"))
pywa.filters.any_(*filters: Callable[[_Wa, _T], bool]) Callable[[_Wa, _T], bool]#

Returns True if any of the given filters returns True. Behaves like or between filters.

>>> any_(contains("Hello"), regex(r"^World"))
pywa.filters.not_(fil: Callable[[_Wa, _T], bool]) Callable[[_Wa, _T], bool]#

Negates the given filter. Behaves like not

>>> not_(contains("Hello"))
pywa.filters.sent_to(*, display_phone_number: str = None, phone_number_id: str = 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")
filters.sent_to_me: _MessageFilterT = <function <lambda>>#
pywa.filters.from_users(*numbers: str) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

Filter for messages that are sent from the given numbers.

>>> from_users("+1 555-555-5555", "972123456789")
pywa.filters.from_countries(*prefixes: str | int) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

Filter for messages 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
pywa.filters.matches(*strings: str, ignore_case: bool = False) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

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

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

  • ignore_case – Whether to ignore case when matching.

pywa.filters.contains(*words: str, ignore_case: bool = False) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

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

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

  • ignore_case – Whether to ignore case when matching.

pywa.filters.startswith(*prefixes: str, ignore_case: bool = False) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

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

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

  • ignore_case – Whether to ignore case when matching.

pywa.filters.endswith(*suffixes: str, ignore_case: bool = False) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

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

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

  • ignore_case – Whether to ignore case when matching.

pywa.filters.regex(*patterns: str | re.Pattern, flags: int = 0) _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT#

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

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

  • flags – The regex flags to use.