feat: 支持 Telegram MarkdownV2 渲染

fixes: #649 #907
This commit is contained in:
Soulter
2025-04-04 20:52:22 +08:00
parent ea7a52945f
commit 72f1ffccd3
4 changed files with 39 additions and 2 deletions
@@ -1,8 +1,10 @@
import telegramify_markdown
from astrbot.api.event import AstrMessageEvent, MessageChain from astrbot.api.event import AstrMessageEvent, MessageChain
from astrbot.api.platform import AstrBotMessage, PlatformMetadata, MessageType from astrbot.api.platform import AstrBotMessage, PlatformMetadata, MessageType
from astrbot.api.message_components import Plain, Image, Reply, At, File, Record from astrbot.api.message_components import Plain, Image, Reply, At, File, Record
from telegram.ext import ExtBot from telegram.ext import ExtBot
from astrbot.core.utils.io import download_file from astrbot.core.utils.io import download_file
from astrbot import logger
class TelegramPlatformEvent(AstrMessageEvent): class TelegramPlatformEvent(AstrMessageEvent):
@@ -49,7 +51,17 @@ class TelegramPlatformEvent(AstrMessageEvent):
if at_user_id and not at_flag: if at_user_id and not at_flag:
i.text = f"@{at_user_id} " + i.text i.text = f"@{at_user_id} " + i.text
at_flag = True 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): elif isinstance(i, Image):
image_path = await i.convert_to_file_path() image_path = await i.convert_to_file_path()
await client.send_photo(photo=image_path, **payload) await client.send_photo(photo=image_path, **payload)
+1
View File
@@ -35,6 +35,7 @@ dependencies = [
"quart>=0.20.0", "quart>=0.20.0",
"readability-lxml>=0.8.1", "readability-lxml>=0.8.1",
"silk-python>=0.2.6", "silk-python>=0.2.6",
"telegramify-markdown>=0.5.0",
"wechatpy>=1.8.18", "wechatpy>=1.8.18",
] ]
+2 -1
View File
@@ -28,4 +28,5 @@ dingtalk-stream
defusedxml defusedxml
mcp mcp
certifi certifi
pip pip
telegramify-markdown
Generated
+23
View File
@@ -225,6 +225,7 @@ dependencies = [
{ name = "quart" }, { name = "quart" },
{ name = "readability-lxml" }, { name = "readability-lxml" },
{ name = "silk-python" }, { name = "silk-python" },
{ name = "telegramify-markdown" },
{ name = "wechatpy" }, { name = "wechatpy" },
] ]
@@ -260,6 +261,7 @@ requires-dist = [
{ name = "quart", specifier = ">=0.20.0" }, { name = "quart", specifier = ">=0.20.0" },
{ name = "readability-lxml", specifier = ">=0.8.1" }, { name = "readability-lxml", specifier = ">=0.8.1" },
{ name = "silk-python", specifier = ">=0.2.6" }, { name = "silk-python", specifier = ">=0.2.6" },
{ name = "telegramify-markdown", specifier = ">=0.5.0" },
{ name = "wechatpy", specifier = ">=1.8.18" }, { 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 }, { 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]] [[package]]
name = "multidict" name = "multidict"
version = "6.2.0" 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 }, { 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]] [[package]]
name = "tomli" name = "tomli"
version = "2.2.1" version = "2.2.1"