Skip to content

Commit c83589c

Browse files
committed
rollback telegram
1 parent d64492b commit c83589c

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

app/modules/telegram/telegram.py

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from urllib.parse import urljoin
88

99
import telebot
10-
import telegramify_markdown
1110
from telebot import apihelper
1211
from telebot.types import InputFile, InlineKeyboardMarkup, InlineKeyboardButton
1312
from telebot.types import InputMediaPhoto
@@ -53,7 +52,7 @@ def __init__(self, TELEGRAM_TOKEN: Optional[str] = None, TELEGRAM_CHAT_ID: Optio
5352
else:
5453
apihelper.proxy = settings.PROXY
5554
# bot
56-
_bot = telebot.TeleBot(self._telegram_token, parse_mode="MarkdownV2")
55+
_bot = telebot.TeleBot(self._telegram_token, parse_mode="Markdown")
5756
# 记录句柄
5857
self._bot = _bot
5958
# 获取并存储bot用户名用于@检测
@@ -237,14 +236,12 @@ def send_msg(self, title: str, text: Optional[str] = None, image: Optional[str]
237236
return False
238237

239238
try:
240-
if title and text:
241-
caption = f"**{title}**\n{text}"
242-
elif title:
243-
caption = f"**{title}**"
244-
elif text:
245-
caption = text
239+
if text:
240+
# 对text进行Markdown特殊字符转义
241+
text = re.sub(r"([_`])", r"\\\1", text)
242+
caption = f"*{title}*\n{text}"
246243
else:
247-
caption = ""
244+
caption = f"*{title}*"
248245

249246
if link:
250247
caption = f"{caption}\n[查看详情]({link})"
@@ -266,7 +263,7 @@ def send_msg(self, title: str, text: Optional[str] = None, image: Optional[str]
266263
return self.__send_request(userid=chat_id, image=image, caption=caption, reply_markup=reply_markup)
267264

268265
except Exception as msg_e:
269-
logger.error(f"使用 send_msg 发送消息失败:{msg_e}")
266+
logger.error(f"发送消息失败:{msg_e}")
270267
return False
271268

272269
def _determine_target_chat_id(self, userid: Optional[str] = None,
@@ -311,7 +308,7 @@ def send_medias_msg(self, medias: List[MediaInfo], userid: Optional[str] = None,
311308
return None
312309

313310
try:
314-
index, image, caption = 1, "", f"**{title}**" if title else ""
311+
index, image, caption = 1, "", "*%s*" % title
315312
for media in medias:
316313
if not image:
317314
image = media.get_message_image()
@@ -350,7 +347,7 @@ def send_medias_msg(self, medias: List[MediaInfo], userid: Optional[str] = None,
350347
return self.__send_request(userid=chat_id, image=image, caption=caption, reply_markup=reply_markup)
351348

352349
except Exception as msg_e:
353-
logger.error(f"使用 send_medias_msg 发送消息失败:{msg_e}")
350+
logger.error(f"发送消息失败:{msg_e}")
354351
return False
355352

356353
def send_torrents_msg(self, torrents: List[Context],
@@ -372,7 +369,7 @@ def send_torrents_msg(self, torrents: List[Context],
372369
return None
373370

374371
try:
375-
index, caption = 1, f"**{title}**" if title else ""
372+
index, caption = 1, "*%s*" % title
376373
image = torrents[0].media_info.get_message_image()
377374
for context in torrents:
378375
torrent = context.torrent_info
@@ -410,7 +407,7 @@ def send_torrents_msg(self, torrents: List[Context],
410407
return self.__send_request(userid=chat_id, image=image, caption=caption, reply_markup=reply_markup)
411408

412409
except Exception as msg_e:
413-
logger.error(f"使用 send_torrents_msg 发送消息失败:{msg_e}")
410+
logger.error(f"发送消息失败:{msg_e}")
414411
return False
415412

416413
@staticmethod
@@ -502,7 +499,7 @@ def __edit_message(self, chat_id: str, message_id: int, text: str,
502499

503500
if image:
504501
# 如果有图片,使用edit_message_media
505-
media = InputMediaPhoto(media=image, caption=text, parse_mode="MarkdownV2")
502+
media = InputMediaPhoto(media=image, caption=text, parse_mode="Markdown")
506503
self._bot.edit_message_media(
507504
chat_id=chat_id,
508505
message_id=message_id,
@@ -515,7 +512,7 @@ def __edit_message(self, chat_id: str, message_id: int, text: str,
515512
chat_id=chat_id,
516513
message_id=message_id,
517514
text=text,
518-
parse_mode="MarkdownV2",
515+
parse_mode="Markdown",
519516
reply_markup=reply_markup
520517
)
521518
return True
@@ -545,37 +542,26 @@ def __send_request(self, userid: Optional[str] = None, image="", caption="",
545542
ret = self._bot.send_photo(chat_id=userid or self._telegram_chat_id,
546543
photo=photo,
547544
caption=caption,
548-
parse_mode="MarkdownV2",
545+
parse_mode="Markdown",
549546
reply_markup=reply_markup)
550547
if ret is None:
551548
raise RetryException("发送图片消息失败")
552549
return True
553-
# 使用 telegramify-markdown 的 telegramify 函数智能分割长消息
554-
# telegramify 会自动处理 Markdown 格式转换和智能分割,避免在格式标记中间分割
550+
# 按4096分段循环发送消息
555551
ret = None
556-
try:
557-
# 使用 telegramify 处理原始 Markdown 文本
558-
chunks = list(telegramify_markdown.markdownify(
559-
caption,
560-
max_line_length=4095
561-
))
562-
563-
if not chunks:
564-
# 如果没有分割,使用 markdownify 转换后直接发送
565-
converted = telegramify_markdown.markdownify(caption)
566-
chunks = [converted]
567-
568-
# 发送所有消息块(telegramify 已经转换过格式,直接发送)
569-
for i, chunk in enumerate(chunks):
552+
if len(caption) > 4095:
553+
for i in range(0, len(caption), 4095):
570554
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id,
571-
text=chunk,
572-
parse_mode="MarkdownV2",
555+
text=caption[i:i + 4095],
556+
parse_mode="Markdown",
573557
reply_markup=reply_markup if i == 0 else None)
574-
if ret is None:
575-
raise RetryException(f"发送文本消息失败(第 {i + 1}/{len(chunks)} 条)")
576-
except Exception as e:
577-
logger.warning(f"Telegram 发送消息失败:{e}")
578-
558+
else:
559+
ret = self._bot.send_message(chat_id=userid or self._telegram_chat_id,
560+
text=caption,
561+
parse_mode="Markdown",
562+
reply_markup=reply_markup)
563+
if ret is None:
564+
raise RetryException("发送文本消息失败")
579565
return True if ret else False
580566

581567
def register_commands(self, commands: Dict[str, dict]):

0 commit comments

Comments
 (0)