该提交实现了完整的钉钉聊天渠道插件,包含: 1. 基础配置、账号管理与凭证校验 2. WebSocket长连接网关与消息去重 3. 消息接收/解析/分发与安全校验 4. 媒体文件上传下载与缓存 5. 互动卡片流式更新与回调处理 6. 群管理、命令支持与诊断工具 7. 完整的插件元数据与依赖声明
38 lines
1010 B
Python
38 lines
1010 B
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import secrets
|
|
import time
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
CODE_TTL = 300
|
|
|
|
|
|
class DingTalkPairing:
|
|
def __init__(self):
|
|
self._codes: dict[str, tuple[str, float]] = {}
|
|
|
|
async def generate_code(self, peer_id: str) -> str:
|
|
code = f"{secrets.randbelow(1_000_000):06d}"
|
|
self._codes[peer_id] = (code, time.monotonic())
|
|
self._evict_expired()
|
|
return code
|
|
|
|
async def verify_code(self, peer_id: str, code: str) -> bool:
|
|
self._evict_expired()
|
|
entry = self._codes.get(peer_id)
|
|
if entry is None:
|
|
return False
|
|
stored_code, _ts = entry
|
|
if stored_code == code:
|
|
del self._codes[peer_id]
|
|
return True
|
|
return False
|
|
|
|
def _evict_expired(self) -> None:
|
|
now = time.monotonic()
|
|
expired = [pid for pid, (_code, ts) in self._codes.items() if now - ts > CODE_TTL]
|
|
for pid in expired:
|
|
del self._codes[pid]
|