If given a message_id to telegram provider send, the bot will try to reply to that message

This commit is contained in:
Darye
2026-02-18 20:27:48 +01:00
parent c865b293a9
commit 3ac5513004

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
import asyncio import asyncio
import re import re
from loguru import logger from loguru import logger
from telegram import BotCommand, Update from telegram import BotCommand, Update, ReplyParameters
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from telegram.request import HTTPXRequest from telegram.request import HTTPXRequest
@@ -224,6 +224,15 @@ class TelegramChannel(BaseChannel):
logger.error(f"Invalid chat_id: {msg.chat_id}") logger.error(f"Invalid chat_id: {msg.chat_id}")
return return
# Build reply parameters (Will reply to the message if it exists)
reply_to_message_id = msg.metadata.get("message_id")
reply_params = None
if reply_to_message_id:
reply_params = ReplyParameters(
message_id=reply_to_message_id,
allow_sending_without_reply=True
)
# Send media files # Send media files
for media_path in (msg.media or []): for media_path in (msg.media or []):
try: try:
@@ -235,22 +244,39 @@ class TelegramChannel(BaseChannel):
}.get(media_type, self._app.bot.send_document) }.get(media_type, self._app.bot.send_document)
param = "photo" if media_type == "photo" else media_type if media_type in ("voice", "audio") else "document" param = "photo" if media_type == "photo" else media_type if media_type in ("voice", "audio") else "document"
with open(media_path, 'rb') as f: with open(media_path, 'rb') as f:
await sender(chat_id=chat_id, **{param: f}) await sender(
chat_id=chat_id,
**{param: f},
reply_parameters=reply_params
)
except Exception as e: except Exception as e:
filename = media_path.rsplit("/", 1)[-1] filename = media_path.rsplit("/", 1)[-1]
logger.error(f"Failed to send media {media_path}: {e}") logger.error(f"Failed to send media {media_path}: {e}")
await self._app.bot.send_message(chat_id=chat_id, text=f"[Failed to send: {filename}]") await self._app.bot.send_message(
chat_id=chat_id,
text=f"[Failed to send: {filename}]",
reply_parameters=reply_params
)
# Send text content # Send text content
if msg.content and msg.content != "[empty message]": if msg.content and msg.content != "[empty message]":
for chunk in _split_message(msg.content): for chunk in _split_message(msg.content):
try: try:
html = _markdown_to_telegram_html(chunk) html = _markdown_to_telegram_html(chunk)
await self._app.bot.send_message(chat_id=chat_id, text=html, parse_mode="HTML") await self._app.bot.send_message(
chat_id=chat_id,
text=html,
parse_mode="HTML",
reply_parameters=reply_params
)
except Exception as e: except Exception as e:
logger.warning(f"HTML parse failed, falling back to plain text: {e}") logger.warning(f"HTML parse failed, falling back to plain text: {e}")
try: try:
await self._app.bot.send_message(chat_id=chat_id, text=chunk) await self._app.bot.send_message(
chat_id=chat_id,
text=chunk,
reply_parameters=reply_params
)
except Exception as e2: except Exception as e2:
logger.error(f"Error sending Telegram message: {e2}") logger.error(f"Error sending Telegram message: {e2}")