实现了 Teams 机器人所需的全功能组件,包括: - 基础命令解析与帮助卡片生成 - 租户验证与访问控制 - 自定义 UA 与媒体工具 - 消息分块、批注处理与会话管理 - 防抖、缓存与配置路由能力 - 投票、配对、审计与运行时状态管理 - TTS 语音合成与卡片构建工具 - 群组管理与权限控制逻辑
80 lines
2.1 KiB
Python
80 lines
2.1 KiB
Python
"""Microsoft Teams 群组管理。
|
|
|
|
addParticipant / removeParticipant / renameGroup 操作。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, TYPE_CHECKING
|
|
|
|
if TYPE_CHECKING:
|
|
from .graph import GraphClient
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
async def add_participant(
|
|
client: GraphClient,
|
|
chat_id: str,
|
|
user_id: str,
|
|
role: str = "member",
|
|
) -> dict[str, Any]:
|
|
path = f"/chats/{chat_id}/members"
|
|
data = {
|
|
"@odata.type": "#microsoft.graph.aadUserConversationMember",
|
|
"roles": [role],
|
|
"user@odata.bind": f"https://graph.microsoft.com/v1.0/users('{user_id}')",
|
|
}
|
|
result = await client._request_with_retry("POST", path, json_data=data)
|
|
if "error" not in result:
|
|
logger.info(f"MSTeams participant added: {user_id} to {chat_id}")
|
|
return result
|
|
|
|
|
|
async def remove_participant(
|
|
client: GraphClient,
|
|
chat_id: str,
|
|
membership_id: str,
|
|
) -> dict[str, Any]:
|
|
path = f"/chats/{chat_id}/members/{membership_id}"
|
|
result = await client._request_with_retry("DELETE", path)
|
|
if "error" not in result:
|
|
logger.info(f"MSTeams participant removed: {membership_id} from {chat_id}")
|
|
return result
|
|
|
|
|
|
async def rename_group(
|
|
client: GraphClient,
|
|
chat_id: str,
|
|
new_name: str,
|
|
) -> dict[str, Any]:
|
|
path = f"/chats/{chat_id}"
|
|
data = {"topic": new_name}
|
|
result = await client._request_with_retry("PATCH", path, json_data=data)
|
|
if "error" not in result:
|
|
logger.info(f"MSTeams group renamed: {chat_id} -> {new_name}")
|
|
return result
|
|
|
|
|
|
async def list_members(
|
|
client: GraphClient,
|
|
chat_id: str,
|
|
) -> list[dict[str, Any]]:
|
|
path = f"/chats/{chat_id}/members"
|
|
result = await client._request_with_retry("GET", path)
|
|
if "error" in result:
|
|
return []
|
|
return result.get("value", [])
|
|
|
|
|
|
async def get_member_info(
|
|
client: GraphClient,
|
|
chat_id: str,
|
|
user_id: str,
|
|
) -> dict[str, Any] | None:
|
|
members = await list_members(client, chat_id)
|
|
for member in members:
|
|
if member.get("userId", "") == user_id:
|
|
return member
|
|
return None
|