新增淘宝(Taobao)渠道扩展,支持在 Yuxi 平台中集成淘宝电商客服渠道。 包含以下功能模块: - client: 淘宝 API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - message: 消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 加解密处理 - status: 会话状态管理 - tools: Agent 工具集成 - types: 类型定义
51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
import logging
|
||
from collections import defaultdict
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
class TaobaoSecurity:
|
||
def __init__(self):
|
||
self._allowlists: dict[str, set[str]] = defaultdict(set)
|
||
|
||
def load_config(self, account_id: str, account: dict) -> None:
|
||
allow_from = account.get("allow_from", [])
|
||
self._allowlists[account_id] = {entry.strip().lower() for entry in allow_from if entry and entry.strip()}
|
||
|
||
async def check_allowlist(self, peer_id: str, channel_type: str, account_id: str | None = None, account: dict | None = None) -> bool:
|
||
if account is None:
|
||
return True
|
||
|
||
policy = account.get("dm_policy", "open")
|
||
|
||
if policy == "disabled":
|
||
return False
|
||
|
||
if policy in ("allowlist", "pairing"):
|
||
if account_id:
|
||
allow_set = self._allowlists.get(account_id, set())
|
||
if not allow_set:
|
||
allow_from = account.get("allow_from", [])
|
||
allow_set = {entry.strip().lower() for entry in allow_from if entry and entry.strip()}
|
||
return peer_id in allow_set
|
||
return peer_id in account.get("allow_from", [])
|
||
|
||
return True
|
||
|
||
def resolve_dm_policy(self, account_id: str | None = None, account: dict | None = None) -> dict:
|
||
if account is None:
|
||
return {"mode": "open", "allow_from": []}
|
||
return {
|
||
"mode": account.get("dm_policy", "open"),
|
||
"allow_from": account.get("allow_from", []),
|
||
}
|
||
|
||
def collect_warnings(self, config: dict, account_id: str | None = None, account: dict | None = None) -> list[str]:
|
||
warnings = []
|
||
acc = account or {}
|
||
if acc.get("dm_policy") == "open":
|
||
warnings.append("DM 策略为 'open',任何人可发送消息")
|
||
if not acc.get("callback_url"):
|
||
warnings.append("未配置 Webhook 回调 URL,无法接收消息")
|
||
return warnings
|