Skip to content

Commit

Permalink
[webhook,types] fix raw update handlers and remove InlineButton
Browse files Browse the repository at this point in the history
  • Loading branch information
david-lev committed Aug 16, 2023
1 parent 7e40e9d commit 65737c7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
27 changes: 21 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
`PyWa <https://github.com/david-lev/pywa>`_ • Python wrapper for the WhatsApp Cloud API
########################################################################################

**THIS IS A WORK IN PROGRESS. DO NOT USE IN PRODUCTION.**


.. image:: https://img.shields.io/pypi/dm/pywa?style=flat-square
:alt: PyPI Downloads
:target: https://pypi.org/project/pywa/
Expand All @@ -31,13 +28,15 @@

________________________

**PyWa is a Python framework for building WhatsApp bots using the WhatsApp Cloud API.**
**The goal is to provide a uniform, fully typed, convenient and usable layer.**

📄 Quick Documentation Index
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

`Get Started <https://pywa.readthedocs.io/en/latest/content/getting-started.html>`_
• `The WhatsApp Client <https://pywa.readthedocs.io/en/latest/content/client/overview.html>`_
•`Handlers <https://pywa.readthedocs.io/en/latest/content/handlers/overview.html>`_
• `WhatsApp Client <https://pywa.readthedocs.io/en/latest/content/client/overview.html>`_
`Handlers <https://pywa.readthedocs.io/en/latest/content/handlers/overview.html>`_
• `Filters <https://pywa.readthedocs.io/en/latest/content/filters/overview.html>`_
• `Updates <https://pywa.readthedocs.io/en/latest/content/updates/overview.html>`_
• `Examples <https://pywa.readthedocs.io/en/latest/content/examples.html>`_
Expand All @@ -53,7 +52,10 @@ ________________________
from pywa import WhatsApp
wa = WhatsApp(phone_id='100458559237541', token='xxxxxxxxxxxxxxx')
wa = WhatsApp(
phone_id='100458559237541',
token='xxxxxxxxxxxxxxx'
)
wa.send_message(
to='9876543210',
Expand Down Expand Up @@ -133,6 +135,19 @@ ________________________

See the `Documentation <https://pywa.readthedocs.io/>`_ for detailed instructions

☑️ **TODO**
------------
- Add tests
- Add support for async
- Add support for typed template messages
- Media URL caching
- Move from threading
- Add support for more web frameworks (``Django``, etc.)
- Add support for more types of updates (``account_alerts``, ``message_template_status_updates``, etc.)
- Add more examples

Feel free to open an issue if you have any suggestions. or even better - submit a PR!

📝 **License**
---------------

Expand Down
2 changes: 1 addition & 1 deletion docs/source/content/filters/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Here is some examples:
.. role:: python(code)
:language: python

.. hint::
.. tip::
:class: dropdown

Keep in mind that all match-filters (:meth:`text.matches`, :meth:`text.contains`, etc) will return ``True`` if
Expand Down
7 changes: 6 additions & 1 deletion docs/source/content/handlers/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ when an update is received from WhatsApp.
.. attention::
:class: dropdown

ALl callback functions must be registered before starting the server. Otherwise, the updates will not be handled!
All callback functions must be registered before starting the server. Otherwise, the updates will not be handled!

There are two ways to register a callback function:

Expand Down Expand Up @@ -166,6 +166,11 @@ main code, or when you want to dynamically register handlers programmatically.
if __name__ == '__main__':
# start the server
.. seealso::
:class: dropdown

See how to filter updates in `Filters <filters/overview.html>`_.


Available handlers
^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion docs/source/content/updates/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
In WhatsApp Cloud API, updates are sent to your webhook URL. There are lots of different types of updates,
but currently, we only support the ``messages`` field. this field contains all the user-related updates (e.g. messages, callbacks, etc.).

.. hint::
.. tip::

If you do want to handle other types of updates, you can use the :func:`~pywa.client.WhatsApp.on_raw_update`
decorator. This decorator will be called for every update.
Expand Down
2 changes: 1 addition & 1 deletion pywa/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
from .message import Message
from .others import Contact, User, Reaction, Location, Metadata, ReplyToMessage, Order, Product, System, \
ProductsSection, CommerceSettings, BusinessProfile, Industry, MessageType
from .callback import CallbackButton, CallbackSelection, Button, SectionRow, Section, SectionList, InlineButton
from .callback import CallbackButton, CallbackSelection, Button, SectionRow, Section, SectionList
from .message_status import MessageStatus, MessageStatusType, Conversation, ConversationCategory
from .media import MediaUrlResponse
12 changes: 0 additions & 12 deletions pywa/types/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,6 @@ def to_dict(self) -> dict:
return {"type": "reply", "reply": {"id": self.callback_data, "title": self.title}}


@dataclass(frozen=True, slots=True)
class InlineButton(Button):
"""
Deprecated. Use :class:`Button` instead.
- This class will be removed in v1.0.0
"""
def __post_init__(self):
import warnings
warnings.warn("InlineButton is deprecated. Use Button instead (This class will be removed in v1.0.0)")


@dataclass(frozen=True, slots=True)
class SectionRow:
"""
Expand Down
4 changes: 2 additions & 2 deletions pywa/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def call_handlers(self, update: dict) -> None:
if not self.filter_updates or (
update["entry"][0]["changes"][0]["value"]["metadata"][
"phone_number_id"] == self.wa_client.phone_id):
update, key = self._convert_dict_to_update(client=self.wa_client, d=update)
update_obj, key = self._convert_dict_to_update(client=self.wa_client, d=update)
if key is None:
return
for handler in self.handlers[key.__handler_type__]:
handler(self.wa_client, update)
handler(self.wa_client, update_obj)
raise KeyError # to always skip the except block
except (KeyError, IndexError): # the update not send to this phone id or filter_updates is True
for raw_update_handler in self.handlers[RawUpdateHandler.__handler_type__]:
Expand Down

0 comments on commit 65737c7

Please sign in to comment.