1. 调整导入顺序和导入项顺序优化代码结构 2. 新增位置消息类型映射支持 3. 新增频道帖子事件的消息分发处理 4. 重构WebSocket认证失败日志格式 5. 优化令牌刷新错误提示的换行格式 6. 简化事件队列满时的日志输出 7. 新增系统事件处理和打字状态上报支持 8. 实现打字指示器接口的实际调用逻辑 9. 更新通道能力配置,补充缺失的能力项
70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from yuxi.channels.models import ChannelResponse, MessageType
|
|
|
|
|
|
def format_outbound(response: ChannelResponse) -> dict:
|
|
chat_id = response.identity.channel_chat_id
|
|
if not chat_id:
|
|
raise ValueError(
|
|
"channel_chat_id is empty, cannot format outbound message. "
|
|
"Ensure the ChannelIdentity has a valid channel_chat_id."
|
|
)
|
|
metadata = response.metadata or {}
|
|
|
|
payload: dict = {
|
|
"msg_type": _map_message_type(response.message_type, response.content),
|
|
}
|
|
|
|
if metadata.get("group_open_id"):
|
|
payload["group_open_id"] = metadata["group_open_id"]
|
|
elif metadata.get("channel_id"):
|
|
payload["channel_id"] = metadata["channel_id"]
|
|
else:
|
|
payload["open_id"] = chat_id
|
|
|
|
if response.reply_to_message_id:
|
|
payload["reply_to_msg_id"] = response.reply_to_message_id
|
|
|
|
if response.message_type == MessageType.IMAGE or response.message_type == MessageType.STICKER:
|
|
if response.attachments:
|
|
payload["media_url"] = response.attachments[0].url
|
|
payload["content"] = response.content or ""
|
|
else:
|
|
payload["content"] = response.content
|
|
elif response.message_type == MessageType.FILE:
|
|
if response.attachments:
|
|
payload["media_url"] = response.attachments[0].url
|
|
payload["filename"] = response.attachments[0].filename or "file"
|
|
payload["content"] = response.content or ""
|
|
else:
|
|
payload["content"] = response.content
|
|
|
|
if metadata.get("extra"):
|
|
payload["extra"] = metadata["extra"]
|
|
|
|
buttons = metadata.get("buttons")
|
|
if buttons and isinstance(buttons, list):
|
|
payload["buttons"] = buttons
|
|
|
|
card = metadata.get("card")
|
|
if card and isinstance(card, dict):
|
|
payload["card"] = card
|
|
|
|
return payload
|
|
|
|
|
|
def _map_message_type(message_type: MessageType, content: str) -> str:
|
|
_type_map = {
|
|
MessageType.TEXT: "text",
|
|
MessageType.IMAGE: "image",
|
|
MessageType.FILE: "file",
|
|
MessageType.AUDIO: "audio",
|
|
MessageType.VIDEO: "video",
|
|
MessageType.STICKER: "sticker",
|
|
MessageType.CARD: "card",
|
|
MessageType.COMMAND: "text",
|
|
MessageType.LOCATION: "location",
|
|
}
|
|
return _type_map.get(message_type, "text")
|