新增了Telegram适配器的全套基础模块,包括: 1. 核心适配器入口与会话工具 2. 账号管理、认证与配置系统 3. 连接相关的轮询、Webhook、更新偏移管理 4. 话题路由、管理与缓存系统 5. 消息反抖动、超时配置与工具类 6. 响应式UI与命令交互系统 7. 反应表情与通知系统 8. 审批与安全审计模块 9. 健康检查与状态监控 10. 贴纸缓存与视觉工具 11. 流式响应与协作功能 12. 群组迁移与目标归一化处理
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
REACTION_VARIANTS: dict[str, list[str]] = {
|
|
"like": ["\u2764\ufe0f", "\U0001f44d", "\U0001f90d", "\U0001f499", "\U0001f49c"],
|
|
"ack": ["\U0001f44d", "\u2705", "\U0001f44c"],
|
|
"processing": ["\u23f3", "\U0001f916", "\U0001f4ac"],
|
|
"done": ["\u2705", "\U0001f44d", "\U0001f389"],
|
|
"error": ["\u274c", "\u26a0\ufe0f", "\U0001f6ab"],
|
|
"thinking": ["\U0001f4ad", "\U0001f9e0", "\U0001f914"],
|
|
"searching": ["\U0001f50d", "\U0001f50e", "\U0001f4da"],
|
|
"generating": ["\u2728", "\U0001f4dd", "\U0001f3a8"],
|
|
}
|
|
|
|
|
|
def get_reaction_variant(action: str, index: int = 0) -> str:
|
|
variants = REACTION_VARIANTS.get(action, ["\u2764\ufe0f"])
|
|
if index < 0:
|
|
return variants[0]
|
|
return variants[index % len(variants)]
|
|
|
|
|
|
def get_all_reaction_actions() -> list[str]:
|
|
return list(REACTION_VARIANTS.keys())
|
|
|
|
|
|
def resolve_reaction_emoji(action_or_emoji: str) -> str:
|
|
if action_or_emoji in REACTION_VARIANTS:
|
|
return get_reaction_variant(action_or_emoji, 0)
|
|
return action_or_emoji
|
|
|
|
|
|
class ReactionNotificationTriggerFilter:
|
|
def __init__(self, config: dict[str, Any] | None = None):
|
|
cfg = config or {}
|
|
react_cfg = cfg.get("reaction_notifications", {})
|
|
if isinstance(react_cfg, dict):
|
|
self._mode = react_cfg.get("mode", "off")
|
|
self._whitelist = react_cfg.get("whitelist", [])
|
|
else:
|
|
self._mode = str(react_cfg) if react_cfg else "off"
|
|
self._whitelist = []
|
|
|
|
def should_notify(self, action: str, chat_id: str | None = None) -> bool:
|
|
if self._mode == "off":
|
|
return False
|
|
if self._mode == "all":
|
|
return True
|
|
if self._mode == "own":
|
|
return True
|
|
if self._mode == "whitelist" and chat_id:
|
|
return chat_id in self._whitelist
|
|
return False
|
|
|
|
def notify_reaction(self, action: str) -> str | None:
|
|
if not self.should_notify(action):
|
|
return None
|
|
return get_reaction_variant(action, 0)
|