新增 RingCentral 渠道扩展,支持在 Yuxi 平台中集成 RingCentral 统一通信平台。 包含以下功能模块: - sdk: RingCentral SDK 封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - subscription: 事件订阅 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - events: 事件处理 - adaptive_cards: 自适应卡片 - formatting: 格式化 - media: 媒体资源处理 - mentions: @提及 - notes: 笔记功能 - reactions: 表情反应 - tasks: 任务管理 - teams: 团队管理 - types: 类型定义
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import secrets
|
|
import time
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_CODE_TTL_SEC = 300
|
|
_CODE_LENGTH = 6
|
|
|
|
_pairing_store: dict[str, dict] = {}
|
|
|
|
|
|
class RingCentralPairingAdapter:
|
|
id_label = "RingCentral User ID"
|
|
|
|
async def generate_code(self, peer_id: str) -> str:
|
|
code = secrets.token_hex(_CODE_LENGTH // 2)[:_CODE_LENGTH].upper()
|
|
_pairing_store[peer_id] = {
|
|
"code": code,
|
|
"status": "pending",
|
|
"created_at": time.time(),
|
|
}
|
|
return code
|
|
|
|
async def verify_code(self, peer_id: str, code: str) -> bool:
|
|
stored = _pairing_store.get(peer_id)
|
|
if not stored:
|
|
return False
|
|
if time.time() - stored.get("created_at", 0) > _CODE_TTL_SEC:
|
|
del _pairing_store[peer_id]
|
|
return False
|
|
if stored.get("code") != code:
|
|
return False
|
|
stored["status"] = "verified"
|
|
return True
|
|
|
|
def is_paired(self, peer_id: str) -> bool:
|
|
stored = _pairing_store.get(peer_id)
|
|
return stored is not None and stored.get("status") == "verified"
|
|
|
|
def cleanup_expired(self) -> None:
|
|
now = time.time()
|
|
expired = [k for k, v in _pairing_store.items() if now - v.get("created_at", 0) > _CODE_TTL_SEC]
|
|
for k in expired:
|
|
del _pairing_store[k]
|