新增 Telegram 渠道扩展,支持在 Yuxi 平台中集成 Telegram 即时通讯渠道。 包含以下功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - polling: 长轮询模式 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - actions: 动作处理 - inline_keyboard: 内联键盘 - native_commands: 原生指令 - chat: 聊天管理 - delivery: 消息送达确认 - media: 媒体资源处理 - profile: 用户资料 - reactions: 表情反应 - sticker: 贴纸处理 - types: 类型定义
52 lines
1.3 KiB
Python
52 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
class TelegramDeduplicator:
|
|
def __init__(self, max_size: int = 10000, ttl_seconds: int = 25560):
|
|
self._cache: OrderedDict[str, float] = OrderedDict()
|
|
self._max_size = max_size
|
|
self._ttl_seconds = ttl_seconds
|
|
|
|
def is_duplicate(self, key: str) -> bool:
|
|
if not key:
|
|
return False
|
|
|
|
now = time.monotonic()
|
|
self._evict_expired(now)
|
|
|
|
if key in self._cache:
|
|
return True
|
|
|
|
self._cache[key] = now
|
|
while len(self._cache) > self._max_size:
|
|
self._cache.popitem(last=False)
|
|
|
|
return False
|
|
|
|
def mark_seen(self, key: str) -> None:
|
|
if not key:
|
|
return
|
|
now = time.monotonic()
|
|
self._evict_expired(now)
|
|
self._cache[key] = now
|
|
while len(self._cache) > self._max_size:
|
|
self._cache.popitem(last=False)
|
|
|
|
def reset(self) -> None:
|
|
self._cache.clear()
|
|
|
|
@property
|
|
def ttl_seconds(self) -> int:
|
|
return self._ttl_seconds
|
|
|
|
@property
|
|
def max_entries(self) -> int:
|
|
return self._max_size
|
|
|
|
def _evict_expired(self, now: float) -> None:
|
|
expired = [k for k, v in self._cache.items() if now - v > self._ttl_seconds]
|
|
for k in expired:
|
|
del self._cache[k] |