Skip to content

Commit

Permalink
feat: add sync smartapp events handler (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhook authored Jun 11, 2024
1 parent d329adc commit 99db207
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 56 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,23 @@ from anywhere import methods
...
smartapp = SmartAppRPC(routers=[methods.rpc])
```
4. Сделайте хендлер для `smartapp_event` и вызывайте в нем хендлер библиотеки
``` python
@collector.smartapp_event
async def handle_smartapp_event(event: SmartAppEvent, bot: Bot) -> None:
await smartapp.handle_smartapp_event(event, bot)
```
4. Сделайте хендлер для `smartapp_event` и вызывайте в нем хендлер библиотеки:
a. Aсинхронный подход:
``` python
@collector.smartapp_event
async def handle_smartapp_event(event: SmartAppEvent, bot: Bot) -> None:
await smartapp.handle_smartapp_event(event, bot)
```
б. Синхронный подход:
``` python
from pybotx import SyncSmartAppEventResponsePayload

...

@collector.sync_smartapp_event
async def handle_sync_smartapp_event(event: SmartAppEvent, bot: Bot) -> SyncSmartAppEventResponsePayload:
return await smartapp.handle_sync_smartapp_event(event, bot)
```

## Продвинутая работа с библиотекой
* В `RPCResultResponse` можно передавать `botx.File` файлы.
Expand Down
93 changes: 47 additions & 46 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 28 additions & 1 deletion pybotx_smartapp_rpc/rpc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Optional

from pybotx import Bot, SmartAppEvent
from pybotx import Bot, SmartAppEvent, SyncSmartAppEventResponsePayload
from pydantic.error_wrappers import ValidationError

from pybotx_smartapp_rpc.exception_handlers import (
Expand Down Expand Up @@ -54,6 +54,33 @@ async def handle_smartapp_event(self, event: SmartAppEvent, bot: Bot) -> None:
encrypted=rpc_response.encrypted,
)

async def handle_sync_smartapp_event(
self,
event: SmartAppEvent,
bot: Bot,
) -> SyncSmartAppEventResponsePayload:
try:
rpc_request = RPCRequest(**event.data)
except ValidationError as invalid_rcp_request_exc:
rpc_response: RPCResponse = build_invalid_rpc_request_error_response(
invalid_rcp_request_exc,
)
else:
rpc_response = await self._router.perform_rpc_request(
SmartApp(bot, event.bot.id, event.chat.id, event),
rpc_request,
)

return SyncSmartAppEventResponsePayload.from_domain(
ref=event.ref,
smartapp_id=event.bot.id,
chat_id=event.chat.id,
data=rpc_response.jsonable_dict(),
opts={},
files=rpc_response.files,
encrypted=rpc_response.encrypted,
)

@property
def router(self) -> RPCRouter:
return self._router
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybotx-smartapp-rpc"
version = "0.8.0"
version = "0.9.0"
description = "eXpress SmartApp JSON-RPC library"
authors = ["Arseniy Zhiltsov <[email protected]>"]
readme = "README.md"
Expand All @@ -9,7 +9,7 @@ readme = "README.md"
python = ">=3.8,<3.12"

pydantic = ">=1.6.0,<1.11.0"
pybotx = ">=0.60.0"
pybotx = ">=0.67.0"

[tool.poetry.dev-dependencies]
add-trailing-comma = "^2.2.1"
Expand Down
Loading

0 comments on commit 99db207

Please sign in to comment.