Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add notification v5 method #448

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions pybotx/bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
BotXAPIRefreshAccessTokenRequestPayload,
RefreshAccessTokenMethod,
)
from pybotx.client.smartapps_api.smartapp_counter_notification import (
BotXAPISmartAppCounterNotificationRequestPayload,
SmartAppCounterNotificationMethod,
)
from pybotx.client.smartapps_api.smartapp_custom_notification import (
BotXAPISmartAppCustomNotificationRequestPayload,
SmartAppCustomNotificationMethod,
Expand Down Expand Up @@ -1573,6 +1577,56 @@ async def send_smartapp_custom_notification(

return botx_api_sync_id.to_domain()

async def send_smartapp_counter_notification(
self,
*,
bot_id: UUID,
group_chat_id: UUID,
title: str,
body: str,
unread_counter: int,
meta: Missing[Dict[str, Any]] = Undefined,
wait_callback: bool = True,
callback_timeout: Optional[float] = None,
) -> UUID:
"""Send SmartApp notification with counter.

:param bot_id: Bot which should perform the request.
:param group_chat_id: Target chat id.
:param title: Notification title.
:param body: Notification body.
:param meta: Meta information.
:param unread_counter: Counter information.
:param wait_callback: Block method call until callback received.
:param callback_timeout: Callback timeout in seconds (or `None` for
endless waiting).

:return: Notification sync_id.
"""

method = SmartAppCounterNotificationMethod(
bot_id,
self._httpx_client,
self._bot_accounts_storage,
self._callbacks_manager,
)
payload = BotXAPISmartAppCounterNotificationRequestPayload.from_domain(
group_chat_id=group_chat_id,
title=title,
body=body,
unread_counter=unread_counter,
meta=meta,
)

botx_api_sync_id = await method.execute(
payload,
wait_callback,
callback_timeout,
self._default_callback_timeout,
)

return botx_api_sync_id.to_domain()

# - Stickers API -
async def create_sticker_pack(
self,
Expand Down
84 changes: 84 additions & 0 deletions pybotx/client/smartapps_api/smartapp_counter_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from typing import Any, Dict, Literal, Optional
from uuid import UUID

from pybotx.client.authorized_botx_method import AuthorizedBotXMethod
from pybotx.missing import Missing
from pybotx.models.api_base import UnverifiedPayloadBaseModel, VerifiedPayloadBaseModel


class BotXAPISmartAppCounterNotificationNestedPayload(UnverifiedPayloadBaseModel):
title: str
body: str
unread_counter: int


class BotXAPISmartAppCounterNotificationRequestPayload(UnverifiedPayloadBaseModel):
group_chat_id: UUID
payload: BotXAPISmartAppCounterNotificationNestedPayload
meta: Missing[Dict[str, Any]]

@classmethod
def from_domain(
cls,
group_chat_id: UUID,
title: str,
body: str,
unread_counter: int,
meta: Missing[Dict[str, Any]],
) -> "BotXAPISmartAppCounterNotificationRequestPayload":
return cls(
group_chat_id=group_chat_id,
payload=BotXAPISmartAppCounterNotificationNestedPayload(
title=title,
body=body,
unread_counter=unread_counter,
),
meta=meta,
)


class BotXAPISyncIdResult(VerifiedPayloadBaseModel):
sync_id: UUID


class BotXAPISmartAppCounterNotificationResponsePayload(VerifiedPayloadBaseModel):
status: Literal["ok"]
result: BotXAPISyncIdResult

def to_domain(self) -> UUID:
return self.result.sync_id


class SmartAppCounterNotificationMethod(AuthorizedBotXMethod):
error_callback_handlers = {
**AuthorizedBotXMethod.error_callback_handlers,
}

async def execute(
self,
payload: BotXAPISmartAppCounterNotificationRequestPayload,
wait_callback: bool,
callback_timeout: Optional[float],
default_callback_timeout: float,
) -> BotXAPISmartAppCounterNotificationResponsePayload:
path = "/api/v5/botx/smartapps/notification"

response = await self._botx_method_call(
"POST",
self._build_url(path),
json=payload.jsonable_dict(),
)

api_model = self._verify_and_extract_api_model(
BotXAPISmartAppCounterNotificationResponsePayload,
response,
)

await self._process_callback(
api_model.result.sync_id,
wait_callback,
callback_timeout,
default_callback_timeout,
)

return api_model
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybotx"
version = "0.63.0"
version = "0.64.0"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <[email protected]>",
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ per-file-ignores =
pybotx/bot/handler_collector.py:WPS437,
pybotx/client/notifications_api/internal_bot_notification.py:WPS202,
pybotx/client/smartapps_api/smartapp_custom_notification.py:WPS118,
pybotx/client/smartapps_api/smartapp_counter_notification.py:WPS118,
# Complex model converting
pybotx/models/message/incoming_message.py:WPS232,
# WPS reacts at using `}` in f-strings
Expand Down
80 changes: 80 additions & 0 deletions tests/client/smartapps_api/test_smartapp_counter_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import asyncio
from http import HTTPStatus
from uuid import UUID

import httpx
import pytest
from respx.router import MockRouter

from pybotx import Bot, BotAccountWithSecret, HandlerCollector, lifespan_wrapper

pytestmark = [
pytest.mark.asyncio,
pytest.mark.mock_authorization,
pytest.mark.usefixtures("respx_mock"),
]


@pytest.mark.parametrize(
argnames="unread_counter",
argvalues=[-1, 0, 1],
ids=["counter below zero", "zero counter", "counter above zero"],
)
async def test__send_smartapp_counter_notification__succeed(
respx_mock: MockRouter,
host: str,
bot_id: UUID,
bot_account: BotAccountWithSecret,
unread_counter: int,
) -> None:
# - Arrange -
endpoint = respx_mock.post(
f"https://{host}/api/v5/botx/smartapps/notification",
headers={"Authorization": "Bearer token", "Content-Type": "application/json"},
json={
"group_chat_id": "705df263-6bfd-536a-9d51-13524afaab5c",
"payload": {
"title": "test",
"body": "test",
"unread_counter": unread_counter,
},
"meta": {"message": "ping"},
},
).mock(
return_value=httpx.Response(
HTTPStatus.ACCEPTED,
json={
"status": "ok",
"result": {"sync_id": "21a9ec9e-f21f-4406-ac44-1a78d2ccf9e3"},
},
),
)

built_bot = Bot(collectors=[HandlerCollector()], bot_accounts=[bot_account])

# - Act -
async with lifespan_wrapper(built_bot) as bot:
task = asyncio.create_task(
bot.send_smartapp_counter_notification(
bot_id=bot_id,
group_chat_id=UUID("705df263-6bfd-536a-9d51-13524afaab5c"),
title="test",
body="test",
unread_counter=unread_counter,
meta={"message": "ping"},
),
)
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
Loading