Skip to content

Commit

Permalink
[flows] adding FlowRequest.token_no_longer_valid shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
david-lev committed Jan 2, 2025
1 parent 99d9442 commit 9e536a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/source/content/flows/flow_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Flow Types
.. currentmodule:: pywa.types.flows

.. autoclass:: FlowRequest()
:members: has_error, is_health_check, respond, decrypt_media
:members: respond, decrypt_media, has_error, token_no_longer_valid

.. autoclass:: FlowRequestActionType()

Expand Down
27 changes: 24 additions & 3 deletions pywa/types/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,27 @@ def respond(
close_flow=close_flow,
)

def token_no_longer_valid(self, error_message: str):
"""
Raise a :class:`FlowTokenNoLongerValid` exception with the provided error message.
Example:
>>> wa = WhatsApp(...)
>>> @wa.on_flow_request("/my-flow-endpoint")
... def my_flow_endpoint(_: WhatsApp, req: FlowRequest) -> FlowResponse:
... if req.flow_token == "v1.0": # you see the token is no longer valid
... req.token_no_longer_valid("Open the flow again to continue.")
... ...
Args:
error_message: The error message to display to the user.
Raises:
FlowTokenNoLongerValid: Well, always.
"""
raise FlowTokenNoLongerValid(error_message)

@classmethod
def from_dict(cls, data: dict, raw_encrypted: dict):
return cls(
Expand Down Expand Up @@ -440,7 +461,7 @@ class FlowResponseError(Exception):
"""
Base class for all flow response errors
- Read more at `developers.facebook.com <https://developers.facebook.com/docs/whatsapp/flows/reference/error-codes#endpoint_error_codes>`_.
- Subclass this exception to return or raise from the flow endpoint callback (@wa.on_flow_request).
- Override the ``status_code`` attribute to set the status code of the response.
- Override the ``body`` attribute to set the body of the response (optional).
Expand Down Expand Up @@ -484,8 +505,8 @@ class FlowTokenNoLongerValid(FlowResponseError):
>>> from pywa.types.flows import FlowTokenNoLongerValid
>>> wa = WhatsApp(...)
>>> @wa.on_flow_request("/my-flow-endpoint")
... def my_flow_endpoint(wa: WhatsApp, req: FlowRequest) -> FlowResponse:
... if req.flow_token == "123": # you see the token is no longer valid
... def my_flow_endpoint(_: WhatsApp, req: FlowRequest) -> FlowResponse:
... if req.flow_token == "v1.0": # you see the token is no longer valid
... # wa.send_message(..., buttons=FlowButton(...)) # resend the flow?
... raise FlowTokenNoLongerValid(error_message='Open the flow again to continue.')
... ...
Expand Down

0 comments on commit 9e536a0

Please sign in to comment.