feat: 添加了对markdown的简单封装

This commit is contained in:
远野千束 2024-03-19 22:43:55 +08:00
parent 0bf56f79f1
commit ab5dc2200a

View File

@ -2,11 +2,12 @@ import nonebot
from nonebot.adapters.onebot import v11, v12 from nonebot.adapters.onebot import v11, v12
from typing_extensions import Any from typing_extensions import Any
from .tools import de_escape
from .typing import T_Bot from .typing import T_Bot
async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id: str) -> dict[str, Any]: async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id: str) -> dict[str, Any]:
markdown = markdown.replace("\n", r"\n").replace("\"", r'\\\"') formatted_md = de_escape(markdown).replace("\n", r"\n").replace("\"", r'\\\"')
forward_data = await bot.call_api( forward_data = await bot.call_api(
api="send_private_forward_msg", api="send_private_forward_msg",
user_id=bot.self_id, user_id=bot.self_id,
@ -20,7 +21,7 @@ async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id
{ {
"type": "markdown", "type": "markdown",
"data": { "data": {
"content": '{"content":"%s"}' % markdown "content": '{"content":"%s"}' % formatted_md
} }
} }
] ]
@ -29,6 +30,7 @@ async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id
] ]
) )
try:
data = await bot.send_msg( data = await bot.send_msg(
message_type=message_type, message_type=message_type,
message=[ message=[
@ -37,10 +39,19 @@ async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id
data={ data={
"id": forward_data["forward_id"] "id": forward_data["forward_id"]
} }
) ),
], ],
)
except Exception as e:
nonebot.logger.warning("send_markdown error, send as plane text: %s", e)
data = await bot.send_msg(
message_type=message_type,
message=markdown,
user_id=session_id if message_type == "private" else None, user_id=session_id if message_type == "private" else None,
group_id=session_id if message_type == "group" else None group_id=session_id if message_type == "group" else None
) )
nonebot.logger.info("已发送md%s" % forward_data["forward_id"])
return data return data