新增 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: 类型定义
64 lines
2.3 KiB
Python
64 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass
|
|
class BotStateStore:
|
|
base_dir: str = "./data/msteams/state"
|
|
ttl_seconds: int = 7 * 24 * 3600
|
|
|
|
def __post_init__(self):
|
|
Path(self.base_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
def _file_path(self, namespace: str, key: str) -> Path:
|
|
safe_key = key.replace("/", "_").replace(":", "_")
|
|
return Path(self.base_dir) / f"{namespace}_{safe_key}.json"
|
|
|
|
async def load(self, namespace: str, key: str) -> dict:
|
|
path = self._file_path(namespace, key)
|
|
if not path.exists():
|
|
return {}
|
|
|
|
try:
|
|
data = json.loads(path.read_text(encoding="utf-8"))
|
|
except (json.JSONDecodeError, OSError):
|
|
return {}
|
|
|
|
if time.monotonic() - data.get("_updated_at", 0) > self.ttl_seconds:
|
|
path.unlink(missing_ok=True)
|
|
return {}
|
|
return data.get("state", {})
|
|
|
|
async def save(self, namespace: str, key: str, state: dict) -> None:
|
|
path = self._file_path(namespace, key)
|
|
data = {"state": state, "_updated_at": time.monotonic()}
|
|
path.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
async def delete(self, namespace: str, key: str) -> None:
|
|
path = self._file_path(namespace, key)
|
|
path.unlink(missing_ok=True)
|
|
|
|
async def get_user_state(self, user_id: str) -> dict:
|
|
return await self.load("user", user_id)
|
|
|
|
async def set_user_state(self, user_id: str, state: dict) -> None:
|
|
await self.save("user", user_id, state)
|
|
|
|
async def get_conversation_state(self, conversation_id: str) -> dict:
|
|
return await self.load("conversation", conversation_id)
|
|
|
|
async def set_conversation_state(self, conversation_id: str, state: dict) -> None:
|
|
await self.save("conversation", conversation_id, state)
|
|
|
|
async def get_private_state(self, user_id: str, conversation_id: str) -> dict:
|
|
key = f"{user_id}_{conversation_id}"
|
|
return await self.load("private", key)
|
|
|
|
async def set_private_state(self, user_id: str, conversation_id: str, state: dict) -> None:
|
|
key = f"{user_id}_{conversation_id}"
|
|
await self.save("private", key, state)
|