From af0247ca52902c7a69e0a6cda653937ebe901fde Mon Sep 17 00:00:00 2001 From: kutuzov13 Date: Wed, 10 Apr 2024 13:52:48 +0300 Subject: [PATCH] feat: link in markup --- pybotx/models/message/markup.py | 12 +- tests/client/notifications_api/test_markup.py | 105 ++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/pybotx/models/message/markup.py b/pybotx/models/message/markup.py index f3ab683c..88b5f26b 100644 --- a/pybotx/models/message/markup.py +++ b/pybotx/models/message/markup.py @@ -25,6 +25,7 @@ class Button: width_ratio: Missing[int] = Undefined alert: Missing[str] = Undefined process_on_client: Missing[bool] = Undefined + link: Missing[str] = Undefined ButtonRow = List[Button] @@ -78,6 +79,7 @@ def add_button( width_ratio: Missing[int] = Undefined, alert: Missing[str] = Undefined, process_on_client: Missing[bool] = Undefined, + link: Missing[str] = Undefined, new_row: bool = True, ) -> None: button = Button( @@ -91,6 +93,7 @@ def add_button( width_ratio=width_ratio, alert=alert, process_on_client=process_on_client, + link=link, ) self.add_built_button(button, new_row=new_row) @@ -118,6 +121,7 @@ class BotXAPIButtonOptions(UnverifiedPayloadBaseModel): show_alert: Missing[Literal[True]] alert_text: Missing[str] handler: Missing[Literal["client"]] + link: Missing[str] class BotXAPIButton(UnverifiedPayloadBaseModel): @@ -140,8 +144,13 @@ def api_button_from_domain(button: Button) -> BotXAPIButton: if button.process_on_client: handler = "client" + command = button.command + if button.link is not Undefined: + command = "#" + handler = "client" + return BotXAPIButton( - command=button.command, + command=command, label=button.label, data=button.data, opts=BotXAPIButtonOptions( @@ -153,6 +162,7 @@ def api_button_from_domain(button: Button) -> BotXAPIButton: alert_text=button.alert, show_alert=show_alert, handler=handler, + link=button.link, ), ) diff --git a/tests/client/notifications_api/test_markup.py b/tests/client/notifications_api/test_markup.py index 25ea974e..2e8bff9a 100644 --- a/tests/client/notifications_api/test_markup.py +++ b/tests/client/notifications_api/test_markup.py @@ -374,6 +374,111 @@ async def test__markup__color_and_align( assert endpoint.called +async def test__markup__link( + respx_mock: MockRouter, + host: str, + bot_id: UUID, + bot_account: BotAccountWithSecret, +) -> None: + # - Arrange - + endpoint = respx_mock.post( + f"https://{host}/api/v4/botx/notifications/direct", + headers={"Authorization": "Bearer token", "Content-Type": "application/json"}, + json={ + "group_chat_id": "054af49e-5e18-4dca-ad73-4f96b6de63fa", + "notification": { + "body": "Buttons links:", + "bubble": [ + [ + { + "command": "#", + "data": {}, + "label": "Open me", + "opts": { + "silent": True, + "align": "center", + "handler": "client", + "link": "https://example.com", + }, + }, + ], + ], + "keyboard": [ + [ + { + "command": "#", + "data": {}, + "label": "Open me", + "opts": { + "silent": True, + "align": "center", + "handler": "client", + "link": "https://example.com", + }, + }, + ], + ], + "status": "ok", + }, + }, + ).mock( + return_value=httpx.Response( + HTTPStatus.ACCEPTED, + json={ + "status": "ok", + "result": {"sync_id": "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"}, + }, + ), + ) + + bubbles = BubbleMarkup() + bubbles.add_button( + command="#", + label="Open me", + silent=True, + process_on_client=True, + link="https://example.com", + ) + + keyboard = KeyboardMarkup() + keyboard.add_button( + command="#", + label="Open me", + silent=True, + process_on_client=True, + link="https://example.com", + ) + + built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account]) + + # - Act - + async with lifespan_wrapper(built_bot) as bot: + task = asyncio.create_task( + bot.send_message( + body="Buttons links:", + bot_id=bot_id, + chat_id=UUID("054af49e-5e18-4dca-ad73-4f96b6de63fa"), + bubbles=bubbles, + keyboard=keyboard, + ), + ) + + await asyncio.sleep(0) # Return control to event loop + + await bot.set_raw_botx_method_result( + { + "status": "ok", + "sync_id": "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3", + "result": {}, + }, + verify_request=False, + ) + + # - Assert - + assert (await task) == UUID("21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3") + assert endpoint.called + + def test__markup__comparison() -> None: # - Arrange - button = Button("/test", "test")