新增 KakaoTalk 渠道扩展,支持在 Yuxi 平台中集成 KakaoTalk 即时通讯渠道。 包含以下功能模块: - bot: Bot 客户端封装 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - card_builder: KakaoTalk 卡片消息构建 - quick_reply: 快捷回复处理 - types: 类型定义
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class KakaoTalkDeduplicator:
|
|
|
|
def __init__(self, max_size: int = 4096, ttl_seconds: int = 600):
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
|
self._max_size = max_size
|
|
self._ttl = ttl_seconds
|
|
|
|
def is_duplicate(self, event_key: str) -> bool:
|
|
if not event_key:
|
|
return False
|
|
|
|
now = time.monotonic()
|
|
self._evict_expired(now)
|
|
|
|
if event_key in self._cache:
|
|
return True
|
|
|
|
self._cache[event_key] = now
|
|
while len(self._cache) > self._max_size:
|
|
self._cache.popitem(last=False)
|
|
|
|
return False
|
|
|
|
def _evict_expired(self, now: float) -> None:
|
|
expired = [k for k, v in self._cache.items() if now - v > self._ttl]
|
|
for k in expired:
|
|
del self._cache[k]
|
|
|
|
def reset(self) -> None:
|
|
self._cache.clear()
|
|
|
|
|
|
def build_dedupe_key(account_id: str, skill_request) -> str:
|
|
from yuxi.channel.extensions.kakaotalk.types import SkillRequest
|
|
|
|
if isinstance(skill_request, SkillRequest):
|
|
ur = skill_request.user_request
|
|
raw = f"{account_id}|{ur.user.bot_user_key}|{ur.utterance}"
|
|
return hashlib.sha256(raw.encode()).hexdigest()
|
|
|
|
return hashlib.sha256(f"{account_id}|{str(skill_request)}".encode()).hexdigest() |