diff --git a/astrbot/core/platform/sources/telegram/tg_event.py b/astrbot/core/platform/sources/telegram/tg_event.py index 87fea26c6..eab41ad84 100644 --- a/astrbot/core/platform/sources/telegram/tg_event.py +++ b/astrbot/core/platform/sources/telegram/tg_event.py @@ -1,8 +1,10 @@ +import telegramify_markdown from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.platform import AstrBotMessage, PlatformMetadata, MessageType from astrbot.api.message_components import Plain, Image, Reply, At, File, Record from telegram.ext import ExtBot from astrbot.core.utils.io import download_file +from astrbot import logger class TelegramPlatformEvent(AstrMessageEvent): @@ -49,7 +51,17 @@ class TelegramPlatformEvent(AstrMessageEvent): if at_user_id and not at_flag: i.text = f"@{at_user_id} " + i.text at_flag = True - await client.send_message(text=i.text, **payload) + text = i.text + try: + text = telegramify_markdown.markdownify( + i.text, max_line_length=None, normalize_whitespace=False + ) + except Exception as e: + logger.warning( + f"MarkdownV2 conversion failed: {e}. Using plain text instead." + ) + return + await client.send_message(text=text, parse_mode="MarkdownV2", **payload) elif isinstance(i, Image): image_path = await i.convert_to_file_path() await client.send_photo(photo=image_path, **payload) diff --git a/pyproject.toml b/pyproject.toml index 20f61c95b..93ad29f42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,6 +35,7 @@ dependencies = [ "quart>=0.20.0", "readability-lxml>=0.8.1", "silk-python>=0.2.6", + "telegramify-markdown>=0.5.0", "wechatpy>=1.8.18", ] diff --git a/requirements.txt b/requirements.txt index 437e0807b..e20771e86 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,4 +28,5 @@ dingtalk-stream defusedxml mcp certifi -pip \ No newline at end of file +pip +telegramify-markdown \ No newline at end of file diff --git a/uv.lock b/uv.lock index 835b323b3..445941a7d 100644 --- a/uv.lock +++ b/uv.lock @@ -225,6 +225,7 @@ dependencies = [ { name = "quart" }, { name = "readability-lxml" }, { name = "silk-python" }, + { name = "telegramify-markdown" }, { name = "wechatpy" }, ] @@ -260,6 +261,7 @@ requires-dist = [ { name = "quart", specifier = ">=0.20.0" }, { name = "readability-lxml", specifier = ">=0.8.1" }, { name = "silk-python", specifier = ">=0.2.6" }, + { name = "telegramify-markdown", specifier = ">=0.5.0" }, { name = "wechatpy", specifier = ">=1.8.18" }, ] @@ -1059,6 +1061,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/c1/d1/3ff566ecf322077d861f1a68a1ff025cad337417bd66ad22a7c6f7dfcfaf/mcp-1.5.0-py3-none-any.whl", hash = "sha256:51c3f35ce93cb702f7513c12406bbea9665ef75a08db909200b07da9db641527", size = 73734 }, ] +[[package]] +name = "mistletoe" +version = "1.4.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/11/96/ea46a376a7c4cd56955ecdfff0ea68de43996a4e6d1aee4599729453bd11/mistletoe-1.4.0.tar.gz", hash = "sha256:1630f906e5e4bbe66fdeb4d29d277e2ea515d642bb18a9b49b136361a9818c9d", size = 107203 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2a/0f/b5e545f0c7962be90366af3418989b12cf441d9da1e5d89d88f2f3e5cf8f/mistletoe-1.4.0-py3-none-any.whl", hash = "sha256:44a477803861de1237ba22e375c6b617690a31d2902b47279d1f8f7ed498a794", size = 51304 }, +] + [[package]] name = "multidict" version = "6.2.0" @@ -1792,6 +1803,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/d1/b1/74babcc824a57904e919f3af16d86c08b524c0691504baf038ef2d7f655c/taskgroup-0.2.2-py2.py3-none-any.whl", hash = "sha256:e2c53121609f4ae97303e9ea1524304b4de6faf9eb2c9280c7f87976479a52fb", size = 14237 }, ] +[[package]] +name = "telegramify-markdown" +version = "0.5.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "mistletoe" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/f6/b7/a9b56f856f87e1b4d743932353f71811844a413561be180a22d667ef6f5a/telegramify_markdown-0.5.0.tar.gz", hash = "sha256:70e6eff7e341e6e9c8818fa1ec53a4e25e4f5e3ef50856d7772760fc6b7a4066", size = 36017 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/df/87/4a39dde5b4aea91b874cbbe057edea19334e62651ce0f1f74f5a1f721439/telegramify_markdown-0.5.0-py3-none-any.whl", hash = "sha256:6f66b7029c0eba268fed5f9daf9216f56c588c6202dd591ff572f7df0d318f2f", size = 32389 }, +] + [[package]] name = "tomli" version = "2.2.1"