Skip to content

Commit

Permalink
[filters] adding send_to_me filters shortcut and replays_to filters
Browse files Browse the repository at this point in the history
  • Loading branch information
david-lev committed Apr 28, 2024
1 parent dd9887f commit 89c4a32
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ________________________

# [PyWa](https://github.com/david-lev/pywa) • Python wrapper for the WhatsApp Cloud API

![PyPI Downloads](https://img.shields.io/pypi/dm/pywa)
[![PyPI Version](https://badge.fury.io/py/pywa.svg)](https://badge.fury.io/py/pywa)
[![PyPi Downloads](https://img.shields.io/pypi/dm/pywa)](https://pypi.org/project/pywa/)
[![PyPI Version](https://badge.fury.io/py/pywa.svg)](https://pypi.org/project/pywa/)
![Tests](https://img.shields.io/github/actions/workflow/status/david-lev/pywa/python-app.yml?label=Tests)
[![Docs](https://readthedocs.org/projects/pywa/badge/?version=latest&)](https://pywa.readthedocs.io)
[![License](https://img.shields.io/github/license/david-lev/pywa)](https://github.com/david-lev/pywa/blob/master/LICENSE)
Expand Down
6 changes: 6 additions & 0 deletions docs/source/content/errors/sending_messages_errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Sending Messages Errors
:show-inheritance:
.. autoclass:: TemplateParamCountMismatch()
:show-inheritance:
.. autoclass:: TemplateParamFormatMismatch()
:show-inheritance:
.. autoclass:: TemplateNotExists()
:show-inheritance:
.. autoclass:: TemplateTextTooLong()
Expand All @@ -39,3 +41,7 @@ Sending Messages Errors
:show-inheritance:
.. autoclass:: BusinessPaymentIssue()
:show-inheritance:
.. autoclass:: IncorrectCertificate()
:show-inheritance:
.. autoclass:: AccountInMaintenanceMode()
:show-inheritance:
1 change: 1 addition & 0 deletions docs/source/content/filters/common_filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ Common filters
.. autofunction:: any_
.. autofunction:: not_
.. autofunction:: sent_to
.. autoattribute:: pywa.filters.send_to_me
.. autofunction:: from_users
.. autofunction:: from_countries
1 change: 1 addition & 0 deletions docs/source/content/filters/message_filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Message Filters
.. autoattribute:: pywa.filters.forwarded
.. autoattribute:: pywa.filters.forwarded_many_times
.. autoattribute:: pywa.filters.reply
.. autofunction:: pywa.filters.replays_to
.. autoattribute:: pywa.filters.has_referred_product

----------------
Expand Down
31 changes: 28 additions & 3 deletions pywa/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"forwarded",
"forwarded_many_times",
"reply",
"replays_to",
"has_referred_product",
"sent_to",
"send_to_me",
"from_users",
"from_countries",
"text",
Expand Down Expand Up @@ -74,6 +76,19 @@
>>> filters.reply
"""


def replays_to(*msg_ids: str) -> _MessageFilterT:
"""
Filter for messages that reply to any of the given message ids.
>>> replays_to("wamid.HBKHUIyNTM4NjAfiefhwojfMTNFQ0Q2MERGRjVDMUHUIGGA=")
"""
return (
lambda _, m: m.reply_to_message is not None
and m.reply_to_message.message_id in msg_ids
)


has_referred_product: _MessageFilterT = lambda _, m: (
m.reply_to_message is not None and m.reply_to_message.referred_product is not None
)
Expand Down Expand Up @@ -132,6 +147,16 @@ def sent_to(*, display_phone_number: str = None, phone_number_id: str = None):
)


send_to_me: _MessageFilterT = lambda wa, m: sent_to(phone_number_id=wa.phone_id)(wa, m)
"""
Filter for updates that are sent to the client 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 the client phone number.
>>> send_to_me
"""


def from_users(
*numbers: str,
) -> _MessageFilterT | _CallbackFilterT | _MessageStatusFilterT:
Expand Down Expand Up @@ -214,7 +239,7 @@ def mimetypes(cls, *mimetypes: str) -> _MessageFilterT:
"""
Filter for media messages that match any of the given mime types.
- `\`Supported Media Types\` on developers.facebook.com <https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types>`_.
- `Supported Media Types on developers.facebook.com <https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#supported-media-types>`_.
>>> media.mimetypes("application/pdf", "image/png")
>>> video.mimetypes("video/mp4")
Expand Down Expand Up @@ -344,7 +369,7 @@ def regex(*patterns: str | re.Pattern, flags: int = 0) -> _MessageFilterT:
"""
Filter for text messages that match any of the given regexes.
>>> text.regex(r"Hello\s+World", r"Bye\s+World", flags=re.IGNORECASE)
>>> text.regex(r"^hello", r"bye$")
Args:
*patterns: The regex/regexes to filter for.
Expand Down Expand Up @@ -833,7 +858,7 @@ def data_regex(*patterns: str | re.Pattern, flags: int = 0) -> _CallbackFilterT:
"""
Filter for callbacks their data matches the given regex/regexes.
>>> callback.data_regex(r"^\d+$") # only digits
>>> callback.data_regex(r"^id:", r"true$")
Args:
*patterns: The regex/regexes to match.
Expand Down
46 changes: 46 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
CallbackButton,
MessageStatus,
TemplateStatus,
ReplyToMessage,
ReferredProduct,
)
from pywa.types.base_update import BaseUpdate
from tests.common import UPDATES, API_VERSIONS, WA_NO_FILTERS
Expand Down Expand Up @@ -159,6 +161,20 @@ def same(x: _T) -> _T:
],
"unsupported": [(same, fil.unsupported)],
"reply": [(same, fil.reply)],
"replays_to": [(lambda m: modify_replies_to(m, "123"), fil.replays_to("123"))],
"sent_to": [
(lambda m: modify_send_to(m, "123"), fil.sent_to(phone_number_id="123"))
],
"send_to_me": [
(lambda m: modify_send_to(m, WA_NO_FILTERS.phone_id), fil.send_to_me)
],
"from_users": [(lambda m: modify_send_from(m, "123"), fil.from_users("123"))],
"from_countries": [
(lambda m: modify_send_from(m, "97212345678"), fil.from_countries("972"))
],
"has_referred_product": [
(lambda m: modify_referred_product(m, "IPHONE"), fil.has_referred_product)
],
"forwarded": [(same, fil.forwarded)],
"forwarded_many_times": [(same, fil.forwarded)],
"interactive_message_with_err": [],
Expand Down Expand Up @@ -355,3 +371,33 @@ def modify_callback_data(clb: CallbackButton | CallbackSelection, data: str):

def modify_status_err(status: MessageStatus, err: WhatsAppError):
return dataclasses.replace(status, error=err)


def modify_send_from(msg: Message, wa_id: str):
return dataclasses.replace(
msg, from_user=dataclasses.replace(msg.from_user, wa_id=wa_id)
)


def modify_send_to(msg: Message, wa_id: str):
return dataclasses.replace(
msg, metadata=dataclasses.replace(msg.metadata, phone_number_id=wa_id)
)


def modify_replies_to(msg: Message, message_id: str):
return dataclasses.replace(
msg,
reply_to_message=dataclasses.replace(
msg.reply_to_message, message_id=message_id
),
)


def modify_referred_product(msg: Message, product: str):
reply_to_msg = ReplyToMessage(
message_id=msg.id,
from_user_id=msg.from_user.wa_id,
referred_product=ReferredProduct(catalog_id="123", sku=product),
)
return dataclasses.replace(msg, reply_to_message=reply_to_msg)

0 comments on commit 89c4a32

Please sign in to comment.