新增淘宝(Taobao)渠道扩展,支持在 Yuxi 平台中集成淘宝电商客服渠道。 包含以下功能模块: - client: 淘宝 API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - message: 消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 加解密处理 - status: 会话状态管理 - tools: Agent 工具集成 - types: 类型定义
86 lines
3.0 KiB
Python
86 lines
3.0 KiB
Python
import logging
|
|
|
|
from yuxi.channel.extensions.taobao.types import InboundTaobaoMessage
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def parse_to_unified_message(raw: dict) -> dict | None:
|
|
msg_type = str(raw.get("msg_type", raw.get("type", "text")))
|
|
|
|
content_map = {
|
|
"text": lambda r: r.get("content", "") or r.get("text", ""),
|
|
"0": lambda r: r.get("content", "") or r.get("text", ""),
|
|
"image": lambda r: "[图片消息]",
|
|
"1": lambda r: "[图片消息]",
|
|
"order": lambda r: _format_order_message(r),
|
|
"trade": lambda r: _format_order_message(r),
|
|
"system": lambda r: "[系统消息]",
|
|
"ordering": lambda r: _format_ordering_message(r),
|
|
"item": lambda r: _format_item_card(r),
|
|
"coupon": lambda r: _format_coupon_card(r),
|
|
}
|
|
|
|
handler = content_map.get(msg_type, lambda r: r.get("content", "") or r.get("text", ""))
|
|
content = handler(raw)
|
|
|
|
buyer_nick = raw.get("buyer_nick", "") or raw.get("from_user", "") or raw.get("from_id", "")
|
|
msg_id = raw.get("msg_id", "") or raw.get("id", "") or str(hash(f"{buyer_nick}{content}"))
|
|
|
|
if not buyer_nick or not content:
|
|
return None
|
|
|
|
unified = {
|
|
"sender": {"id": buyer_nick, "name": buyer_nick},
|
|
"content": content,
|
|
"message_id": msg_id,
|
|
"channel_type": "taobao",
|
|
"chat_type": "direct",
|
|
"timestamp": raw.get("timestamp"),
|
|
"raw": raw,
|
|
}
|
|
|
|
order_id = raw.get("order_id") or raw.get("trade_id") or raw.get("tid")
|
|
if order_id:
|
|
unified["order_id"] = str(order_id)
|
|
|
|
return unified
|
|
|
|
|
|
def _format_order_message(raw: dict) -> str:
|
|
order_id = raw.get("order_id", raw.get("trade_id", ""))
|
|
status = raw.get("status", raw.get("order_status", ""))
|
|
buyer = raw.get("buyer_nick", "")
|
|
return f"[订单消息] 订单ID: {order_id}, 状态: {status}, 买家: {buyer}"
|
|
|
|
|
|
def _format_ordering_message(raw: dict) -> str:
|
|
item_name = raw.get("item_title", raw.get("item_name", "未知商品"))
|
|
price = raw.get("price", "")
|
|
return f"[商品卡片] {item_name}" + (f" ¥{price}" if price else "")
|
|
|
|
|
|
def _format_item_card(raw: dict) -> str:
|
|
item_name = raw.get("item_title", raw.get("item_name", "未知商品"))
|
|
price = raw.get("price", "")
|
|
return f"[商品推荐] {item_name}" + (f" ¥{price}" if price else "")
|
|
|
|
|
|
def _format_coupon_card(raw: dict) -> str:
|
|
coupon_name = raw.get("coupon_name", "优惠券")
|
|
amount = raw.get("amount", raw.get("denomination", ""))
|
|
return f"[优惠券] {coupon_name}" + (f" ¥{amount}" if amount else "")
|
|
|
|
|
|
def parse_raw_to_taobao_message(raw: dict) -> InboundTaobaoMessage:
|
|
return InboundTaobaoMessage(
|
|
msg_id=raw.get("msg_id", raw.get("id", "")),
|
|
buyer_nick=raw.get("buyer_nick", raw.get("from_user", "")),
|
|
seller_nick=raw.get("seller_nick", ""),
|
|
content=raw.get("content", raw.get("text", "")),
|
|
msg_type=raw.get("msg_type", "text"),
|
|
order_id=raw.get("order_id") or raw.get("trade_id"),
|
|
item_id=raw.get("item_id"),
|
|
raw_payload=raw,
|
|
)
|