ForcePilot/backend/package/yuxi/channel/extensions/alexa/pairing.py
Kris 37493b6da6 feat(alexa): 新增Amazon Alexa渠道插件完整实现
新增了Alexa渠道的所有核心功能模块,包括配置管理、请求校验、会话管理、安全验证、去重、响应格式化、渐进式响应、主动推送、提醒功能、配对绑定以及Webhook端点,并完成了插件注册和配置项定义。
2026-05-21 10:38:31 +08:00

58 lines
1.6 KiB
Python

import random
import time
CODE_LENGTH = 6
CODE_TTL_SECONDS = 600
PENDING_MAX = 3
RATE_LIMIT_SECONDS = 3600
class AlexaPairing:
def __init__(self):
self._pending: dict[str, dict] = {}
self._userid_last_code_at: dict[str, float] = {}
def generate_code(self, user_id: str) -> str | None:
now = time.time()
if user_id in self._userid_last_code_at:
elapsed = now - self._userid_last_code_at[user_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_userid = next(
k for k, v in self._pending.items()
if v == oldest
)
del self._pending[expired_userid]
code = _generate_code()
self._pending[user_id] = {"code": code, "created_at": now}
self._userid_last_code_at[user_id] = now
return code
def verify(self, user_id: str, code: str) -> bool:
entry = self._pending.get(user_id)
if not entry:
return False
if time.time() - entry["created_at"] > CODE_TTL_SECONDS:
del self._pending[user_id]
return False
if entry["code"] != code.upper().strip():
return False
del self._pending[user_id]
return True
def _generate_code() -> str:
no_ambiguous = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
return "".join(random.choices(no_ambiguous, k=CODE_LENGTH))