新增 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: 类型定义
303 lines
8.8 KiB
Python
303 lines
8.8 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from .client import LazadaAPIError
|
|
from .gateway import LazadaGateway
|
|
from .types import OutboundResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class LazadaOutbound:
|
|
def __init__(self, gateway: LazadaGateway):
|
|
self._gateway = gateway
|
|
|
|
async def send_text(
|
|
self,
|
|
session_id: str,
|
|
content: str,
|
|
*,
|
|
translate_txt: str | None = None,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "1",
|
|
"txt": content[:2000],
|
|
}
|
|
if translate_txt:
|
|
payload["translateTxt"] = translate_txt
|
|
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 文本消息失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_image(
|
|
self,
|
|
session_id: str,
|
|
img_url: str,
|
|
width: int = 800,
|
|
height: int = 600,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "3",
|
|
"img_url": img_url,
|
|
"width": str(width),
|
|
"height": str(height),
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 图片消息失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_product_card(
|
|
self,
|
|
session_id: str,
|
|
item_id: str,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "10006",
|
|
"item_id": item_id,
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 商品卡片失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_order_card(
|
|
self,
|
|
session_id: str,
|
|
order_id: str,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "10007",
|
|
"order_id": order_id,
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 订单卡片失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def recall_message(
|
|
self,
|
|
message_id: str,
|
|
session_id: str,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"message_id": message_id,
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/recall",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=message_id,
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("撤回 Lazada 消息失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_emoji(
|
|
self,
|
|
session_id: str,
|
|
emoji_text: str,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "4",
|
|
"txt": emoji_text,
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada Emoji 消息失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_video(
|
|
self,
|
|
session_id: str,
|
|
video_id: str,
|
|
width: int = 800,
|
|
height: int = 600,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "6",
|
|
"video_id": video_id,
|
|
"width": str(width),
|
|
"height": str(height),
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 视频消息失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_coupon(
|
|
self,
|
|
session_id: str,
|
|
promotion_id: str,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "10008",
|
|
"promotion_id": promotion_id,
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 优惠券失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|
|
|
|
async def send_invite_follow(
|
|
self,
|
|
session_id: str,
|
|
) -> OutboundResult:
|
|
try:
|
|
payload = {
|
|
"session_id": session_id,
|
|
"template_id": "10010",
|
|
}
|
|
resp = await self._gateway.client.call(
|
|
"/im/message/send",
|
|
method="POST",
|
|
params=payload,
|
|
)
|
|
data = resp.get("data", {})
|
|
return OutboundResult(
|
|
success=True,
|
|
message_id=data.get("message_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except LazadaAPIError as e:
|
|
logger.error("发送 Lazada 邀请关注失败: %s", e)
|
|
return OutboundResult(
|
|
success=False,
|
|
error=str(e),
|
|
error_code=e.code,
|
|
raw_response=e.raw,
|
|
)
|