实现了 Teams 机器人所需的全功能组件,包括: - 基础命令解析与帮助卡片生成 - 租户验证与访问控制 - 自定义 UA 与媒体工具 - 消息分块、批注处理与会话管理 - 防抖、缓存与配置路由能力 - 投票、配对、审计与运行时状态管理 - TTS 语音合成与卡片构建工具 - 群组管理与权限控制逻辑
87 lines
2.1 KiB
Python
87 lines
2.1 KiB
Python
"""Microsoft Teams 频道路由配置。
|
||
|
||
resolveMSTeamsRouteConfig,id + name + slug 候选键匹配,
|
||
wildcard fallback 支持。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
|
||
def resolve_route_config(
|
||
config: dict[str, Any],
|
||
team_id: str = "",
|
||
team_name: str = "",
|
||
channel_id: str = "",
|
||
channel_name: str = "",
|
||
) -> dict[str, Any]:
|
||
routes = config.get("routes", []) or []
|
||
if not routes:
|
||
return config
|
||
|
||
for route in routes:
|
||
if _match_route(route, team_id, team_name, channel_id, channel_name):
|
||
merged = {**config, **route.get("config", {})}
|
||
return merged
|
||
|
||
wildcard = _find_wildcard_route(routes)
|
||
if wildcard:
|
||
return {**config, **wildcard.get("config", {})}
|
||
|
||
return config
|
||
|
||
|
||
def _match_route(
|
||
route: dict[str, Any],
|
||
team_id: str,
|
||
team_name: str,
|
||
channel_id: str,
|
||
channel_name: str,
|
||
) -> bool:
|
||
match_team = route.get("match_team", {}) or {}
|
||
match_channel = route.get("match_channel", {}) or {}
|
||
|
||
team_match = _match_candidate(
|
||
match_team,
|
||
{"id": team_id, "name": team_name},
|
||
)
|
||
if match_team and not team_match:
|
||
return False
|
||
|
||
channel_match = _match_candidate(
|
||
match_channel,
|
||
{"id": channel_id, "name": channel_name},
|
||
)
|
||
if match_channel and not channel_match:
|
||
return False
|
||
|
||
return True
|
||
|
||
|
||
def _match_candidate(
|
||
matcher: dict[str, str],
|
||
candidate: dict[str, str],
|
||
) -> bool:
|
||
if not matcher:
|
||
return True
|
||
if matcher.get("id") and matcher["id"] != candidate.get("id"):
|
||
return False
|
||
if matcher.get("name") and matcher["name"].lower() != (candidate.get("name", "")).lower():
|
||
return False
|
||
slug = matcher.get("slug", "")
|
||
if slug:
|
||
name_slug = (candidate.get("name", "")).lower().replace(" ", "-")
|
||
if slug != name_slug:
|
||
return False
|
||
return True
|
||
|
||
|
||
def _find_wildcard_route(
|
||
routes: list[dict[str, Any]],
|
||
) -> dict[str, Any] | None:
|
||
for route in routes:
|
||
if route.get("match_team", {}).get("id") == "*":
|
||
return route
|
||
return None
|