ForcePilot/backend/package/yuxi/channel/extensions/msteams/graph_teams.py
Kris 94444ced96 feat(channel): 添加 Microsoft Teams 渠道扩展
新增 Microsoft Teams 渠道扩展,支持在 Yuxi 平台中集成 Microsoft Teams 协作平台。

包含以下功能模块:
- sdk: Bot Framework SDK 封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- auth: JWT 认证
- jwks: JWKS 密钥管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- state: 状态管理
- runtime: 运行时管理
- actions: 动作处理
- adaptive_card: 自适应卡片
- task_modules: 任务模块
- message_extension: 消息扩展
- proactive: Proactive Messaging
- graph: Microsoft Graph API 集成
- graph_teams: Teams 操作
- graph_members: 成员管理
- graph_messages: 消息获取
- graph_thread: 线程管理
- graph_users: 用户管理
- graph_upload: 文件上传
- files: 文件处理
- file_consent: 文件授权
- conversations: 会话存储
- mentions: @提及处理
- threading: 线程管理
- reactions: 表情反应
- polls: 投票功能
- meetings: 会议集成
- feedback: 反馈处理
- sso: 单点登录
- deep_links: 深层链接
- incoming_webhook: 入站 Webhook
- localization: 本地化
- user_agent: 用户代理
- sent_message_cache: 消息缓存
- types: 类型定义
2026-05-21 11:28:42 +08:00

132 lines
3.5 KiB
Python

from __future__ import annotations
import logging
from .graph import MSTeamsGraphClient
logger = logging.getLogger(__name__)
async def get_joined_teams(
graph_client: MSTeamsGraphClient,
*,
limit: int = 50,
) -> list[dict]:
path = f"/me/joinedTeams?$top={limit}"
try:
data = await graph_client.fetch_json(path)
return _parse_teams(data)
except Exception as e:
logger.warning("Failed to get joined teams: %s", e)
return []
async def get_team(
graph_client: MSTeamsGraphClient,
team_id: str,
) -> dict | None:
path = f"/teams/{team_id}"
try:
data = await graph_client.fetch_json(path)
return _parse_team(data)
except Exception as e:
logger.warning("Failed to get team %s: %s", team_id, e)
return None
async def get_team_channels(
graph_client: MSTeamsGraphClient,
team_id: str,
*,
limit: int = 50,
) -> list[dict]:
path = f"/teams/{team_id}/channels?$top={limit}"
try:
data = await graph_client.fetch_json(path)
return _parse_channels(data)
except Exception as e:
logger.warning("Failed to get channels for team %s: %s", team_id, e)
return []
async def get_channel(
graph_client: MSTeamsGraphClient,
team_id: str,
channel_id: str,
) -> dict | None:
path = f"/teams/{team_id}/channels/{channel_id}"
try:
data = await graph_client.fetch_json(path)
return _parse_channel(data)
except Exception as e:
logger.warning("Failed to get channel %s in team %s: %s", channel_id, team_id, e)
return None
async def create_channel(
graph_client: MSTeamsGraphClient,
team_id: str,
name: str,
*,
description: str = "",
membership_type: str = "standard",
) -> dict | None:
path = f"/teams/{team_id}/channels"
body = {
"displayName": name,
"description": description,
"membershipType": membership_type,
}
try:
data = await graph_client.fetch_json(path, method="POST", body=body)
return _parse_channel(data)
except Exception as e:
logger.warning("Failed to create channel %s in team %s: %s", name, team_id, e)
return None
async def rename_group_chat(
graph_client: MSTeamsGraphClient,
chat_id: str,
new_topic: str,
) -> bool:
path = f"/chats/{chat_id}"
body = {"topic": new_topic}
try:
await graph_client.fetch_json(path, method="PATCH", body=body)
return True
except Exception as e:
logger.warning("Failed to rename group chat %s: %s", chat_id, e)
return False
def _parse_team(data: dict) -> dict:
return {
"id": data.get("id", ""),
"display_name": data.get("displayName", ""),
"description": data.get("description", ""),
"visibility": data.get("visibility", ""),
"created_at": data.get("createdDateTime", ""),
"member_count": data.get("summary", {}).get("membersCount", 0),
}
def _parse_teams(data: dict) -> list[dict]:
teams = data.get("value", [])
return [_parse_team(t) for t in teams]
def _parse_channel(data: dict) -> dict:
return {
"id": data.get("id", ""),
"display_name": data.get("displayName", ""),
"description": data.get("description", ""),
"membership_type": data.get("membershipType", "standard"),
"created_at": data.get("createdDateTime", ""),
}
def _parse_channels(data: dict) -> list[dict]:
channels = data.get("value", [])
return [_parse_channel(ch) for ch in channels]