ForcePilot/backend/package/yuxi/channel/extensions/wecom/config.py

104 lines
3.9 KiB
Python
Raw Normal View History

import os
from yuxi.channel.extensions.wecom.types import WeComAccount
class WeComConfig:
ENV_MAP = {
"corp_id": "WECOM_CORP_ID",
"corp_secret": "WECOM_CORP_SECRET",
"token": "WECOM_TOKEN",
"encoding_aes_key": "WECOM_ENCODING_AES_KEY",
"agent_id": "WECOM_AGENT_ID",
"dm_policy": "WECOM_DM_POLICY",
"group_policy": "WECOM_GROUP_POLICY",
}
def list_account_ids(self, config: dict | None = None) -> list[str]:
if self._resolve_corp_id():
return ["default"]
return []
def resolve_account(self, account_id: str = "default") -> WeComAccount:
corp_id = self._resolve_corp_id()
return WeComAccount(
account_id=account_id,
corp_id=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 "",
agent_id=int(self._env_or_config("agent_id") or "0"),
dm_policy=self._env_or_config("dm_policy") or "open",
group_policy=self._env_or_config("group_policy") or "always",
)
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")
and self._env_or_config("agent_id")
)
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",
"x-ui-password": True,
"description": "自建应用的 Secret",
},
"token": {
"type": "string",
"title": "回调 Token",
"description": "回调 URL 验证用的自定义 Token 字符串",
},
"encoding_aes_key": {
"type": "string",
"title": "EncodingAESKey",
"x-ui-password": True,
"description": "消息加解密密钥43 字符 Base64",
},
"agent_id": {
"type": "integer",
"title": "应用 AgentID",
"description": "自建企业应用的 AgentID整数",
},
"dm_policy": {
"type": "string",
"enum": ["open", "pairing", "allowlist", "disabled"],
"default": "open",
"title": "私聊 DM 策略",
},
"group_policy": {
"type": "string",
"enum": ["always", "activate_on_mention", "disabled"],
"default": "always",
"title": "群聊策略",
"description": "always=总是回复, activate_on_mention=仅被@时回复, disabled=群聊禁用",
},
},
"required": ["corp_id", "corp_secret", "token", "encoding_aes_key", "agent_id"],
}