98 lines
3.9 KiB
Python
98 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
from .shared import *
|
|
|
|
|
|
class FeishuReceiveMixin:
|
|
async def _handle_message_receive(
|
|
self,
|
|
event: Dict[str, Any]
|
|
) -> Dict[str, Any]:
|
|
"""处理收到的消息事件(编排层)。"""
|
|
state_trace_id = f"feishu-sm-{uuid4().hex[:12]}"
|
|
context = self._build_receive_context(event=event, state_trace_id=state_trace_id)
|
|
message = context["message"]
|
|
sender = context["sender"]
|
|
message_id = str(context["message_id"])
|
|
sender_type = str(context["sender_type"])
|
|
if sender_type in {"app", "bot"}:
|
|
return {"code": 0, "msg": "Ignore bot/app message"}
|
|
await self._record_ingress_trace(
|
|
ingress_source=str(context["ingress_source"]),
|
|
trace_id=state_trace_id,
|
|
message_id=message_id,
|
|
event=event,
|
|
message_content=str(context.get("raw_message_content") or ""),
|
|
)
|
|
message_content = str(context.get("message_content") or "").strip()
|
|
if not message_content:
|
|
await self._reply_for_empty_text_ingress(
|
|
state_trace_id=state_trace_id,
|
|
message=message,
|
|
sender=sender,
|
|
message_id=message_id,
|
|
chat_id=str(context.get("chat_id") or ""),
|
|
mentioned_bot_names=(
|
|
context.get("mentioned_bot_names")
|
|
if isinstance(context.get("mentioned_bot_names"), list)
|
|
else []
|
|
),
|
|
sender_open_id=context.get("sender_open_id"),
|
|
perf_t0=context.get("perf_t0"),
|
|
)
|
|
return {"code": 0, "msg": "No text content"}
|
|
|
|
sender_name = await self._resolve_sender_display_name(
|
|
sender=sender,
|
|
sender_id=(
|
|
context.get("sender_id")
|
|
if isinstance(context.get("sender_id"), dict)
|
|
else {}
|
|
),
|
|
)
|
|
chat_id = str(context.get("chat_id") or "")
|
|
sender_open_id = context.get("sender_open_id")
|
|
message_attachments = self._normalize_message_attachments_from_context(context=context)
|
|
try:
|
|
result = await self._capture_and_persist_from_ingress(
|
|
chat_id=chat_id,
|
|
message_content=message_content,
|
|
sender_name=sender_name,
|
|
sender_open_id=sender_open_id,
|
|
captured_at=context.get("captured_at"),
|
|
mentioned_bot_names=(
|
|
context.get("mentioned_bot_names")
|
|
if isinstance(context.get("mentioned_bot_names"), list)
|
|
else []
|
|
),
|
|
message_id=(message.get("message_id") or "").strip() or None,
|
|
chat_type=str(context.get("chat_type") or "group"),
|
|
message_attachments=message_attachments,
|
|
)
|
|
except Exception as exc:
|
|
return await self._handle_capture_exception_from_ingress(
|
|
state_trace_id=state_trace_id,
|
|
error=exc,
|
|
chat_id=chat_id,
|
|
sender_open_id=sender_open_id,
|
|
sender_name=sender_name,
|
|
message_id=message_id,
|
|
perf_t0=context.get("perf_t0"),
|
|
)
|
|
return await self._finalize_receive_result(
|
|
result=result,
|
|
state_trace_id=state_trace_id,
|
|
message_id=message_id,
|
|
message_content=message_content,
|
|
chat_id=chat_id,
|
|
chat_type=str(context.get("chat_type") or "group"),
|
|
sender_open_id=sender_open_id,
|
|
sender_name=sender_name,
|
|
mentioned_bot_names=(
|
|
context.get("mentioned_bot_names")
|
|
if isinstance(context.get("mentioned_bot_names"), list)
|
|
else []
|
|
),
|
|
perf_t0=context.get("perf_t0"),
|
|
)
|