新增 Lazada 渠道扩展,支持在 Yuxi 平台中集成 Lazada 电商客服渠道。 包含以下功能模块: - client: Lazada API 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - tools: Agent 工具集成 - tools_config: 工具配置 - types: 类型定义
37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
from __future__ import annotations
|
|
|
|
import secrets
|
|
import time
|
|
|
|
|
|
class LazadaPairingStore:
|
|
def __init__(self, code_ttl: int = 300):
|
|
self._codes: dict[str, tuple[str, float]] = {}
|
|
self._ttl = code_ttl
|
|
|
|
async def generate(self, account_id: str) -> str:
|
|
code = secrets.token_hex(3).upper()
|
|
self._codes[code] = (account_id, time.time())
|
|
self._evict_expired()
|
|
return code
|
|
|
|
async def verify(self, account_id: str, code: str) -> bool:
|
|
self._evict_expired()
|
|
entry = self._codes.get(code)
|
|
if entry is None:
|
|
return False
|
|
stored_id, _ = entry
|
|
if stored_id != account_id:
|
|
return False
|
|
del self._codes[code]
|
|
return True
|
|
|
|
def revoke(self, code: str) -> None:
|
|
self._codes.pop(code, None)
|
|
|
|
def _evict_expired(self) -> None:
|
|
now = time.time()
|
|
expired = [k for k, (_, t) in self._codes.items() if now - t > self._ttl]
|
|
for k in expired:
|
|
del self._codes[k]
|