2024-03-26 17:14:41 +08:00
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
|
2024-03-19 21:56:31 +08:00
|
|
|
|
import nonebot
|
|
|
|
|
from nonebot.adapters.onebot import v11, v12
|
2024-03-23 19:55:12 +08:00
|
|
|
|
from typing import Any
|
|
|
|
|
from .ly_typing import T_Bot, T_MessageEvent
|
2024-03-19 21:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-23 19:55:12 +08:00
|
|
|
|
async def send_markdown(markdown: str, bot: T_Bot, *, message_type: str = None, session_id: str | int = None, event: T_MessageEvent = None, **kwargs) -> dict[
|
2024-03-26 17:14:41 +08:00
|
|
|
|
str, Any]:
|
|
|
|
|
formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace("\"", r'\\\"')
|
2024-03-20 18:27:49 +08:00
|
|
|
|
if event is not None and message_type is None:
|
|
|
|
|
message_type = event.message_type
|
|
|
|
|
session_id = event.user_id if event.message_type == "private" else event.group_id
|
2024-03-19 22:43:55 +08:00
|
|
|
|
try:
|
2024-03-23 19:55:12 +08:00
|
|
|
|
forward_id = await bot.call_api(
|
|
|
|
|
api="send_forward_msg",
|
2024-03-20 00:44:36 +08:00
|
|
|
|
messages=[
|
|
|
|
|
v11.MessageSegment(
|
|
|
|
|
type="node",
|
|
|
|
|
data={
|
|
|
|
|
"name" : "Liteyuki.OneBot",
|
|
|
|
|
"uin" : bot.self_id,
|
|
|
|
|
"content": [
|
|
|
|
|
{
|
|
|
|
|
"type": "markdown",
|
|
|
|
|
"data": {
|
|
|
|
|
"content": '{"content":"%s"}' % formatted_md
|
|
|
|
|
}
|
2024-03-23 19:55:12 +08:00
|
|
|
|
},
|
2024-03-20 00:44:36 +08:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
)
|
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={
|
2024-03-23 19:55:12 +08:00
|
|
|
|
"id": forward_id
|
2024-03-20 00:44:36 +08:00
|
|
|
|
}
|
|
|
|
|
),
|
2024-03-19 22:43:55 +08:00
|
|
|
|
],
|
2024-03-22 12:41:38 +08:00
|
|
|
|
**kwargs
|
2024-03-19 22:43:55 +08:00
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
2024-03-23 19:55:12 +08:00
|
|
|
|
nonebot.logger.warning("send_markdown error, send as plain text: %s" % e.__repr__())
|
2024-03-20 00:44:36 +08:00
|
|
|
|
if isinstance(bot, v11.Bot):
|
|
|
|
|
data = await bot.send_msg(
|
|
|
|
|
message_type=message_type,
|
|
|
|
|
message=markdown,
|
|
|
|
|
user_id=int(session_id),
|
2024-03-22 12:41:38 +08:00
|
|
|
|
group_id=int(session_id),
|
|
|
|
|
**kwargs
|
2024-03-20 00:44:36 +08:00
|
|
|
|
)
|
|
|
|
|
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),
|
2024-03-22 12:41:38 +08:00
|
|
|
|
group_id=str(session_id),
|
|
|
|
|
**kwargs
|
2024-03-20 00:44:36 +08:00
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
nonebot.logger.error("send_markdown: bot type not supported")
|
|
|
|
|
data = {}
|
2024-03-20 18:27:49 +08:00
|
|
|
|
return data
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
class Markdown:
|
|
|
|
|
@staticmethod
|
|
|
|
|
def button(name: str, cmd: str, reply: bool = False, enter: bool = True) -> str:
|
2024-03-22 07:44:41 +08:00
|
|
|
|
"""生成点击回调按钮
|
2024-03-21 12:10:24 +08:00
|
|
|
|
Args:
|
|
|
|
|
name: 按钮显示内容
|
|
|
|
|
cmd: 发送的命令,已在函数内url编码,不需要再次编码
|
|
|
|
|
reply: 是否以回复的方式发送消息
|
|
|
|
|
enter: 自动发送消息则为True,否则填充到输入框
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
Returns:
|
|
|
|
|
markdown格式的可点击回调按钮
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
"""
|
2024-03-26 17:14:41 +08:00
|
|
|
|
return f"[{name}](mqqapi://aio/inlinecmd?command={quote(cmd)}&reply={str(reply).lower()}&enter={str(enter).lower()})"
|
2024-03-21 12:10:24 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def link(name: str, url: str) -> str:
|
2024-03-22 07:44:41 +08:00
|
|
|
|
"""生成点击链接按钮
|
2024-03-21 12:10:24 +08:00
|
|
|
|
Args:
|
|
|
|
|
name: 链接显示内容
|
|
|
|
|
url: 链接地址
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
markdown格式的链接
|
|
|
|
|
|
|
|
|
|
"""
|
2024-03-21 13:02:08 +08:00
|
|
|
|
return f"[🔗{name}]({url})"
|
2024-03-22 07:44:41 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def escape(text: str) -> str:
|
|
|
|
|
"""转义特殊字符
|
|
|
|
|
Args:
|
|
|
|
|
text: 需要转义的文本,请勿直接把整个markdown文本传入,否则会转义掉所有字符
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
转义后的文本
|
|
|
|
|
|
|
|
|
|
"""
|
2024-03-22 13:39:01 +08:00
|
|
|
|
chars = "*[]()~_`>#+=|{}.!"
|
2024-03-22 07:44:41 +08:00
|
|
|
|
for char in chars:
|
|
|
|
|
text = text.replace(char, f"\\\\{char}")
|
|
|
|
|
return text
|