from __future__ import annotations import logging from typing import Any from yuxi.channel.extensions.qqbot.types import ( InlineKeyboard, InlineKeyboardButton, InlineKeyboardRow, ) logger = logging.getLogger(__name__) class InteractionHandler: def __init__(self, approval_manager: Any = None, outbound: Any = None, api_client: Any = None): self._approval = approval_manager self._outbound = outbound self._api_client = api_client async def handle_interaction(self, interaction_data: dict) -> None: interaction_id = interaction_data.get("id", "") if self._api_client and interaction_id: try: await self._api_client.ack_interaction(interaction_id, code=0) except Exception: logger.exception("ack_interaction failed: id=%s", interaction_id) data = interaction_data.get("data", {}) button_data = data.get("resolved", {}).get("button_data", "") if button_data.startswith("approve:"): await self._handle_approval_interaction(interaction_data) elif button_data.startswith("config:"): await self._handle_config_interaction(interaction_data) else: logger.debug("Unknown interaction data: %s", button_data) async def _handle_approval_interaction(self, data: dict) -> None: button_data = data.get("data", {}).get("resolved", {}).get("button_data", "") parts = button_data.split(":") if len(parts) < 3: return approval_id = parts[1] action = parts[2] if self._approval: await self._approval.handle_decision(approval_id, action) async def _handle_config_interaction(self, data: dict) -> None: button_data = data.get("data", {}).get("resolved", {}).get("button_data", "") parts = button_data.split(":") if len(parts) < 2: return config_type = parts[1] if config_type == "query": await self._handle_config_query(data) elif config_type == "update": await self._handle_config_update(data) async def _handle_config_query(self, data: dict) -> None: user_id = data.get("data", {}).get("resolved", {}).get("user_id", "") if self._outbound and user_id: await self._outbound.send_text( f"qqbot:c2c:{user_id}", "**远程配置查询**: 请通过 Web 管理面板查看当前配置快照", ) async def _handle_config_update(self, data: dict) -> None: user_id = data.get("data", {}).get("resolved", {}).get("user_id", "") if self._outbound and user_id: await self._outbound.send_text( f"qqbot:c2c:{user_id}", "**远程配置更新**: 请通过 Web 管理面板修改配置", ) def build_approval_keyboard(approval_id: str) -> dict: kb = InlineKeyboard( group_id="approval", click_limit=1, permission={"type": 2}, rows=[ InlineKeyboardRow( buttons=[ InlineKeyboardButton( id="approve_once", label="✅ 允许一次", data=f"approve:{approval_id}:allow-once", style=1, ), InlineKeyboardButton( id="approve_always", label="⭐ 始终允许", data=f"approve:{approval_id}:allow-always", style=1, ), ] ), InlineKeyboardRow( buttons=[ InlineKeyboardButton( id="deny", label="❌ 拒绝", data=f"approve:{approval_id}:deny", style=2, ), ] ), ], ) return kb.to_dict() def build_template_keyboard(template_id: str) -> dict: return { "type": 1, "id": template_id, }