ForcePilot/backend/package/yuxi/channel/extensions/wechat-kf/config.py

113 lines
4.3 KiB
Python
Raw Normal View History

feat(channel): 添加微信客服、微信公众号和微信支付通知渠道扩展 新增微信客服、微信公众号、微信支付通知三个渠道扩展。 微信客服渠道扩展功能模块: - account: 账户管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 加解密处理 - dedupe: 消息去重 - customer: 客户管理 - servicer: 客服管理 - session: 会话管理 - status: 会话状态管理 - media: 媒体资源处理 - statistics: 统计功能 - sync: 数据同步 - upgrade: 升级处理 微信公众号渠道扩展功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 加解密处理 - dedupe: 消息去重 - passive_reply: 被动回复 - message: 消息处理 - broadcast: 群发消息 - template: 模板消息 - menu: 菜单管理 - qrcode: 二维码管理 - user: 用户管理 - media: 媒体资源处理 - status: 会话状态管理 微信支付通知渠道扩展功能模块: - config: 渠道配置管理 - webhook: Webhook 事件处理 - crypto: 加解密与签名校验 - cert_manager: 证书管理 - event_router: 事件路由 - dedupe: 消息去重 - pay_repo: 支付数据仓库 - query_client: 查询客户端 - arq_tasks: 异步任务 - callback_compensator: 回调补偿
2026-05-21 12:00:30 +08:00
import os
from yuxi.channel.extensions.wechat_kf.types import KFAccount
class WeChatKFConfig:
ENV_MAP = {
"corp_id": "WECHAT_KF_CORP_ID",
"corp_secret": "WECHAT_KF_CORP_SECRET",
"token": "WECHAT_KF_TOKEN",
"encoding_aes_key": "WECHAT_KF_ENCODING_AES_KEY",
"kf_account_ids": "WECHAT_KF_ACCOUNT_IDS",
"dm_policy": "WECHAT_KF_DM_POLICY",
"sync_interval": "WECHAT_KF_SYNC_INTERVAL",
"session_ttl": "WECHAT_KF_SESSION_TTL",
}
def list_account_ids(self, config: dict | None = None) -> list[str]:
env_ids = os.getenv("WECHAT_KF_ACCOUNT_IDS", "")
if env_ids:
return [aid.strip() for aid in env_ids.split(",") if aid.strip()]
if self._resolve_corp_id():
return ["default"]
return []
def resolve_account(self, account_id: str = "default") -> KFAccount:
account_ids_str = os.getenv("WECHAT_KF_ACCOUNT_IDS", "")
kf_ids = [aid.strip() for aid in account_ids_str.split(",") if aid.strip()]
return KFAccount(
account_id=account_id,
corp_id=self._resolve_corp_id() or "",
corp_secret=self._env_or_config("corp_secret") or "",
token=self._env_or_config("token") or "",
encoding_aes_key=self._env_or_config("encoding_aes_key") or "",
kf_account_ids=kf_ids,
dm_policy=self._env_or_config("dm_policy") or "open",
sync_interval=float(self._env_or_config("sync_interval") or "1.0"),
session_ttl=int(self._env_or_config("session_ttl") or "3600"),
)
def is_configured(self, account: dict | None = None) -> bool:
return bool(
self._resolve_corp_id()
and self._env_or_config("corp_secret")
and self._env_or_config("token")
and self._env_or_config("encoding_aes_key")
)
def _resolve_corp_id(self) -> str | None:
return self._env_or_config("corp_id")
def _env_or_config(self, key: str) -> 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": {
"corp_id": {
"type": "string",
"title": "企业 ID (corpid)",
"description": "企业微信管理后台的企业 ID",
},
"corp_secret": {
"type": "string",
"title": "微信客服 Secret (corpsecret)",
"x-ui-password": True,
"description": "微信客服应用的 Secret",
},
"token": {
"type": "string",
"title": "回调 Token",
"description": "回调验证 Token自定义字符串≤32 位)",
},
"encoding_aes_key": {
"type": "string",
"title": "EncodingAESKey",
"x-ui-password": True,
"description": "消息加解密密钥43 字符)",
},
"kf_account_ids": {
"type": "string",
"title": "客服账号 ID 列表(逗号分隔)",
"description": "需要管理的微信客服账号 open_kfid",
},
"dm_policy": {
"type": "string",
"enum": ["open", "pairing", "allowlist", "disabled"],
"default": "open",
"title": "DM 策略",
},
"sync_interval": {
"type": "number",
"title": "sync_msg 轮询间隔(秒)",
"default": 1.0,
},
"session_ttl": {
"type": "integer",
"title": "会话状态 TTL",
"default": 3600,
},
},
"required": ["corp_id", "corp_secret", "token", "encoding_aes_key"],
}