ForcePilot/backend/package/yuxi/channel/extensions/taobao/config.py
Kris 904ab9a6c2 feat(channel): 添加淘宝渠道扩展
新增淘宝(Taobao)渠道扩展,支持在 Yuxi 平台中集成淘宝电商客服渠道。

包含以下功能模块:
- client: 淘宝 API 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- message: 消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- crypto: 加解密处理
- status: 会话状态管理
- tools: Agent 工具集成
- types: 类型定义
2026-05-21 11:46:45 +08:00

110 lines
4.6 KiB
Python

import os
from yuxi.channel.extensions.taobao.types import TaobaoAccount
class TaobaoConfigAdapter:
def __init__(self):
self._config: dict = {}
self._accounts: dict = {}
def list_account_ids(self, config: dict) -> list[str]:
self._config = config
self._accounts = config.get("accounts", {})
if self._accounts:
return list(self._accounts.keys())
return ["default"]
def resolve_account(self, account_id: str) -> dict:
raw = self._accounts.get(account_id, {})
if not raw:
raw = {"account_id": account_id}
return self.build_account(account_id, raw).to_dict()
def is_configured(self, account: dict) -> bool:
acc = self._to_account(account)
return acc.is_configured()
def is_enabled(self, account: dict) -> bool:
return account.get("enabled", True)
def disabled_reason(self, account: dict) -> str:
if not account.get("enabled", True):
return "账户已禁用"
return ""
def describe_account(self, account: dict) -> dict:
acc = self._to_account(account)
return {
"account_id": acc.account_id,
"name": acc.name or acc.seller_nick or acc.account_id,
"configured": acc.is_configured(),
"has_token": acc.has_valid_token(),
}
def build_account(self, account_id: str, raw: dict) -> TaobaoAccount:
return TaobaoAccount(
account_id=account_id,
app_key=raw.get("app_key", os.getenv("TAOBAO_APP_KEY", "")),
app_secret=raw.get("app_secret", os.getenv("TAOBAO_APP_SECRET", "")),
sign_method=raw.get("sign_method", "md5"),
access_token=raw.get("access_token", ""),
refresh_token=raw.get("refresh_token", ""),
callback_url=raw.get("callback_url", os.getenv("TAOBAO_CALLBACK_URL", "")),
rsa_public_key=raw.get("rsa_public_key", ""),
rsa_private_key=raw.get("rsa_private_key", ""),
encrypt_messages=raw.get("encrypt_messages", False),
seller_nick=raw.get("seller_nick", ""),
name=raw.get("name", ""),
dm_policy=raw.get("dm_policy", "open"),
allow_from=raw.get("allow_from", []),
timeout=raw.get("timeout", 10),
sandbox=raw.get("sandbox", False),
)
def _to_account(self, account: dict) -> TaobaoAccount:
account_id = account.get("account_id", "default")
if account.get("app_key"):
return self.build_account(account_id, account)
return TaobaoAccount(account_id=account_id)
def config_schema(self) -> dict:
return {
"type": "object",
"properties": {
"accounts": {
"type": "object",
"description": "多商家账号配置",
"additionalProperties": {
"type": "object",
"properties": {
"app_key": {"type": "string", "description": "淘宝开放平台 AppKey"},
"app_secret": {"type": "string", "description": "AppSecret"},
"sign_method": {
"type": "string",
"enum": ["md5", "hmac-sha256"],
"default": "md5",
},
"callback_url": {"type": "string", "description": "Webhook 回调地址"},
"rsa_public_key": {"type": "string", "description": "淘宝分配的 RSA 公钥"},
"rsa_private_key": {"type": "string", "description": "ISV RSA 私钥"},
"encrypt_messages": {"type": "boolean", "default": False},
"seller_nick": {"type": "string", "description": "商家淘宝昵称"},
"dm_policy": {
"type": "string",
"enum": ["open", "pairing", "allowlist", "disabled"],
},
"allow_from": {
"type": "array",
"items": {"type": "string"},
"description": "允许交互的买家 nick 列表",
},
"timeout": {"type": "integer", "default": 10},
"sandbox": {"type": "boolean", "default": False},
},
"required": ["app_key", "app_secret"],
},
}
},
}