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

117 lines
4.6 KiB
Python
Raw Normal View History

import os
from yuxi.channel.extensions.weibo.types import WeiboAccount
class WeiboConfig:
ENV_MAP = {
"app_key": "WEIBO_APP_KEY",
"app_secret": "WEIBO_APP_SECRET",
"callback_token": "WEIBO_CALLBACK_TOKEN",
"access_token": "WEIBO_ACCESS_TOKEN",
"dm_policy": "WEIBO_DM_POLICY",
"reply_mode": "WEIBO_REPLY_MODE",
"subscribe_msg": "WEIBO_SUBSCRIBE_MSG",
"remove_markdown": "WEIBO_REMOVE_MD",
"subscribe_guide": "WEIBO_SUBSCRIBE_GUIDE",
}
def list_account_ids(self, config: dict | None = None) -> list[str]:
if self._resolve_app_key():
return ["default"]
return []
def resolve_account(self, account_id: str = "default") -> WeiboAccount:
return WeiboAccount(
account_id=account_id,
app_key=self._env_or_config("app_key") or "",
app_secret=self._env_or_config("app_secret") or "",
callback_token=self._env_or_config("callback_token") or "",
access_token=self._env_or_config("access_token") or "",
dm_policy=self._env_or_config("dm_policy") or "open",
reply_mode=self._env_or_config("reply_mode") or "active",
subscribe_msg=self._env_or_config("subscribe_msg") or "",
remove_markdown=self._env_or_config("remove_markdown") != "false",
subscribe_guide=self._env_or_config("subscribe_guide") or "",
)
def is_configured(self, account: dict | None = None) -> bool:
return bool(
self._resolve_app_key()
and self._env_or_config("app_secret")
and self._env_or_config("callback_token")
and self._env_or_config("access_token")
)
def _resolve_app_key(self) -> str | None:
return self._env_or_config("app_key")
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": {
"app_key": {
"type": "string",
"title": "App Key",
"description": "微博开放平台应用的 App Key",
},
"app_secret": {
"type": "string",
"title": "App Secret",
"x-ui-password": True,
"description": "微博开放平台应用的 App Secret",
},
"callback_token": {
"type": "string",
"title": "回调 Token",
"description": "微博开放平台配置的自定义 Token用于 URL 验证签名)",
},
"access_token": {
"type": "string",
"title": "Access Token",
"x-ui-password": True,
"description": "OAuth2 access_token蓝V后台粉丝服务平台直接获取有效期约 5 年)",
},
"dm_policy": {
"type": "string",
"enum": ["open", "pairing", "allowlist", "disabled"],
"default": "open",
"title": "DM 策略",
},
"reply_mode": {
"type": "string",
"enum": ["passive", "active"],
"default": "active",
"title": "回复模式",
"description": "passive=被动回复, active=客服消息 API",
},
"subscribe_msg": {
"type": "string",
"title": "关注欢迎语",
"description": "用户关注后自动回复的文本",
},
"subscribe_guide": {
"type": "string",
"title": "订阅引导语",
"description": "关注欢迎语后追加的订阅引导文本(如:发送 DY 订阅服务消息)",
},
"remove_markdown": {
"type": "boolean",
"default": True,
"title": "移除 Markdown 格式",
"description": "自动移除 AI 回复中的 Markdown 符号(微博不支持 Markdown 渲染)",
},
},
"required": ["app_key", "app_secret", "callback_token", "access_token"],
}