mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 23:47:41 +08:00
fix telegram markdown
This commit is contained in:
@@ -7,10 +7,10 @@ from typing import Optional, List, Dict, Callable
|
|||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
import telebot
|
import telebot
|
||||||
|
import telegramify_markdown
|
||||||
from telebot import apihelper
|
from telebot import apihelper
|
||||||
from telebot.types import InputFile, InlineKeyboardMarkup, InlineKeyboardButton
|
from telebot.types import InputFile, InlineKeyboardMarkup, InlineKeyboardButton
|
||||||
from telebot.types import InputMediaPhoto
|
from telebot.types import InputMediaPhoto
|
||||||
import telegramify_markdown
|
|
||||||
|
|
||||||
from app.core.config import settings
|
from app.core.config import settings
|
||||||
from app.core.context import MediaInfo, Context
|
from app.core.context import MediaInfo, Context
|
||||||
@@ -237,7 +237,6 @@ class Telegram:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 构建完整的 Markdown 文本,然后统一转换
|
|
||||||
if title and text:
|
if title and text:
|
||||||
caption = f"**{title}**\n{text}"
|
caption = f"**{title}**\n{text}"
|
||||||
elif title:
|
elif title:
|
||||||
@@ -250,10 +249,6 @@ class Telegram:
|
|||||||
if link:
|
if link:
|
||||||
caption = f"{caption}\n[查看详情]({link})"
|
caption = f"{caption}\n[查看详情]({link})"
|
||||||
|
|
||||||
# 使用 telegramify-markdown 转换整个文本
|
|
||||||
if caption:
|
|
||||||
caption = self.escape_markdown_smart(caption)
|
|
||||||
|
|
||||||
# Determine target chat_id with improved logic using user mapping
|
# Determine target chat_id with improved logic using user mapping
|
||||||
chat_id = self._determine_target_chat_id(userid, original_chat_id)
|
chat_id = self._determine_target_chat_id(userid, original_chat_id)
|
||||||
|
|
||||||
@@ -316,7 +311,6 @@ class Telegram:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 构建完整的 Markdown 文本,然后统一转换
|
|
||||||
index, image, caption = 1, "", f"**{title}**" if title else ""
|
index, image, caption = 1, "", f"**{title}**" if title else ""
|
||||||
for media in medias:
|
for media in medias:
|
||||||
if not image:
|
if not image:
|
||||||
@@ -339,10 +333,6 @@ class Telegram:
|
|||||||
if link:
|
if link:
|
||||||
caption = f"{caption}\n[查看详情]({link})"
|
caption = f"{caption}\n[查看详情]({link})"
|
||||||
|
|
||||||
# 使用 telegramify-markdown 转换整个文本
|
|
||||||
if caption:
|
|
||||||
caption = self.escape_markdown_smart(caption)
|
|
||||||
|
|
||||||
# Determine target chat_id with improved logic using user mapping
|
# Determine target chat_id with improved logic using user mapping
|
||||||
chat_id = self._determine_target_chat_id(userid, original_chat_id)
|
chat_id = self._determine_target_chat_id(userid, original_chat_id)
|
||||||
|
|
||||||
@@ -382,7 +372,6 @@ class Telegram:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 构建完整的 Markdown 文本,然后统一转换
|
|
||||||
index, caption = 1, f"**{title}**" if title else ""
|
index, caption = 1, f"**{title}**" if title else ""
|
||||||
image = torrents[0].media_info.get_message_image()
|
image = torrents[0].media_info.get_message_image()
|
||||||
for context in torrents:
|
for context in torrents:
|
||||||
@@ -404,10 +393,6 @@ class Telegram:
|
|||||||
if link:
|
if link:
|
||||||
caption = f"{caption}\n[查看详情]({link})"
|
caption = f"{caption}\n[查看详情]({link})"
|
||||||
|
|
||||||
# 使用 telegramify-markdown 转换整个文本
|
|
||||||
if caption:
|
|
||||||
caption = self.escape_markdown_smart(caption)
|
|
||||||
|
|
||||||
# Determine target chat_id with improved logic using user mapping
|
# Determine target chat_id with improved logic using user mapping
|
||||||
chat_id = self._determine_target_chat_id(userid, original_chat_id)
|
chat_id = self._determine_target_chat_id(userid, original_chat_id)
|
||||||
|
|
||||||
@@ -565,21 +550,39 @@ class Telegram:
|
|||||||
if ret is None:
|
if ret is None:
|
||||||
raise RetryException("发送图片消息失败")
|
raise RetryException("发送图片消息失败")
|
||||||
return True
|
return True
|
||||||
# 按4096分段循环发送消息
|
# 使用 telegramify-markdown 的 telegramify 函数智能分割长消息
|
||||||
|
# telegramify 会自动处理 Markdown 格式转换和智能分割,避免在格式标记中间分割
|
||||||
ret = None
|
ret = None
|
||||||
if len(caption) > 4095:
|
try:
|
||||||
for i in range(0, len(caption), 4095):
|
# 使用 telegramify 处理原始 Markdown 文本
|
||||||
|
# telegramify 会同时完成格式转换和智能分割
|
||||||
|
# 如果文本已经转换过,也可以直接传入,telegramify 会处理
|
||||||
|
chunks = list(telegramify_markdown.telegramify(
|
||||||
|
caption,
|
||||||
|
max_length=4095, # Telegram 消息最大长度
|
||||||
|
interpreter=None # 不使用代码块解释器,直接发送文本
|
||||||
|
))
|
||||||
|
|
||||||
|
if not chunks:
|
||||||
|
# 如果没有分割,使用 markdownify 转换后直接发送
|
||||||
|
converted = telegramify_markdown.markdownify(
|
||||||
|
caption,
|
||||||
|
max_line_length=None,
|
||||||
|
normalize_whitespace=False
|
||||||
|
)
|
||||||
|
chunks = [converted]
|
||||||
|
|
||||||
|
# 发送所有消息块(telegramify 已经转换过格式,直接发送)
|
||||||
|
for i, chunk in enumerate(chunks):
|
||||||
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id,
|
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id,
|
||||||
text=caption[i:i + 4095],
|
text=chunk,
|
||||||
parse_mode="MarkdownV2",
|
parse_mode="MarkdownV2",
|
||||||
reply_markup=reply_markup if i == 0 else None)
|
reply_markup=reply_markup if i == 0 else None)
|
||||||
else:
|
if ret is None:
|
||||||
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id,
|
raise RetryException(f"发送文本消息失败(第 {i + 1}/{len(chunks)} 条)")
|
||||||
text=caption,
|
except Exception as e:
|
||||||
parse_mode="MarkdownV2",
|
logger.warning(f"Telegram 发送消息失败:{e}")
|
||||||
reply_markup=reply_markup)
|
|
||||||
if ret is None:
|
|
||||||
raise RetryException("发送文本消息失败")
|
|
||||||
return True if ret else False
|
return True if ret else False
|
||||||
|
|
||||||
def register_commands(self, commands: Dict[str, dict]):
|
def register_commands(self, commands: Dict[str, dict]):
|
||||||
|
|||||||
Reference in New Issue
Block a user