新增 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: 类型定义
79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from yuxi.channel.extensions.telegram.actions import ReactionLevel
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
ACK_EMOJI = "👀"
|
|
DONE_EMOJI = "✅"
|
|
ERROR_EMOJI = "❌"
|
|
|
|
|
|
class TelegramReactions:
|
|
def __init__(self):
|
|
self._pending_acks: dict[str, str] = {}
|
|
|
|
def resolve_emoji(self, level: str, phase: str = "ack") -> str | None:
|
|
if level == ReactionLevel.OFF:
|
|
return None
|
|
if level == ReactionLevel.ACK and phase == "ack":
|
|
return ACK_EMOJI
|
|
if level == ReactionLevel.MINIMAL:
|
|
return {"ack": ACK_EMOJI, "done": DONE_EMOJI, "error": ERROR_EMOJI}.get(phase)
|
|
return None
|
|
|
|
async def send_ack(
|
|
self, outbound, target_id: str, message_id: str, level: str,
|
|
account_id: str | None = None,
|
|
) -> None:
|
|
emoji = self.resolve_emoji(level, "ack")
|
|
if not emoji:
|
|
return
|
|
try:
|
|
await outbound.send_reaction(target_id, message_id, emoji, account_id=account_id)
|
|
self._pending_acks[message_id] = emoji
|
|
except Exception:
|
|
logger.debug("Failed to send ack reaction", exc_info=True)
|
|
|
|
async def clear_ack(
|
|
self, outbound, target_id: str, message_id: str,
|
|
account_id: str | None = None,
|
|
) -> None:
|
|
if message_id not in self._pending_acks:
|
|
return
|
|
try:
|
|
await outbound.send_reaction(target_id, message_id, "", account_id=account_id)
|
|
self._pending_acks.pop(message_id, None)
|
|
except Exception:
|
|
logger.debug("Failed to clear ack reaction", exc_info=True)
|
|
|
|
async def send_done(
|
|
self, outbound, target_id: str, message_id: str, level: str,
|
|
account_id: str | None = None,
|
|
) -> None:
|
|
if level == ReactionLevel.ACK:
|
|
return await self.clear_ack(outbound, target_id, message_id, account_id)
|
|
|
|
emoji = self.resolve_emoji(level, "done")
|
|
if not emoji:
|
|
return
|
|
try:
|
|
await outbound.send_reaction(target_id, message_id, emoji, account_id=account_id)
|
|
self._pending_acks.pop(message_id, None)
|
|
except Exception:
|
|
logger.debug("Failed to send done reaction", exc_info=True)
|
|
|
|
async def send_error(
|
|
self, outbound, target_id: str, message_id: str, level: str,
|
|
account_id: str | None = None,
|
|
) -> None:
|
|
emoji = self.resolve_emoji(level, "error")
|
|
if not emoji:
|
|
return
|
|
try:
|
|
await outbound.send_reaction(target_id, message_id, emoji, account_id=account_id)
|
|
except Exception:
|
|
logger.debug("Failed to send error reaction", exc_info=True)
|