新增 Lazada 渠道扩展,支持在 Yuxi 平台中集成 Lazada 电商客服渠道。 包含以下功能模块: - client: Lazada API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - tools: Agent 工具集成 - tools_config: 工具配置 - types: 类型定义
103 lines
2.7 KiB
Python
103 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
@dataclass
|
|
class LazadaMessageTemplate:
|
|
template_id: int
|
|
payload: dict = field(default_factory=dict)
|
|
|
|
def to_dict(self) -> dict:
|
|
return {"template_id": self.template_id, **self.payload}
|
|
|
|
@classmethod
|
|
def text(cls, txt: str, translate_txt: str | None = None) -> LazadaMessageTemplate:
|
|
payload = {"txt": txt}
|
|
if translate_txt:
|
|
payload["translateTxt"] = translate_txt
|
|
return cls(template_id=1, payload=payload)
|
|
|
|
@classmethod
|
|
def system(cls, txt: str) -> LazadaMessageTemplate:
|
|
return cls(template_id=2, payload={"txt": txt})
|
|
|
|
@classmethod
|
|
def image(cls, img_url: str, width: int = 800, height: int = 600) -> LazadaMessageTemplate:
|
|
return cls(
|
|
template_id=3,
|
|
payload={
|
|
"img_url": img_url,
|
|
"width": width,
|
|
"height": height,
|
|
},
|
|
)
|
|
|
|
@classmethod
|
|
def emoji(cls, txt: str) -> LazadaMessageTemplate:
|
|
return cls(template_id=4, payload={"txt": txt})
|
|
|
|
@classmethod
|
|
def video(cls, video_id: str, width: int = 800, height: int = 600) -> LazadaMessageTemplate:
|
|
return cls(
|
|
template_id=6,
|
|
payload={
|
|
"video_id": video_id,
|
|
"width": width,
|
|
"height": height,
|
|
},
|
|
)
|
|
|
|
@classmethod
|
|
def product_card(cls, item_id: str) -> LazadaMessageTemplate:
|
|
return cls(template_id=10006, payload={"item_id": item_id})
|
|
|
|
@classmethod
|
|
def order_card(cls, order_id: str) -> LazadaMessageTemplate:
|
|
return cls(template_id=10007, payload={"order_id": order_id})
|
|
|
|
@classmethod
|
|
def coupon(cls, promotion_id: str) -> LazadaMessageTemplate:
|
|
return cls(template_id=10008, payload={"promotion_id": promotion_id})
|
|
|
|
@classmethod
|
|
def invite_follow(cls) -> LazadaMessageTemplate:
|
|
return cls(template_id=10010, payload={})
|
|
|
|
|
|
TEMPLATE_DESCRIPTIONS = {
|
|
1: "普通文本",
|
|
2: "系统消息",
|
|
3: "图片",
|
|
4: "表情 (Emoji)",
|
|
6: "视频",
|
|
10006: "商品卡片",
|
|
10007: "订单卡片",
|
|
10008: "优惠券",
|
|
10010: "邀请关注店铺",
|
|
}
|
|
|
|
INCOMING_TEMPLATE_LABELS = {
|
|
1: "text",
|
|
2: "system",
|
|
3: "image",
|
|
4: "emoji",
|
|
6: "video",
|
|
10006: "product_card",
|
|
10007: "order_card",
|
|
10008: "coupon",
|
|
10010: "invite_follow",
|
|
}
|
|
|
|
|
|
def truncate_text(content: str, max_chars: int = 2000) -> str:
|
|
if len(content) <= max_chars:
|
|
return content
|
|
return content[: max_chars - 3] + "..."
|
|
|
|
|
|
def strip_html(content: str) -> str:
|
|
import re
|
|
|
|
return re.sub(r"<[^>]+>", "", content or "")
|