新增 KakaoTalk 渠道扩展,支持在 Yuxi 平台中集成 KakaoTalk 即时通讯渠道。 包含以下功能模块: - bot: Bot 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - card_builder: KakaoTalk 卡片消息构建 - quick_reply: 快捷回复处理 - types: 类型定义
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import secrets
|
|
import time
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class KakaoTalkPairingAdapter:
|
|
id_label = "botUserKey"
|
|
|
|
def __init__(self):
|
|
self._pending_codes: dict[str, tuple[str, float]] = {}
|
|
self._code_ttl = 600
|
|
|
|
async def generate_code(self, peer_id: str) -> str:
|
|
code = f"{secrets.randbelow(1_000_000):06d}"
|
|
self._pending_codes[peer_id] = (code, time.monotonic())
|
|
return code
|
|
|
|
async def verify_code(self, peer_id: str, code: str) -> bool:
|
|
entry = self._pending_codes.get(peer_id)
|
|
if not entry:
|
|
return False
|
|
|
|
stored_code, generated_at = entry
|
|
if time.monotonic() - generated_at > self._code_ttl:
|
|
del self._pending_codes[peer_id]
|
|
return False
|
|
|
|
if stored_code == code:
|
|
del self._pending_codes[peer_id]
|
|
return True
|
|
return False
|
|
|
|
def normalize_allow_entry(self, entry: str) -> str:
|
|
stripped = entry.strip()
|
|
if stripped.startswith("kakaotalk:"):
|
|
return stripped[len("kakaotalk:"):]
|
|
return stripped
|
|
|
|
async def notify_approval(self, config: dict, peer_id: str, account_id: str | None = None) -> None:
|
|
logger.info("KakaoTalk pairing approved for peer %s", peer_id) |