113 lines
4.3 KiB
Python
113 lines
4.3 KiB
Python
|
|
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"],
|
|||
|
|
}
|