2024-03-19 21:56:31 +08:00
|
|
|
import nonebot
|
|
|
|
from nonebot.adapters.onebot import v11, v12
|
|
|
|
from typing_extensions import Any
|
|
|
|
|
2024-03-19 22:43:55 +08:00
|
|
|
from .tools import de_escape
|
2024-03-19 21:56:31 +08:00
|
|
|
from .typing import T_Bot
|
|
|
|
|
|
|
|
|
2024-03-20 00:44:36 +08:00
|
|
|
async def send_markdown(markdown: str, bot: T_Bot, message_type: str, session_id: str | int) -> dict[str, Any]:
|
2024-03-19 22:43:55 +08:00
|
|
|
formatted_md = de_escape(markdown).replace("\n", r"\n").replace("\"", r'\\\"')
|
|
|
|
try:
|
2024-03-20 00:44:36 +08:00
|
|
|
forward_data = await bot.call_api(
|
|
|
|
api="send_private_forward_msg",
|
|
|
|
user_id=bot.self_id,
|
|
|
|
messages=[
|
|
|
|
v11.MessageSegment(
|
|
|
|
type="node",
|
|
|
|
data={
|
|
|
|
"name" : "Liteyuki.OneBot",
|
|
|
|
"uin" : bot.self_id,
|
|
|
|
"content": [
|
|
|
|
{
|
|
|
|
"type": "markdown",
|
|
|
|
"data": {
|
|
|
|
"content": '{"content":"%s"}' % formatted_md
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2024-03-19 22:43:55 +08:00
|
|
|
data = await bot.send_msg(
|
2024-03-20 00:44:36 +08:00
|
|
|
user_id=session_id,
|
|
|
|
group_id=session_id,
|
2024-03-19 22:43:55 +08:00
|
|
|
message_type=message_type,
|
|
|
|
message=[
|
2024-03-20 00:44:36 +08:00
|
|
|
v11.MessageSegment(
|
|
|
|
type="longmsg",
|
|
|
|
data={
|
|
|
|
"id": forward_data["forward_id"]
|
|
|
|
}
|
|
|
|
),
|
2024-03-19 22:43:55 +08:00
|
|
|
],
|
|
|
|
|
|
|
|
)
|
|
|
|
except Exception as e:
|
2024-03-20 00:44:36 +08:00
|
|
|
nonebot.logger.warning("send_markdown error, send as plane text: %s" % e)
|
|
|
|
if isinstance(bot, v11.Bot):
|
|
|
|
data = await bot.send_msg(
|
|
|
|
message_type=message_type,
|
|
|
|
message=markdown,
|
|
|
|
user_id=int(session_id),
|
|
|
|
group_id=int(session_id)
|
|
|
|
)
|
|
|
|
elif isinstance(bot, v12.Bot):
|
|
|
|
data = await bot.send_message(
|
|
|
|
detail_type=message_type,
|
|
|
|
message=v12.Message(
|
|
|
|
v12.MessageSegment.text(
|
|
|
|
text=markdown
|
|
|
|
)
|
|
|
|
),
|
|
|
|
user_id=str(session_id),
|
|
|
|
group_id=str(session_id)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
nonebot.logger.error("send_markdown: bot type not supported")
|
|
|
|
data = {}
|
2024-03-19 21:56:31 +08:00
|
|
|
return data
|