新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。 企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status 微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import random
|
|
import time
|
|
|
|
CODE_LENGTH = 6
|
|
CODE_TTL_SECONDS = 600
|
|
PENDING_MAX = 3
|
|
RATE_LIMIT_SECONDS = 3600
|
|
|
|
|
|
class WeiboPairing:
|
|
def __init__(self):
|
|
self._pending: dict[str, dict] = {}
|
|
self._sender_last_code_at: dict[str, float] = {}
|
|
|
|
def generate_code(self, sender_id: str) -> str | None:
|
|
now = time.time()
|
|
|
|
if sender_id in self._sender_last_code_at:
|
|
elapsed = now - self._sender_last_code_at[sender_id]
|
|
if elapsed < RATE_LIMIT_SECONDS:
|
|
return None
|
|
|
|
if len(self._pending) >= PENDING_MAX:
|
|
oldest = min(self._pending.values(), key=lambda p: p["created_at"])
|
|
if now - oldest["created_at"] < CODE_TTL_SECONDS:
|
|
return None
|
|
|
|
expired_sender = next(k for k, v in self._pending.items() if v == oldest)
|
|
del self._pending[expired_sender]
|
|
|
|
code = _generate_code()
|
|
self._pending[sender_id] = {"code": code, "created_at": now}
|
|
self._sender_last_code_at[sender_id] = now
|
|
return code
|
|
|
|
def verify(self, sender_id: str, code: str) -> bool:
|
|
entry = self._pending.get(sender_id)
|
|
if not entry:
|
|
return False
|
|
|
|
if time.time() - entry["created_at"] > CODE_TTL_SECONDS:
|
|
del self._pending[sender_id]
|
|
return False
|
|
|
|
if entry["code"] != code.upper().strip():
|
|
return False
|
|
|
|
del self._pending[sender_id]
|
|
return True
|
|
|
|
def get_pending_count(self) -> int:
|
|
return len(self._pending)
|
|
|
|
|
|
def _generate_code() -> str:
|
|
no_ambiguous = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
|
return "".join(random.choices(no_ambiguous, k=CODE_LENGTH))
|