ForcePilot/backend/package/yuxi/channel/extensions/messenger/config.py
Kris 895602f774 feat(channel): 添加 Facebook Messenger 渠道扩展
新增 Facebook Messenger 渠道扩展,支持在 Yuxi 平台中集成 Messenger 即时通讯渠道。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- actions: 动作处理
- template: 消息模板
- quick_reply: 快捷回复
- private_reply: 私密回复
- handover: 转人工切换
- persona: 人设管理
- profile: 主页配置
- user: 用户信息
- insights: 数据洞察
- notification: 通知推送
- media: 媒体资源处理
- types: 类型定义
2026-05-21 11:25:47 +08:00

155 lines
6.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import logging
logger = logging.getLogger(__name__)
ENV_PREFIX = "MESSENGER"
class MessengerConfigAdapter:
def list_account_ids(self, config: dict | None = None) -> list[str]:
ids = []
if config and "accounts" in config:
ids.extend(config["accounts"].keys())
page_id = os.environ.get(f"{ENV_PREFIX}_PAGE_ID")
if page_id:
ids.append("default")
return list(dict.fromkeys(ids)) or ["default"]
def resolve_account(self, account_id: str = "default", config: dict | None = None) -> dict:
raw = {}
if config and "accounts" in config:
raw = config["accounts"].get(account_id, {})
return self._build_account(account_id, raw)
def _build_account(self, account_id: str, raw: dict) -> dict:
suffix = f"_{account_id.upper()}" if account_id != "default" else ""
return {
"account_id": account_id,
"page_id": raw.get("page_id", os.environ.get(f"{ENV_PREFIX}_PAGE_ID{suffix}", "")),
"page_access_token": raw.get(
"page_access_token", os.environ.get(f"{ENV_PREFIX}_PAGE_ACCESS_TOKEN{suffix}", "")
),
"app_secret": raw.get("app_secret", os.environ.get(f"{ENV_PREFIX}_APP_SECRET{suffix}", "")),
"verify_token": raw.get("verify_token", os.environ.get(f"{ENV_PREFIX}_VERIFY_TOKEN{suffix}", "")),
"dm_policy": raw.get("dm_policy", os.environ.get(f"{ENV_PREFIX}_DM_POLICY{suffix}", "pairing")),
"allow_from": raw.get("allow_from", []),
"persona_id": raw.get("persona_id"),
"default_messaging_type": raw.get(
"default_messaging_type",
os.environ.get(f"{ENV_PREFIX}_DEFAULT_MESSAGING_TYPE{suffix}", "RESPONSE"),
),
"notification_type": raw.get(
"notification_type",
os.environ.get(f"{ENV_PREFIX}_NOTIFICATION_TYPE{suffix}", "REGULAR"),
),
"text_chunk_limit": raw.get("text_chunk_limit", 2000),
"chunk_mode": raw.get("chunk_mode", "length"),
}
@staticmethod
def is_configured(account: dict) -> bool:
return bool(account.get("page_id") and account.get("page_access_token"))
@staticmethod
def is_enabled(account: dict) -> bool:
return account.get("enabled", True)
@staticmethod
def disabled_reason(account: dict) -> str:
if not account.get("page_id"):
return "缺少 Page ID"
if not account.get("page_access_token"):
return "缺少 Page Access Token"
return ""
@staticmethod
def default_account_id(config: dict | None = None) -> str:
if config and "accounts" in config:
return next(iter(config["accounts"]), "default")
return "default"
@staticmethod
def config_schema() -> dict:
return {
"$schema": "https://json-schema.org/draft-07/schema#",
"type": "object",
"title": "Facebook Messenger 渠道配置",
"properties": {
"accounts": {
"type": "object",
"title": "账户列表",
"description": "多账户配置key 为账户 ID",
"additionalProperties": {
"type": "object",
"title": "账户配置",
"properties": {
"page_id": {
"type": "string",
"title": "Page ID",
"description": "Facebook Page 的数字 ID",
},
"page_access_token": {
"type": "string",
"title": "Page Access Token",
"description": "Facebook Page 的访问令牌",
"x-ui-password": True,
},
"app_secret": {
"type": "string",
"title": "App Secret",
"description": "Meta App Secret用于 Webhook 签名验证",
"x-ui-password": True,
},
"verify_token": {
"type": "string",
"title": "Webhook 验证 Token",
"description": "Meta Webhook 配置时的自定义验证 Token",
},
"dm_policy": {
"type": "string",
"title": "DM 策略",
"enum": ["pairing", "allowlist", "open", "disabled"],
"default": "pairing",
},
"allow_from": {
"type": "array",
"title": "白名单用户 PSID 列表",
"items": {"type": "string"},
"default": [],
},
"persona_id": {
"type": "string",
"title": "Persona ID",
"description": "Messenger Persona 虚拟形象 ID",
},
"default_messaging_type": {
"type": "string",
"title": "消息类型",
"enum": ["RESPONSE", "UPDATE", "MESSAGE_TAG"],
"default": "RESPONSE",
},
"notification_type": {
"type": "string",
"title": "通知类型",
"enum": ["REGULAR", "SILENT_PUSH", "NO_PUSH"],
"default": "REGULAR",
},
"enabled": {
"type": "boolean",
"title": "启用",
"default": True,
},
},
"required": ["page_id", "page_access_token"],
},
},
"default_account": {
"type": "string",
"title": "默认账户",
"description": "默认使用的账户 ID",
"default": "default",
},
},
}