from typing import Any from sqlalchemy.orm import Session from app.core.constants import ActorValue from app.modules.audit.constants import AuditSource from app.modules.audit.schemas import AuditLogCreate from app.modules.feishu.commands import FeishuCommandService from app.modules.feishu.constants import FeishuCommandKey from app.modules.feishu.service import FeishuService class FeishuEventService: """Handle Feishu message events from webhook or long connection.""" def __init__(self, db: Session): self.db = db self.feishu = FeishuService(db) self.commands = FeishuCommandService(db) def handle_event( self, payload: dict[str, Any], source: str, auto_reply: bool = True, ) -> dict[str, Any]: self.feishu.verify_event(payload) self.feishu.audit.log( AuditLogCreate( actor=ActorValue.FEISHU, source=AuditSource.FEISHU, action=f"{source}_event", request_payload=payload, response_payload={"accepted": True}, ) ) command = self.commands.extract_event_command(payload) if not command: return {"ok": True, "handled": False} result = self.commands.handle_text( command[FeishuCommandKey.TEXT], chat_id=command[FeishuCommandKey.CHAT_ID], actor=command[FeishuCommandKey.ACTOR], auto_reply=auto_reply, ) return {"ok": True, "handled": True, "result": result}