本次提交对Microsoft Teams适配器代码进行了多维度优化与新增: 1. 调整多处导入顺序,优化代码可读性 2. 新增media_tools工具模块,提供媒体相关辅助函数 3. 新增thread_history模块,实现对话历史拉取与缓存功能 4. 新增connection_modes模块,支持webhook/websocket/polling三种连接模式 5. 扩展security.py与tool_policy.py,新增通配符配置校验与三级策略解析 6. 新增feedback会话记录功能 7. 为sent_message_cache添加自动清理任务 8. 优化normalizer模块,新增引用、编辑消息解析与线程上下文注入 9. 重构file_upload的SSRF防护逻辑,复用公共校验工具 10. 修复多处导入顺序与代码排版问题 11. 为消息发送添加断路器保护与异步去重锁
68 lines
1.9 KiB
Python
68 lines
1.9 KiB
Python
"""Microsoft Teams 媒体辅助工具。
|
|
|
|
提供 MIME 类型识别、文件名提取、消息 ID 提取等辅助函数。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import mimetypes
|
|
import os
|
|
from typing import Any
|
|
|
|
|
|
def get_mime_type(filename: str) -> str:
|
|
mime_type, _ = mimetypes.guess_type(filename)
|
|
return mime_type or "application/octet-stream"
|
|
|
|
|
|
def extract_filename(activity: dict[str, Any]) -> str:
|
|
attachments = activity.get("attachments", []) or []
|
|
for att in attachments:
|
|
name = att.get("name", "").strip()
|
|
if name:
|
|
return os.path.basename(name)
|
|
content = att.get("content", {}) or {}
|
|
content_name = content.get("name", "").strip()
|
|
if content_name:
|
|
return os.path.basename(content_name)
|
|
return ""
|
|
|
|
|
|
def extract_message_id(activity: dict[str, Any]) -> str:
|
|
msg_id = activity.get("id", "")
|
|
if msg_id:
|
|
return msg_id
|
|
|
|
channel_data = activity.get("channelData", {}) or {}
|
|
channel_msg_id = channel_data.get("id", "")
|
|
if channel_msg_id:
|
|
return channel_msg_id
|
|
|
|
return ""
|
|
|
|
|
|
def extract_attachment_urls(activity: dict[str, Any]) -> list[dict[str, str]]:
|
|
urls: list[dict[str, str]] = []
|
|
attachments = activity.get("attachments", []) or []
|
|
for att in attachments:
|
|
content_type = att.get("contentType", "")
|
|
content_url = att.get("contentUrl", "")
|
|
name = att.get("name", "")
|
|
if content_url:
|
|
urls.append(
|
|
{
|
|
"url": content_url,
|
|
"name": name or os.path.basename(content_url),
|
|
"content_type": content_type,
|
|
}
|
|
)
|
|
return urls
|
|
|
|
|
|
def get_edited_timestamp(activity: dict[str, Any]) -> str | None:
|
|
channel_data = activity.get("channelData", {}) or {}
|
|
edit_time = channel_data.get("editedTimestamp", "")
|
|
if edit_time:
|
|
return edit_time
|
|
return activity.get("editedTimestamp")
|