实现了 Teams 机器人所需的全功能组件,包括: - 基础命令解析与帮助卡片生成 - 租户验证与访问控制 - 自定义 UA 与媒体工具 - 消息分块、批注处理与会话管理 - 防抖、缓存与配置路由能力 - 投票、配对、审计与运行时状态管理 - TTS 语音合成与卡片构建工具 - 群组管理与权限控制逻辑
87 lines
2.4 KiB
Python
87 lines
2.4 KiB
Python
"""Microsoft Teams Team/Channel 嵌套配置解析。
|
||
|
||
支持 teams.{id}.channels.{id} 细分配置覆盖,
|
||
通配符 fallback,层级继承 Channel > Team > Global。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
def resolve_nested_config(
|
||
config: dict[str, Any],
|
||
team_id: str,
|
||
channel_id: str = "",
|
||
) -> dict[str, Any]:
|
||
result: dict[str, Any] = {}
|
||
|
||
teams_config = config.get("teams", {}) or {}
|
||
team_config = _resolve_team_entry(teams_config, team_id)
|
||
if not team_config:
|
||
return result
|
||
|
||
result["team_config"] = team_config
|
||
|
||
if channel_id:
|
||
channels_config = team_config.get("channels", {}) or {}
|
||
channel_config = _resolve_channel_entry(channels_config, channel_id)
|
||
if channel_config:
|
||
result["channel_config"] = channel_config
|
||
|
||
return result
|
||
|
||
|
||
def _resolve_team_entry(
|
||
teams_config: dict[str, Any],
|
||
team_id: str,
|
||
) -> dict[str, Any] | None:
|
||
if team_id in teams_config:
|
||
return teams_config[team_id]
|
||
if "*" in teams_config:
|
||
return teams_config["*"]
|
||
return None
|
||
|
||
|
||
def _resolve_channel_entry(
|
||
channels_config: dict[str, Any],
|
||
channel_id: str,
|
||
) -> dict[str, Any] | None:
|
||
if channel_id in channels_config:
|
||
return channels_config[channel_id]
|
||
if "*" in channels_config:
|
||
return channels_config["*"]
|
||
return None
|
||
|
||
|
||
def resolve_reply_style(
|
||
config: dict[str, Any],
|
||
team_id: str,
|
||
channel_id: str = "",
|
||
default: str = "thread",
|
||
) -> str:
|
||
nested = resolve_nested_config(config, team_id, channel_id)
|
||
channel_config = nested.get("channel_config", {}) or {}
|
||
if "reply_style" in channel_config:
|
||
return channel_config["reply_style"]
|
||
team_config = nested.get("team_config", {}) or {}
|
||
if "reply_style" in team_config:
|
||
return team_config["reply_style"]
|
||
return config.get("reply_style", default)
|
||
|
||
|
||
def resolve_require_mention(
|
||
config: dict[str, Any],
|
||
team_id: str,
|
||
channel_id: str = "",
|
||
chat_type: str = "group",
|
||
) -> bool | None:
|
||
nested = resolve_nested_config(config, team_id, channel_id)
|
||
channel_config = nested.get("channel_config", {}) or {}
|
||
if "require_mention" in channel_config:
|
||
return channel_config["require_mention"]
|
||
team_config = nested.get("team_config", {}) or {}
|
||
if "require_mention" in team_config:
|
||
return team_config["require_mention"]
|
||
return config.get("require_mention")
|