新增 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: 类型定义
146 lines
4.2 KiB
Python
146 lines
4.2 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import secrets
|
|
import time
|
|
from dataclasses import dataclass, field
|
|
|
|
from .adaptive_card import build_attachment
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CONSENT_TTL_SECONDS = 300
|
|
|
|
|
|
@dataclass
|
|
class FileConsentRequest:
|
|
consent_id: str
|
|
file_name: str
|
|
file_size: int
|
|
description: str = ""
|
|
conversation_id: str = ""
|
|
service_url: str = ""
|
|
tenant_id: str | None = None
|
|
reply_to_id: str | None = None
|
|
file_url: str = ""
|
|
upload_path: str = ""
|
|
created_at: float = field(default_factory=time.monotonic)
|
|
accepted: bool = False
|
|
declined: bool = False
|
|
|
|
@property
|
|
def is_expired(self) -> bool:
|
|
return time.monotonic() - self.created_at > CONSENT_TTL_SECONDS
|
|
|
|
@property
|
|
def is_pending(self) -> bool:
|
|
return not self.accepted and not self.declined and not self.is_expired
|
|
|
|
|
|
class FileConsentStore:
|
|
def __init__(self):
|
|
self._consents: dict[str, FileConsentRequest] = {}
|
|
|
|
def create(
|
|
self,
|
|
file_name: str,
|
|
file_size: int,
|
|
*,
|
|
description: str = "",
|
|
file_url: str = "",
|
|
upload_path: str = "",
|
|
conversation_id: str = "",
|
|
service_url: str = "",
|
|
tenant_id: str | None = None,
|
|
reply_to_id: str | None = None,
|
|
) -> FileConsentRequest:
|
|
consent_id = secrets.token_hex(16)
|
|
req = FileConsentRequest(
|
|
consent_id=consent_id,
|
|
file_name=file_name,
|
|
file_size=file_size,
|
|
description=description,
|
|
file_url=file_url,
|
|
upload_path=upload_path,
|
|
conversation_id=conversation_id,
|
|
service_url=service_url,
|
|
tenant_id=tenant_id,
|
|
reply_to_id=reply_to_id,
|
|
)
|
|
self._consents[consent_id] = req
|
|
self._cleanup_expired()
|
|
return req
|
|
|
|
def get(self, consent_id: str) -> FileConsentRequest | None:
|
|
req = self._consents.get(consent_id)
|
|
if req and req.is_expired:
|
|
self._consents.pop(consent_id, None)
|
|
return None
|
|
return req
|
|
|
|
def accept(self, consent_id: str) -> bool:
|
|
req = self.get(consent_id)
|
|
if not req or not req.is_pending:
|
|
return False
|
|
req.accepted = True
|
|
return True
|
|
|
|
def decline(self, consent_id: str) -> bool:
|
|
req = self.get(consent_id)
|
|
if not req or not req.is_pending:
|
|
return False
|
|
req.declined = True
|
|
return True
|
|
|
|
def _cleanup_expired(self) -> None:
|
|
expired = [cid for cid, req in self._consents.items() if req.is_expired]
|
|
for cid in expired:
|
|
self._consents.pop(cid, None)
|
|
|
|
|
|
def build_file_consent_card(consent_id: str, file_name: str, file_size: int, description: str = "") -> dict:
|
|
size_str = _format_size(file_size)
|
|
subtitle = f"大小: {size_str}"
|
|
if description:
|
|
subtitle += f"\n{description}"
|
|
|
|
return {
|
|
"type": "AdaptiveCard",
|
|
"version": "1.4",
|
|
"body": [
|
|
{
|
|
"type": "TextBlock",
|
|
"text": "📎 文件上传请求",
|
|
"weight": "Bolder",
|
|
"size": "Medium",
|
|
},
|
|
{"type": "TextBlock", "text": f"**{file_name}**", "wrap": True},
|
|
{"type": "TextBlock", "text": subtitle, "wrap": True, "isSubtle": True},
|
|
],
|
|
"actions": [
|
|
{
|
|
"type": "Action.Submit",
|
|
"title": "✅ 允许",
|
|
"data": {"action": "file_consent_accept", "consent_id": consent_id},
|
|
},
|
|
{
|
|
"type": "Action.Submit",
|
|
"title": "❌ 拒绝",
|
|
"data": {"action": "file_consent_decline", "consent_id": consent_id},
|
|
},
|
|
],
|
|
}
|
|
|
|
|
|
def build_file_consent_attachment(consent_id: str, file_name: str, file_size: int, description: str = "") -> dict:
|
|
card = build_file_consent_card(consent_id, file_name, file_size, description)
|
|
return build_attachment(card)
|
|
|
|
|
|
def _format_size(size: int) -> str:
|
|
if size < 1024:
|
|
return f"{size} B"
|
|
if size < 1024 * 1024:
|
|
return f"{size / 1024:.1f} KB"
|
|
return f"{size / (1024 * 1024):.1f} MB"
|