新增 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: 类型定义
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
|
|
from yuxi.channel.extensions.telegram.format import split_telegram_html_chunks
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class TelegramDelivery:
|
|
def __init__(self, outbound):
|
|
self._outbound = outbound
|
|
|
|
async def deliver_chunks(
|
|
self,
|
|
target_id: str,
|
|
html_chunks: list[str],
|
|
*,
|
|
reply_to_id: str | None = None,
|
|
thread_id: str | None = None,
|
|
account_id: str | None = None,
|
|
) -> list[str]:
|
|
msg_ids: list[str] = []
|
|
for chunk in html_chunks:
|
|
msg_id = await self._outbound.send_text_raw(
|
|
target_id, chunk, reply_to_id=reply_to_id, thread_id=thread_id, account_id=account_id,
|
|
)
|
|
if msg_id:
|
|
msg_ids.append(msg_id)
|
|
if len(html_chunks) > 1:
|
|
await asyncio.sleep(0.3)
|
|
return msg_ids
|
|
|
|
async def deliver_single(
|
|
self,
|
|
target_id: str,
|
|
html: str,
|
|
*,
|
|
reply_to_id: str | None = None,
|
|
thread_id: str | None = None,
|
|
account_id: str | None = None,
|
|
chunk_limit: int = 4096,
|
|
) -> list[str]:
|
|
chunks = split_telegram_html_chunks(html, chunk_limit)
|
|
return await self.deliver_chunks(
|
|
target_id, chunks, reply_to_id=reply_to_id, thread_id=thread_id, account_id=account_id,
|
|
)
|
|
|
|
async def deliver_placeholder_edit(
|
|
self,
|
|
target_id: str,
|
|
html: str,
|
|
*,
|
|
reply_to_id: str | None = None,
|
|
thread_id: str | None = None,
|
|
account_id: str | None = None,
|
|
chunk_limit: int = 4096,
|
|
placeholder_text: str = "⏳ 正在生成回复...",
|
|
) -> list[str]:
|
|
chunks = split_telegram_html_chunks(html, chunk_limit)
|
|
|
|
if len(chunks) <= 1:
|
|
return await self.deliver_single(
|
|
target_id, html,
|
|
reply_to_id=reply_to_id, thread_id=thread_id,
|
|
account_id=account_id, chunk_limit=chunk_limit,
|
|
)
|
|
|
|
placeholder_id = await self._outbound.send_text_raw(
|
|
target_id, placeholder_text,
|
|
reply_to_id=reply_to_id, thread_id=thread_id, account_id=account_id,
|
|
)
|
|
if not placeholder_id:
|
|
return await self.deliver_chunks(
|
|
target_id, chunks,
|
|
reply_to_id=reply_to_id, thread_id=thread_id, account_id=account_id,
|
|
)
|
|
|
|
msg_ids = [placeholder_id]
|
|
accumulated = ""
|
|
for chunk in chunks:
|
|
accumulated += chunk
|
|
if len(accumulated) > chunk_limit:
|
|
accumulated = accumulated[:chunk_limit]
|
|
await self._outbound.edit_message(
|
|
target_id, placeholder_id, accumulated,
|
|
thread_id=thread_id, account_id=account_id,
|
|
)
|
|
if len(chunks) > 1:
|
|
await asyncio.sleep(0.5)
|
|
|
|
return msg_ids
|