feat(01-03): remove Matrix reaction confirmation flow
- drop reaction event handling from Matrix bot - render OutgoingUI as text with !yes/!no instructions - persist pending confirmations when UI buttons are sent
This commit is contained in:
parent
4636b359e2
commit
8a6a33a2ce
1 changed files with 29 additions and 48 deletions
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue