Skip to content

Commit

Permalink
Merge pull request #95 from david-lev/update-shared-data
Browse files Browse the repository at this point in the history
[base_update] adding `shared_data` to help sharing context between handlers & filters
  • Loading branch information
david-lev authored Jan 11, 2025
2 parents 08ee43f + 08c19c2 commit dbd8458
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 12 deletions.
15 changes: 3 additions & 12 deletions pywa/types/base_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,10 @@ class BaseUpdate(abc.ABC):
"""Base class for all update types."""

_client: WhatsApp = dataclasses.field(repr=False, hash=False, compare=False)
id: str
timestamp: datetime.datetime
raw: dict = dataclasses.field(repr=False, hash=False, compare=False)

@property
@abc.abstractmethod
def id(self) -> str:
"""The id of the update"""
...

@property
@abc.abstractmethod
def timestamp(self) -> datetime.datetime:
"""The timestamp the update was sent"""
...
shared_data: dict = dataclasses.field(hash=False, default_factory=dict)

@classmethod
@abc.abstractmethod
Expand Down
1 change: 1 addition & 0 deletions pywa/types/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ class CallbackButton(BaseUserUpdate, Generic[_CallbackDataT]):
data: The data of the button (the ``callback_data`` parameter you provided in :class:`Button` or
:class:`Template.QuickReplyButtonData`).
title: The title of the button.
shared_data: Shared data between handlers.
"""

id: str
Expand Down
1 change: 1 addition & 0 deletions pywa/types/chat_opened.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ChatOpened(BaseUserUpdate):
type: The message type (Always ``MessageType.REQUEST_WELCOME``).
from_user: The user who opened the chat.
timestamp: The timestamp when this message was sent.
shared_data: Shared data between handlers.
"""

id: str
Expand Down
1 change: 1 addition & 0 deletions pywa/types/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class FlowCompletion(BaseUserUpdate):
body: The body of the message.
token: The token of the flow. can be ``None`` in some cases :|
response: The response from the flow.
shared_data: Shared data between handlers.
"""

id: str
Expand Down
1 change: 1 addition & 0 deletions pywa/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Message(BaseUserUpdate):
order: The order of the message (if the message type is :class:`MessageType.ORDER`).
system: The system update (if the message type is :class:`MessageType.SYSTEM`).
error: The error of the message (if the message type is :class:`MessageType.UNSUPPORTED`).
shared_data: Shared data between handlers.
"""

id: str
Expand Down
1 change: 1 addition & 0 deletions pywa/types/message_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class MessageStatus(BaseUserUpdate, Generic[_CallbackDataT]):
conversation: The conversation the given status notification belongs to (Optional).
pricing_model: Type of pricing model used by the business. Current supported value is CBP.
error: The error that occurred (if status is :class:`MessageStatusType.FAILED`).
shared_data: Shared data between handlers.
"""

id: str
Expand Down
1 change: 1 addition & 0 deletions pywa/types/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ class TemplateStatus(BaseUpdate):
reason: The reason the template was rejected (if applicable).
disable_date: The date the template was disabled (if applicable).
other_info: Additional information about the template (if applicable).
shared_data: Shared data between handlers.
"""

id: str
Expand Down
1 change: 1 addition & 0 deletions pywa_async/types/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CallbackButton(BaseUserUpdateAsync, _CallbackButton[_CallbackDataT]):
data: The data of the button (the ``callback_data`` parameter you provided in :class:`Button` or
:class:`Template.QuickReplyButtonData`).
title: The title of the button.
shared_data: Shared data between handlers.
"""


Expand Down
1 change: 1 addition & 0 deletions pywa_async/types/chat_opened.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ class ChatOpened(BaseUserUpdateAsync, _ChatOpened):
type: The message type (Always ``MessageType.REQUEST_WELCOME``).
from_user: The user who opened the chat.
timestamp: The timestamp when this message was sent.
shared_data: Shared data between handlers.
"""
1 change: 1 addition & 0 deletions pywa_async/types/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class FlowCompletion(BaseUserUpdateAsync, _FlowCompletion):
body: The body of the message.
token: The token of the flow. can be ``None`` in some cases :|
response: The response from the flow.
shared_data: Shared data between handlers.
"""


Expand Down
1 change: 1 addition & 0 deletions pywa_async/types/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Message(BaseUserUpdateAsync, _Message):
order: The order of the message (if the message type is :class:`MessageType.ORDER`).
system: The system update (if the message type is :class:`MessageType.SYSTEM`).
error: The error of the message (if the message type is :class:`MessageType.UNSUPPORTED`).
shared_data: Shared data between handlers.
"""

_client: WhatsApp = dataclasses.field(repr=False, hash=False, compare=False)
Expand Down
1 change: 1 addition & 0 deletions pywa_async/types/message_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ class MessageStatus(BaseUserUpdateAsync, _MessageStatus[_CallbackDataT]):
conversation: The conversation the given status notification belongs to (Optional).
pricing_model: Type of pricing model used by the business. Current supported value is CBP.
error: The error that occurred (if status is :class:`MessageStatusType.FAILED`).
shared_data: Shared data between handlers.
"""
39 changes: 39 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pywa import handlers, types, WhatsApp, filters
from pywa.handlers import FlowRequestHandler, _flow_request_handler_attr
from pywa.types import Message
from pywa_async import WhatsApp as WhatsAppAsync

FAKE_WA = WhatsApp(phone_id="1234567890", token="1234567890:1234567890")
Expand Down Expand Up @@ -193,3 +194,41 @@ def callback(_, __): ...

h.add_handler(callback=callback, action=types.FlowRequestActionType.INIT)
assert h._handlers[(types.FlowRequestActionType.INIT, None)][0][1] is callback


def test_shared_data():
wa = WhatsApp(server=None, verify_token="xyzxyz")
msg = Message(
_client=wa,
raw={},
id="123",
type=types.MessageType.TEXT,
forwarded=False,
forwarded_many_times=False,
reply_to_message=None,
metadata=types.Metadata(
display_phone_number="1234567890",
phone_number_id="1234567890",
),
from_user=types.User(
wa_id="1234567890",
name="John",
),
timestamp=datetime.datetime.now(),
)

@wa.on_message
def shared_data_handler(_: WhatsApp, m: types.Message):
m.shared_data["key"] = None
m.continue_handling()

@wa.on_message
def shared_data_check(_: WhatsApp, m: types.Message):
m.shared_data["key"] = "value"

wa._invoke_callbacks(
handler_type=handlers.MessageHandler,
update=msg,
)

assert msg.shared_data["key"] == "value"

0 comments on commit dbd8458

Please sign in to comment.