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.text.command("start"))
... def on_hi_msg(_: WhatsApp, m: types.Message):
...     print("This is a welcome message!")
filters.forwarded = <pywa.filters.forwarded object>#
filters.forwarded_many_times = <pywa.filters.forwarded_many_times object>#
filters.reply = <pywa.filters.reply object>#
pywa.filters.replays_to(*msg_ids: str) Filter#

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

>>> replays_to("wamid.HBKHUIyNTM4NjAfiefhwojfMTNFQ0Q2MERGRjVDMUHUIGGA=")
filters.has_referred_product = <pywa.filters.has_referred_product object>#
pywa.filters.text()#

Filter for text messages.

pywa.filters.is_command()#

Filter for text messages that are commands (start with / or !).

>>> filters.is_command
pywa.filters.command(*cmds: str, prefixes: str | Iterable[str] = '/!', ignore_case: bool = False) Filter#

Filter for text messages that are commands.

>>> 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()#

Filter for media messages (images, videos, documents, audio, stickers).

pywa.filters.mimetypes(*mmtps: str) Filter#

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

>>> mimetypes("application/pdf", "image/png")
pywa.filters.extensions(*exts: str) Filter#

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

>>> extensions(".pdf", ".png")
pywa.filters.has_caption()#

Filter for media messages that have a caption.

pywa.filters.image()#

Filter for image messages.

pywa.filters.video()#

Filter for video messages.

pywa.filters.audio()#

Filter for audio messages (both voice notes and audio files).

pywa.filters.audio_only()#

Filter for audio messages that are not voice notes.

pywa.filters.voice()#

Filter for audio messages that are voice notes.

pywa.filters.document()#

Filter for document messages.

pywa.filters.sticker()#

Filter for sticker messages (both static and animated).

pywa.filters.animated_sticker()#

Filter for animated sticker messages.

pywa.filters.static_sticker()#

Filter for static sticker messages.

pywa.filters.reaction()#

Filter for reaction messages (both added and removed).

pywa.filters.reaction_added()#

Filter for reaction messages that were added to a message.

pywa.filters.reaction_removed()#

Filter for reaction messages that were removed from a message.

pywa.filters.reaction_emojis(*emojis: str) Filter#

Filter for custom reaction messages. pass emojis as strings.

>>> reaction_emojis("πŸ‘","πŸ‘Ž")
pywa.filters.unsupported()#

Filter for all unsupported messages.

pywa.filters.location()#

Filter for location messages.

pywa.filters.current_location()#

Filter for location messages that are current locations.

pywa.filters.location_in_radius(lat: float, lon: float, radius: float | int) Filter#

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()#

Filter for contacts messages.

pywa.filters.contacts_has_wa()#

Filter for contacts messages that have a WhatsApp account.

pywa.filters.order()#

Filter for order messages.