diff --git a/adapter/matrix/bot.py b/adapter/matrix/bot.py index 8bf7457..a7e6ac6 100644 --- a/adapter/matrix/bot.py +++ b/adapter/matrix/bot.py @@ -11,16 +11,16 @@ from nio import ( AsyncClientConfig, InviteMemberEvent, MatrixRoom, - ReactionEvent, RoomMemberEvent, RoomMessageText, ) from dotenv import load_dotenv -from adapter.matrix.converter import from_reaction, from_room_event +from adapter.matrix.converter import from_room_event from adapter.matrix.handlers import register_matrix_handlers from adapter.matrix.handlers.auth import handle_invite from adapter.matrix.room_router import resolve_chat_id +from adapter.matrix.store import set_pending_confirm from core.auth import AuthManager from core.chat import ChatManager from core.handler import EventDispatcher @@ -103,16 +103,6 @@ class MatrixBot: outgoing = await self.runtime.dispatcher.dispatch(incoming) await self._send_all(room.room_id, outgoing) - async def on_reaction(self, room: MatrixRoom, event: ReactionEvent) -> None: - if getattr(event, "sender", None) == self.client.user_id: - return - chat_id = await resolve_chat_id(self.runtime.store, room.room_id, event.sender) - incoming = from_reaction(event, sender=event.sender, chat_id=chat_id) - if incoming is None: - return - outgoing = await self.runtime.dispatcher.dispatch(incoming) - await self._send_all(room.room_id, outgoing) - async def on_member(self, room: MatrixRoom, event: RoomMemberEvent) -> None: if getattr(event, "sender", None) == self.client.user_id: return @@ -129,18 +119,14 @@ class MatrixBot: async def _send_all(self, room_id: str, outgoing: list[OutgoingEvent]) -> None: for event in outgoing: - await send_outgoing(self.client, room_id, event) + await send_outgoing(self.client, room_id, event, store=self.runtime.store) - -def _button_action_to_reaction(action: str) -> str | None: - if action in {"confirm", "ok", "accept"}: - return "👍" - if action in {"cancel", "reject", "deny"}: - return "❌" - return None - - -async def send_outgoing(client: AsyncClient, room_id: str, event: OutgoingEvent) -> None: +async def send_outgoing( + client: AsyncClient, + room_id: str, + event: OutgoingEvent, + store: StateStore | None = None, +) -> None: if isinstance(event, OutgoingTyping): await client.room_typing(room_id, event.is_typing, timeout=25000) return @@ -152,31 +138,27 @@ async def send_outgoing(client: AsyncClient, room_id: str, event: OutgoingEvent) await client.room_send(room_id, "m.room.message", {"msgtype": "m.text", "body": event.text}) return if isinstance(event, OutgoingUI): - body = event.text - buttons = [] - for button in event.buttons: - buttons.append(f"• {button.label}") - if buttons: - body = "\n".join([body, "", *buttons]) - resp = await client.room_send( - room_id, "m.room.message", {"msgtype": "m.text", "body": body} - ) - event_id = getattr(resp, "event_id", None) - if event_id: + lines = [event.text] + if event.buttons: + lines.append("") for button in event.buttons: - reaction = _button_action_to_reaction(button.action) - if reaction: - await client.room_send( - room_id, - "m.reaction", - { - "m.relates_to": { - "rel_type": "m.annotation", - "event_id": event_id, - "key": reaction, - } - }, - ) + lines.append(f" {button.label}") + lines.append("") + lines.append("Ответьте !yes для подтверждения или !no для отмены.") + body = "\n".join(lines) + await client.room_send(room_id, "m.room.message", {"msgtype": "m.text", "body": body}) + if event.buttons and store is not None: + action_id = event.buttons[0].action + payload = event.buttons[0].payload + await set_pending_confirm( + store, + room_id, + { + "action_id": action_id, + "description": event.text, + "payload": payload, + }, + ) return @@ -213,7 +195,6 @@ async def main() -> None: bot = MatrixBot(client, runtime) client.add_event_callback(bot.on_room_message, RoomMessageText) - client.add_event_callback(bot.on_reaction, ReactionEvent) client.add_event_callback(bot.on_member, (InviteMemberEvent, RoomMemberEvent)) logger.info(