Message Filters#

Usefully filters to use in your handlers.

>>> from pywa import filters as fil
>>> from pywa import WhatsApp, types
>>> wa = WhatsApp(...)
>>> @wa.on_message(fil.startswith("Hi", "Hello", ignore_case=True))
... def on_hi_msg(_: WhatsApp, m: types.Message):
...     print("This is a welcome message!")
filters.forwarded: _MessageFilterT = <function <lambda>>#
filters.forwarded_many_times: _MessageFilterT = <function <lambda>>#
filters.reply: _MessageFilterT = <function <lambda>>#
pywa.filters.replays_to(*msg_ids: str) _MessageFilterT#

Filter for messages that reply to any of the given message ids.

>>> replays_to("wamid.HBKHUIyNTM4NjAfiefhwojfMTNFQ0Q2MERGRjVDMUHUIGGA=")
filters.has_referred_product: _MessageFilterT = <function <lambda>>#

pywa.filters.text#

alias of _TextFilters

static text.length(*lengths: tuple[int, int]) _MessageFilterT#

Filter for text messages that have a length between any of the given ranges.

>>> text.length((1, 10), (50, 100))
Parameters:

*lengths – The length range/s to filter for (e.g. (1, 10), (50, 100)).

text.is_command(m)#
static text.command(*cmds: str, prefixes: str | Iterable[str] = '/!', ignore_case: bool = False) _MessageFilterT#

Filter for text messages that are commands.

>>> text.command("start", "hello", prefixes="/", ignore_case=True)
Parameters:
  • *cmds – The command/s to filter for (e.g. β€œstart”, β€œhello”).

  • prefixes – The prefix/s to filter for (default: β€œ/!”, i.e. β€œ/start”).

  • ignore_case – Whether to ignore case when matching (default: False).


pywa.filters.media#

alias of _MediaFilters

classmethod media.mimetypes(*mimetypes: str) _MessageFilterT#

Filter for media messages that match any of the given mime types.

>>> media.mimetypes("application/pdf", "image/png")
>>> video.mimetypes("video/mp4")
>>> audio.mimetypes("audio/mpeg")
classmethod media.extensions(*extensions: str) _MessageFilterT#

Filter for media messages that match any of the given extensions.

>>> media.extensions(".pdf", ".png")
>>> video.extensions(".mp4")
>>> document.extensions(".pdf")
pywa.filters.image#

alias of _ImageFilters

image.has_caption: _MessageFilterT = <bound method _MediaWithCaptionFilters._has_caption of <class 'pywa.filters._ImageFilters'>>#

Filter for media messages that have a caption.

>>> filters.image.has_caption
>>> filters.video.has_caption
pywa.filters.video#

alias of _VideoFilters

video.has_caption: _MessageFilterT = <bound method _MediaWithCaptionFilters._has_caption of <class 'pywa.filters._VideoFilters'>>#

Filter for media messages that have a caption.

>>> filters.image.has_caption
>>> filters.video.has_caption
pywa.filters.audio#

alias of _AudioFilters

audio.voice: _MessageFilterT = <function _AudioFilters.<lambda>>#

Filter for audio messages that are voice notes.

>>> filters.audio.voice
audio.audio: _MessageFilterT = <function _AudioFilters.<lambda>>#

Filter for audio messages that are audio files.

>>> filters.audio.audio
pywa.filters.document#

alias of _DocumentFilters

document.has_caption: _MessageFilterT = <bound method _MediaWithCaptionFilters._has_caption of <class 'pywa.filters._DocumentFilters'>>#

Filter for media messages that have a caption.

>>> filters.image.has_caption
>>> filters.video.has_caption
pywa.filters.sticker#

alias of _StickerFilters

sticker.animated: _MessageFilterT = <function _StickerFilters.<lambda>>#

Filter for animated sticker messages.

>>> filters.sticker.animated
sticker.static: _MessageFilterT = <function _StickerFilters.<lambda>>#

Filter for static sticker messages.

>>> filters.sticker.static

pywa.filters.reaction#

alias of _ReactionFilters

reaction.added: _MessageFilterT = <function _ReactionFilters.<lambda>>#

Filter for reaction messages that were added.

>>> filters.reaction.added
reaction.removed: _MessageFilterT = <function _ReactionFilters.<lambda>>#

Filter for reaction messages that were removed.

>>> filters.reaction.removed
static reaction.emojis(*emojis: str) _MessageFilterT#

Filter for custom reaction messages. pass emojis as strings.

>>> reaction.emojis("πŸ‘","πŸ‘Ž")

pywa.filters.location#

alias of _LocationFilters

location.current_location: _MessageFilterT = <function _LocationFilters.<lambda>>#

Filter for location messages that are the current location of the user and not just selected manually.

>>> filters.location.current_location
static location.in_radius(lat: float, lon: float, radius: float | int) _MessageFilterT#

Filter for location messages that are in a given radius.

>>> location.in_radius(lat=37.48508108998884, lon=-122.14744733542707, radius=1)
Parameters:
  • lat – Latitude of the center of the radius.

  • lon – Longitude of the center of the radius.

  • radius – Radius in kilometers.


pywa.filters.contacts#

alias of _ContactsFilters

contacts.has_wa: _MessageFilterT = <function _ContactsFilters.<lambda>>#

Filter for contacts messages that have a WhatsApp account.

>>> filters.contacts.has_wa
static contacts.count(min_count: int, max_count: int) _MessageFilterT#

Filter for contacts messages that have a number of contacts between min_count and max_count.

>>> contacts.count(1, 1) # ensure only 1 contact
>>> contacts.count(1, 5) # between 1 and 5 contacts
static contacts.phones(*phones: str) _MessageFilterT#

Filter for contacts messages that have the given phone number/s.

>>> contacts.phones("+1 555-555-5555","972123456789")

pywa.filters.order#

alias of _OrderFilters

static order.price(min_price: float, max_price: float) _MessageFilterT#

Filter for order messages that have a total price between min_price and max_price.

Parameters:
  • min_price – Minimum price.

  • max_price – Maximum price.

>>> order.price(1, 100) # total price between 1 and 100
static order.count(min_count: int, max_count: int) _MessageFilterT#

Filter for order messages that have a number of items between min_count and max_count.

Parameters:
  • min_count – Minimum number of items.

  • max_count – Maximum number of items.

>>> order.count(1, 5) # between 1 and 5 items
static order.has_product(*skus: str) _MessageFilterT#

Filter for order messages that have the given product/s.

Parameters:

*skus – The products SKUs.

>>> order.has_product("pizza_1","pizza_2")

pywa.filters.unsupported#

alias of _UnsupportedMsgFilters