本次提交新增了完整的多渠道消息网关系统,包括: 1. 支持飞书、钉钉、Web、Hook 四种渠道的适配器与配置 2. 领域模型层:消息、会话、绑定、出箱等核心实体 3. 应用服务层:管道、中间件、DTO 与业务逻辑 4. 基础设施层:持久化、过滤器、队列等端口实现 5. 接口层:REST API、SSE、WebSocket 通信端点 6. 前端页面与路由配置,添加渠道管理菜单 7. 新增相关依赖包与 docker-compose 部署配置
109 lines
3.3 KiB
Python
109 lines
3.3 KiB
Python
from __future__ import annotations
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
class ChannelConfig:
|
|
def __init__(self, yaml_path: str):
|
|
self._yaml_path = yaml_path
|
|
self._data: dict = {}
|
|
self._load()
|
|
|
|
def _load(self) -> None:
|
|
try:
|
|
import yaml
|
|
|
|
with open(self._yaml_path) as f:
|
|
self._data = yaml.safe_load(f) or {}
|
|
except FileNotFoundError:
|
|
logger.warning("channel config not found: %s, using defaults", self._yaml_path)
|
|
self._data = {}
|
|
except Exception as exc:
|
|
logger.warning("channel config load error: %s, using defaults", exc)
|
|
self._data = {}
|
|
|
|
async def reload(self) -> None:
|
|
self._load()
|
|
logger.info("channel config reloaded from %s", self._yaml_path)
|
|
|
|
async def on_config_updated(self, config: dict) -> list[str]:
|
|
old_auth_token = self._data.get("auth", {}).get("token")
|
|
self._data = config
|
|
updated = []
|
|
new_auth_token = config.get("auth", {}).get("token")
|
|
if old_auth_token != new_auth_token:
|
|
updated.append("auth_token")
|
|
return updated
|
|
|
|
@property
|
|
def auth_token(self) -> str | None:
|
|
return self._data.get("auth", {}).get("token")
|
|
|
|
@property
|
|
def auth_password(self) -> str | None:
|
|
return self._data.get("auth", {}).get("password")
|
|
|
|
@property
|
|
def access_policies(self) -> dict:
|
|
return self._data.get("access_policies", {})
|
|
|
|
@property
|
|
def allow_from(self) -> dict:
|
|
return self._data.get("allow_from", {})
|
|
|
|
@property
|
|
def keyword_blocklist(self) -> set[str]:
|
|
keywords = self._data.get("keyword_blocklist", [])
|
|
return set(kw.lower() for kw in keywords)
|
|
|
|
@property
|
|
def hook_mappings(self) -> list[dict]:
|
|
return self._data.get("hooks", {}).get("mappings", [])
|
|
|
|
@property
|
|
def feishu_verification_token(self) -> str | None:
|
|
return self._data.get("feishu", {}).get("verification_token")
|
|
|
|
@property
|
|
def max_auth_attempts(self) -> int:
|
|
return self._data.get("auth", {}).get("max_attempts", 5)
|
|
|
|
@property
|
|
def lockout_seconds(self) -> int:
|
|
return self._data.get("auth", {}).get("lockout_seconds", 300)
|
|
|
|
@property
|
|
def mention_gate_config(self) -> dict:
|
|
return self._data.get("mention_gate", {})
|
|
|
|
@property
|
|
def feishu_ws_config(self) -> dict | None:
|
|
feishu = self._data.get("feishu", {})
|
|
mode = feishu.get("mode", "webhook")
|
|
if mode not in ("websocket", "both"):
|
|
return None
|
|
return {
|
|
"app_id": feishu.get("app_id", ""),
|
|
"app_secret": feishu.get("app_secret", ""),
|
|
"mode": mode,
|
|
}
|
|
|
|
@property
|
|
def dingtalk_ws_config(self) -> dict | None:
|
|
dingtalk = self._data.get("dingtalk", {})
|
|
mode = dingtalk.get("mode", "webhook")
|
|
if mode not in ("websocket", "both"):
|
|
return None
|
|
return {
|
|
"client_id": dingtalk.get("client_id", ""),
|
|
"client_secret": dingtalk.get("client_secret", ""),
|
|
"mode": mode,
|
|
}
|
|
|
|
def get_channel_config(self, channel_name: str) -> dict:
|
|
return self._data.get(channel_name, {})
|
|
|
|
@property
|
|
def raw_data(self) -> dict:
|
|
return self._data
|