Skip to content

Releases: david-lev/pywa

2.6.0

11 Jan 20:04
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [base_update] adding shared_data to help sharing context between handlers & filters (suggested by @mhaugestad in #94)
  • [flows] adding FlowStr - A helper class to create strings containing vars and math expressions without escaping and quoting them
from pywa import WhatsApp, types, filters

wa = WhatsApp(...)

@wa.on_message(filters.text, priority=100)  # a higher priority will be executed first
def on_every_text_message(_: WhatsApp, m: types.Message):
    m.shared_data["key"] = "some_value"  # .shared_data is a dict that can be used to store data between handlers
    m.continue_handling()  # continue to the next handler


# access shared_data from filters
@wa.on_message(filters.new(lambda _, m: m.shared_data["key"] == "value"))
def handle_value(_: WhatsApp, m: types.Message):
    if m.shared_data["other_key"] == "other_value":
        ...
from pywa.types.flows import *

FlowJSON(
    screens=[
        Screen(
            id="START",
            layout=Layout(
                children=[
                    bill := TextInput(name="bill", input_type=InputType.NUMBER),
                    tip := TextInput(name="tip", input_type=InputType.NUMBER),
                    TextHeading(
                        # text=f"`'Your total bill is ' {bill.ref + (bill.ref * tip.ref / 100)}`",
                        text=FlowStr(
                            # avoid escaping and quoting vars and math expressions
                            string="Your total bill is {bill}",
                            bill=bill.ref + (bill.ref * tip.ref / 100),
                        )
                    ),
                ]
            ),
        )
    ]
)

Full Changelog: 2.5.2...2.6.0

2.5.2

07 Jan 21:53
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [handlers] sub-handlers ignoring flow requests with errors when call_on_error set to False
  • [handlers] fix WhatsApp.on_flow_request on class level to mark the FlowRequestHandler instance
  • [callback] callback data generic can be str
# my_handlers.py

from pywa import WhatsApp
from pywa.types import FlowRequest, FlowResponse

@WhatsApp.on_flow_request("/survey-flow")
def survey(_: WhatsApp, req: FlowRequest) -> FlowResponse:
    raise NotImplementedError(req)

@survey.on_data_exchange(call_on_error=False) # The default
def on_good_rating(_: WhatsApp, req: FlowRequest) -> FlowResponse:
    return req.respond(...)

@survey.on_data_exchange(call_on_error=True)
def on_error(_: WhatsApp, req: FlowRequest) -> None:
    logging.error("Error in survey flow: %s", req.data)
# main.py

from . import my_handlers

# register the handlers from the module
wa = WhatsApp(..., handlers_modules=[my_handlers])

Full Changelog: 2.5.1...2.5.2

2.5.1

02 Jan 22:34
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [handlers] adding on_init, on_data_exchange and on_back decorators for flow request callback wrapper
  • [flows] FlowRequest.respond defaults to request flow token
  • [flows] adding FlowRequest.token_no_longer_valid shortcut
  • [flows] deprecate FlowRequest.is_health_check and FlowRequestActionType.PING
from pywa import WhatsApp, filters
from pywa.types import FlowRequest, FlowResponse

wa = WhatsApp(...)

@wa.on_flow_request("/survey-flow")
def survey(_: WhatsApp, req: FlowRequest) -> FlowResponse:
    raise NotImplementedError(req)

@survey.on_init
def on_init(_: WhatsApp, req: FlowRequest) -> FlowResponse:
    return req.respond(screen="SURVEY", data={"first_name": "David"})

@survey.on_data_exchange(screen="SURVEY", filters=filters.new(lambda _, r: int(r.data["rating"]) > 3))
def on_good_rating(_: WhatsApp, req: FlowRequest) -> FlowResponse:
    return req.respond(screen="GOOD_RATING")

@survey.on_data_exchange(call_on_error=True)
def on_error(_: WhatsApp, req: FlowRequest) -> None:
    logging.error("Error in survey flow: %s", req.data)

Full Changelog: 2.4.0...2.5.1

2.4.0

14 Dec 20:28
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [sent_message] adding SentTemplate with SentTemplateStatus
  • [flows] adding pattern for TextInput
  • [flows] adding support for NavigationList
  • [flows] defaulting action's payload to empty dict
  • [flows] deprecating ActionNext and ActionNextType
from pywa import WhatsApp

wa = WhatsApp(...)
sent_template_status = wa.send_template(...).status
from pywa.types.flows import *

FlowJSON(
    version="6.2",
    screens=[
        Screen(
            id="MENU",
            title="Main Menu",
            layout=Layout(
                children=[
                    NavigationList(
                        name="menu",
                        list_items=[
                            NavigationItem(
                                id="1",
                                main_content=NavigationItemMainContent(
                                    title="Contact Us",
                                    description="Contact us now",
                                ),
                                on_click_action=NavigateAction(next=Next(name="CONTACT")),
                            ),
                            NavigationItem(
                                id="2",
                                main_content=NavigationItemMainContent(
                                    title="About Us",
                                    description="Learn more about us",
                                ),
                                on_click_action=NavigateAction(next=Next(name="ABOUT")),
                            ),
                        ],
                    ),
                ],
            ),
        ),
        Screen(id="CONTACT", ...),
        Screen(id="ABOUT", ...),
    ],
)

Full Changelog: 2.3.0...2.4.0

2.3.0

30 Nov 23:14
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [client] allowing to specify the callback url scope
  • [client] expose methods to override callback url in waba and phone scopes
  • [flows] typing DataSource to accept Refs
from pywa import WhatsApp, utils
import fastapi

fastapi_app = fastapi.FastAPI()

wa = WhatsApp(
    phone_id=1234567890,
    token="EAAEZC6hUxkTI...",
    server=fastapi_app,
    callback_url="https://example.com",
    callback_url_scope=utils.CallbackURLScope.PHONE,  # override phone callback url
    verify_token="xyzxyz"
)

Full Changelog: 2.2.0...2.3.0

2.2.0

28 Nov 22:06
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [flows] adding ScreenDataUpdate to use in UpdateDataAction
  • [flows] using math operators between math objs
  • [flows] renaming ActionNext to Next and ActionNextType to NextType
from pywa.types.flows import *

FlowJSON(
    version="6.0",
    screens=[
        Screen(
            id="DEMO_SCREEN",
            data=[
                is_txt_visible := ScreenData(  # Declaring a data object
                    key="is_txt_visible",
                    example=False,
                ),
            ],
            layout=Layout(
                children=[
                    TextBody(
                        text="You checked the box!",
                        visible=is_txt_visible.ref,  # Using the data
                    ),
                    OptIn(
                        label="Show the text",
                        name="show_txt",
                        on_select_action=UpdateDataAction(
                            payload=[is_txt_visible.update(True)]  # Updating the data
                        ),
                        on_unselect_action=UpdateDataAction(
                            payload=[is_txt_visible.update(False)]
                        ),
                    ),
                ]
            ),
        )
    ],
)

Full Changelog: 2.1.0...2.2.0

2.1.0

25 Nov 21:28
92270c2
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

  • [flows] adding CalendarPicker component
  • [flows] allow string concatenation with refs
  • [flows] adding support for math expressions
  • [flows] allowing to use condition in visible
  • [flows] new action: open url
  • [flows] new action: update data
  • [flows] init_value available outside form
  • [flows] allow to use screen/ref as shortcut for .ref_in(screen)
  • [flows] separating Action and adding on_unselect_action
from pywa.types.flows import *

FlowJSON(
    version="6.1",
    screens=[
        start := Screen(
            id="START",
            title="Start",
            layout=Layout(
                children=[
                    date_range := CalendarPicker(  # new component
                        name="date_range",
                        label={
                            "start-date": "Select start date",
                            "end-date": "Select end date",
                        },
                        mode=CalendarPickerMode.RANGE,
                    ),
                    ...,  # Footer with NavigateAction to `RESULT`
                ]
            ),
        ),
        Screen(
            id="RESULT",
            title="Result",
            layout=Layout(
                children=[
                    name := TextInput(
                        name="name",
                        label="Enter your name",
                        input_type=InputType.TEXT,
                    ),
                    birth_year := TextInput(
                        name="birth_year",
                        label="Enter birth year",
                        input_type=InputType.NUMBER,
                        init_value="1990",  # `init_value` outside Form
                    ),
                    TextBody(
                        # string concatenation with refs and math expressions
                        text=f"`'Hello, ' {name.ref} '. You are ' {2024 - birth_year.ref} ' years old.'`",
                        visible=birth_year.ref > 0,  # condition in `visible`
                    ),
                    OptIn(
                        name="rtd",
                        label="I read the docs",
                        on_click_action=OpenUrlAction(  # Open URL action
                            url="https://pywa.readthedocs.io",
                        ),
                    ),
                    Footer(
                        label="Done",
                        on_click_action=CompleteAction(  # `Action` is deprecated
                            payload={
                                "date_range": start/date_range.ref,  # using screen/ref as shortcut
                            },
                        ),
                    ),
                ],
            ),
        ),
    ],
)

Full Changelog: 2.0.5...2.1.0

2.0.5

10 Nov 09:04
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

Note

This release is not compatible with 1.x versions. Please see the Migration Guide for instructions on updating from earlier versions.

  • [client] fix send_template return type SentMessage

Full Changelog: 2.0.4...2.0.5

2.0.4

07 Nov 22:43
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

Note

This release is not compatible with 1.x versions. Please see the Migration Guide for instructions on updating from earlier versions.

  • [client] fix reply_to_message

Full Changelog: 2.0.3...2.0.4

2.0.3

02 Nov 16:27
Compare
Choose a tag to compare

What's Changed

Update with pip: pip3 install -U pywa

Note

This release is not compatible with 1.x versions. Please see the Migration Guide for instructions on updating from earlier versions.

  • [client] override _flow_req_cls
  • [handlers] descriptive repr for Handler

Full Changelog: 2.0.2...2.0.3