ForcePilot/backend/package/yuxi/channels/adapters/feishu/commands.py

50 lines
1.4 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Any
from yuxi.channels.models import ChannelMessage, ChatType, EventType
from yuxi.utils.logging_config import logger
def parse_bot_menu_event(event: dict[str, Any]) -> dict[str, Any] | None:
event_data = event.get("event", {})
event_key = event_data.get("event_key", "")
if not event_key:
logger.debug("[FeishuCmd] No event_key in bot menu event")
return None
timestamp = event_data.get("timestamp", "")
operator = event_data.get("operator", {}) or {}
open_id = operator.get("open_id", "")
return {
"command": event_key,
"open_id": open_id,
"timestamp": timestamp,
}
def build_synthetic_message(
channel_id: str,
channel_type: Any,
open_id: str,
chat_id: str,
command: str,
) -> ChannelMessage:
from yuxi.channels.models import ChannelIdentity, MessageType
return ChannelMessage(
identity=ChannelIdentity(
channel_id=channel_id,
channel_type=channel_type,
channel_user_id=open_id,
channel_chat_id=chat_id,
channel_message_id=f"cmd:{command}",
),
event_type=EventType.BOT_MENU,
message_type=MessageType.TEXT,
chat_type=ChatType.DIRECT,
content=f"/{command}",
metadata={"source": "bot_menu", "command": command},
)