from typing import Any from sqlalchemy.orm import Session from app.modules.audit.schemas import AuditLogCreate from app.modules.feishu.commands import FeishuCommandService 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="feishu", source="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["text"], chat_id=command["chat_id"], actor=command["actor"], auto_reply=auto_reply, ) return {"ok": True, "handled": True, "result": result}