Skip to content

Commit

Permalink
feat: add handler for synchronous smartapp requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Maximenyuk committed Jun 11, 2024
1 parent d329adc commit b75ad78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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, SyncSmartAppRequestResponsePayload
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_request(
self,
event: SmartAppEvent,
bot: Bot,
) -> SyncSmartAppRequestResponsePayload:
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 SyncSmartAppRequestResponsePayload.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
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-smartapp-rpc"
version = "0.8.0"
version = "0.9.0"
description = "eXpress SmartApp JSON-RPC library"
authors = ["Arseniy Zhiltsov <arseniy.zhiltsov@ccsteam.ru>"]
readme = "README.md"
Expand Down

0 comments on commit b75ad78

Please sign in to comment.