新增京东(JD)渠道扩展,支持在 Yuxi 平台中集成京东客服渠道。 包含以下功能模块: - client: 京东 API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - crypto: 加解密处理 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - business: 业务逻辑处理 - types: 类型定义
103 lines
4.1 KiB
Python
103 lines
4.1 KiB
Python
import os
|
||
|
||
from yuxi.channel.extensions.jd.types import JDAccount
|
||
|
||
|
||
class JDConfig:
|
||
ENV_MAP = {
|
||
"app_key": "JD_APP_KEY",
|
||
"app_secret": "JD_APP_SECRET",
|
||
"shop_id": "JD_SHOP_ID",
|
||
"dm_policy": "JD_DM_POLICY",
|
||
}
|
||
|
||
def list_account_ids(self, config: dict | None = None) -> list[str]:
|
||
channels = (config or {}).get("channels", {})
|
||
jd_config = channels.get("jd", {})
|
||
accounts = jd_config.get("accounts", {})
|
||
if accounts:
|
||
return list(accounts.keys())
|
||
if self._env_or_config("app_key", config):
|
||
return ["default"]
|
||
return []
|
||
|
||
def resolve_account(self, account_id: str = "default", config: dict | None = None) -> JDAccount:
|
||
channels = (config or {}).get("channels", {})
|
||
jd_config = channels.get("jd", {})
|
||
accounts = jd_config.get("accounts", {})
|
||
account_data = accounts.get(account_id, {})
|
||
|
||
return JDAccount(
|
||
account_id=account_id,
|
||
app_key=account_data.get("app_key") or self._env_or_config("app_key") or "",
|
||
app_secret=account_data.get("app_secret") or self._env_or_config("app_secret") or "",
|
||
shop_id=account_data.get("shop_id") or self._env_or_config("shop_id") or "",
|
||
access_token=account_data.get("access_token"),
|
||
refresh_token=account_data.get("refresh_token"),
|
||
webhook_token=account_data.get("webhook_token"),
|
||
dm_policy=account_data.get("dm_policy") or self._env_or_config("dm_policy") or "pairing",
|
||
webhook_sign_algorithm=account_data.get("webhook_sign_algorithm", "hmac-sha256"),
|
||
allow_from=account_data.get("allow_from", []),
|
||
)
|
||
|
||
def is_configured(self, account: dict | None = None) -> bool:
|
||
if account:
|
||
return bool(account.get("app_key") and account.get("app_secret") and account.get("shop_id"))
|
||
return bool(
|
||
self._env_or_config("app_key") and self._env_or_config("app_secret") and self._env_or_config("shop_id")
|
||
)
|
||
|
||
def _env_or_config(self, key: str, config: dict | None = None) -> str | None:
|
||
env_key = self.ENV_MAP.get(key, "")
|
||
if env_key:
|
||
val = os.getenv(env_key)
|
||
if val:
|
||
return val
|
||
return None
|
||
|
||
def config_schema(self) -> dict:
|
||
return {
|
||
"$schema": "https://json-schema.org/draft-07/schema#",
|
||
"type": "object",
|
||
"title": "京东/咚咚渠道配置",
|
||
"properties": {
|
||
"appKey": {
|
||
"type": "string",
|
||
"title": "App Key",
|
||
"description": "京东开放平台应用 App Key",
|
||
},
|
||
"appSecret": {
|
||
"type": "string",
|
||
"title": "App Secret",
|
||
"x-ui-password": True,
|
||
"description": "京东开放平台应用 App Secret",
|
||
},
|
||
"shopId": {
|
||
"type": "string",
|
||
"title": "店铺 ID",
|
||
"description": "京东店铺 ID(用于标识发送方)",
|
||
},
|
||
"webhookToken": {
|
||
"type": "string",
|
||
"title": "Webhook Token",
|
||
"x-ui-password": True,
|
||
"description": "咚咚消息推送签名验证 Token(如配置)",
|
||
},
|
||
"dmPolicy": {
|
||
"type": "string",
|
||
"title": "DM 策略",
|
||
"enum": ["pairing", "allowlist", "open", "disabled"],
|
||
"default": "pairing",
|
||
"description": "私聊安全策略:pairing=配对码, allowlist=白名单, open=完全开放",
|
||
},
|
||
"webhookSignAlgorithm": {
|
||
"type": "string",
|
||
"title": "Webhook 签名算法",
|
||
"enum": ["hmac-sha256", "md5"],
|
||
"default": "hmac-sha256",
|
||
"description": "咚咚 Webhook 消息推送签名算法",
|
||
},
|
||
},
|
||
"required": ["appKey", "appSecret", "shopId"],
|
||
}
|