Message Filters#

Usefully filters to use in your handlers.

filters.forwarded: MessageFilterT = <function <lambda>>#
filters.forwarded_many_times: MessageFilterT = <function <lambda>>#
filters.reply: MessageFilterT = <function <lambda>>#

pywa.filters.text#

alias of TextFilter

static text.matches(*matches: str, ignore_case: bool = False) MessageFilterT#

Filter for text messages that match exactly the given text/s.

>>> text.matches("Hello","Hi")
Parameters:
  • *matches – The text/s to filter for.

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

static text.contains(*matches: str, ignore_case: bool = False) MessageFilterT#

Filter for text messages that contain the given text/s.

>>> text.contains("Cat","Dog",ignore_case=True)
Parameters:
  • *matches – The text/s to filter for.

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

static text.startswith(*matches: str, ignore_case: bool = False) MessageFilterT#

Filter for text messages that start with the given text/s.

>>> text.startswith("What", "When", ignore_case=True)
Parameters:
  • *matches – The text/s to filter for.

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

static text.endswith(*matches: str, ignore_case: bool = False) MessageFilterT#

Filter for text messages that end with the given text/s.

>>> text.endswith("Bye", "See you", ignore_case=True)
Parameters:
  • *matches – The text/s to filter for.

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

static text.regex(*patterns: str | re.Pattern, flags: int = 0) MessageFilterT#

Filter for text messages that match the given regex/regexes.

>>> text.regex(r"Hello\s+World", r"Bye\s+World", flags=re.IGNORECASE)
Parameters:
  • *patterns – The regex/regexes to filter for.

  • flags – The regex flags to use (default: 0).

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

Filter for text messages that have a length between the given range/s.

>>> 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 MediaFilter

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")
pywa.filters.image#

alias of ImageFilter

image.has_caption: MessageFilterT = <bound method _MediaWithCaptionFilter._has_caption of <class 'pywa.filters.ImageFilter'>>#

Filter for media messages that have a caption.

>>> filters.media.has_caption
pywa.filters.video#

alias of VideoFilter

video.has_caption: MessageFilterT = <bound method _MediaWithCaptionFilter._has_caption of <class 'pywa.filters.VideoFilter'>>#

Filter for media messages that have a caption.

>>> filters.media.has_caption
pywa.filters.audio#

alias of AudioFilter

audio.voice: MessageFilterT = <function AudioFilter.<lambda>>#

Filter for audio messages that are voice notes.

>>> filters.audio.voice
audio.audio: MessageFilterT = <function AudioFilter.<lambda>>#

Filter for audio messages that are audio files.

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

alias of DocumentFilter

document.has_caption: MessageFilterT = <bound method _MediaWithCaptionFilter._has_caption of <class 'pywa.filters.DocumentFilter'>>#

Filter for media messages that have a caption.

>>> filters.media.has_caption
pywa.filters.sticker#

alias of StickerFilter

sticker.animated: MessageFilterT = <function StickerFilter.<lambda>>#

Filter for animated sticker messages.

>>> filters.sticker.animated
sticker.static: MessageFilterT = <function StickerFilter.<lambda>>#

Filter for static sticker messages.

>>> filters.sticker.static

pywa.filters.reaction#

alias of ReactionFilter

reaction.added: MessageFilterT = <function ReactionFilter.<lambda>>#

Filter for reaction messages that were added.

>>> filters.reaction.added
reaction.removed: MessageFilterT = <function ReactionFilter.<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 LocationFilter

location.current_location: MessageFilterT = <function LocationFilter.<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 ContactsFilter

contacts.has_wa: MessageFilterT = <function ContactsFilter.<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 OrderFilter

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 UnsupportedMsgFilter