新增企业微信、微博、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
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
"""WhatsApp Pairing 适配器 — DM 配对码管理"""
|
|
|
|
import secrets
|
|
import logging
|
|
import time
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_pending_codes: dict[str, tuple[str, float]] = {}
|
|
_CODE_TTL_SECONDS = 300
|
|
|
|
|
|
class WhatsAppPairing:
|
|
id_label = "whatsappSenderId"
|
|
|
|
async def generate_code(self, peer_id: str) -> str:
|
|
code = f"{secrets.randbelow(1_000_000):06d}"
|
|
_pending_codes[peer_id] = (code, time.monotonic())
|
|
logger.info(f"Generated pairing code for {peer_id}")
|
|
_cleanup_expired()
|
|
return code
|
|
|
|
async def verify_code(self, peer_id: str, code: str) -> bool:
|
|
_cleanup_expired()
|
|
entry = _pending_codes.pop(peer_id, None)
|
|
if entry is None:
|
|
logger.warning(f"No pending code for {peer_id}")
|
|
return False
|
|
stored_code, _ = entry
|
|
return stored_code == code
|
|
|
|
def normalize_allow_entry(self, entry: str) -> str:
|
|
stripped = entry.strip().lstrip("+")
|
|
return stripped
|
|
|
|
async def notify_approval(self, config: dict, peer_id: str, account_id: str | None = None) -> None:
|
|
logger.info(f"whatsapp pairing approved for peer {peer_id}")
|
|
|
|
|
|
def _cleanup_expired():
|
|
now = time.monotonic()
|
|
expired = [k for k, (_, ts) in _pending_codes.items() if now - ts > _CODE_TTL_SECONDS]
|
|
for k in expired:
|
|
del _pending_codes[k]
|