2024-04-03 14:10:10 +08:00
|
|
|
|
import base64
|
2024-03-31 06:22:53 +08:00
|
|
|
|
import io
|
2024-03-26 17:14:41 +08:00
|
|
|
|
from urllib.parse import quote
|
|
|
|
|
|
2024-03-31 06:22:53 +08:00
|
|
|
|
import aiofiles
|
|
|
|
|
from PIL import Image
|
|
|
|
|
import aiohttp
|
2024-03-19 21:56:31 +08:00
|
|
|
|
import nonebot
|
2024-04-02 20:32:28 +08:00
|
|
|
|
from nonebot import require
|
2024-05-16 20:09:20 +08:00
|
|
|
|
from nonebot.adapters import satori
|
2024-04-14 21:39:27 +08:00
|
|
|
|
from nonebot.adapters.onebot import v11
|
|
|
|
|
from typing import Any, Type
|
2024-03-31 06:22:53 +08:00
|
|
|
|
|
2024-04-14 21:39:27 +08:00
|
|
|
|
from nonebot.internal.adapter import MessageSegment
|
|
|
|
|
from nonebot.internal.adapter.message import TM
|
|
|
|
|
|
|
|
|
|
from .. import load_from_yaml
|
|
|
|
|
from ..base.ly_typing import T_Bot, T_Message, T_MessageEvent
|
2024-03-19 21:56:31 +08:00
|
|
|
|
|
2024-04-02 20:32:28 +08:00
|
|
|
|
require("nonebot_plugin_htmlrender")
|
|
|
|
|
from nonebot_plugin_htmlrender import md_to_pic
|
|
|
|
|
|
2024-03-31 08:20:20 +08:00
|
|
|
|
config = load_from_yaml("config.yml")
|
|
|
|
|
|
2024-04-03 14:10:10 +08:00
|
|
|
|
can_send_markdown = {} # 用于存储机器人是否支持发送markdown消息,id->bool
|
2024-04-24 20:09:23 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-24 15:20:32 +08:00
|
|
|
|
class TencentBannedMarkdownError(BaseException):
|
|
|
|
|
pass
|
2024-03-19 21:56:31 +08:00
|
|
|
|
|
2024-04-24 20:09:23 +08:00
|
|
|
|
|
2024-04-12 01:15:05 +08:00
|
|
|
|
async def broadcast_to_superusers(message: str | T_Message, markdown: bool = False):
|
|
|
|
|
"""广播消息给超级用户"""
|
|
|
|
|
for bot in nonebot.get_bots().values():
|
|
|
|
|
for user_id in config.get("superusers", []):
|
|
|
|
|
if markdown:
|
2024-04-14 21:39:27 +08:00
|
|
|
|
await MarkdownMessage.send_md(message, bot, message_type="private", session_id=user_id)
|
2024-04-12 01:15:05 +08:00
|
|
|
|
else:
|
|
|
|
|
await bot.send_private_msg(user_id=user_id, message=message)
|
|
|
|
|
|
|
|
|
|
|
2024-04-14 21:39:27 +08:00
|
|
|
|
class MarkdownMessage:
|
2024-03-31 06:22:53 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
async def send_md(
|
|
|
|
|
markdown: str,
|
|
|
|
|
bot: T_Bot, *,
|
|
|
|
|
message_type: str = None,
|
|
|
|
|
session_id: str | int = None,
|
|
|
|
|
event: T_MessageEvent = None,
|
2024-04-02 20:32:28 +08:00
|
|
|
|
retry_as_image: bool = True,
|
2024-03-22 12:41:38 +08:00
|
|
|
|
**kwargs
|
2024-04-02 20:32:28 +08:00
|
|
|
|
) -> dict[str, Any] | None:
|
|
|
|
|
"""
|
|
|
|
|
发送Markdown消息,支持自动转为图片发送
|
|
|
|
|
Args:
|
|
|
|
|
markdown:
|
|
|
|
|
bot:
|
|
|
|
|
message_type:
|
|
|
|
|
session_id:
|
|
|
|
|
event:
|
|
|
|
|
retry_as_image: 发送失败后是否尝试以图片形式发送,否则失败返回None
|
|
|
|
|
**kwargs:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
2024-03-31 06:22:53 +08:00
|
|
|
|
formatted_md = v11.unescape(markdown).replace("\n", r"\n").replace('"', r'\\\"')
|
|
|
|
|
if event is not None and message_type is None:
|
2024-05-16 20:09:20 +08:00
|
|
|
|
if isinstance(event, satori.event.Event):
|
|
|
|
|
message_type = "private" if event.guild is None else "group"
|
|
|
|
|
group_id = event.guild.id if event.guild is not None else None
|
|
|
|
|
else:
|
|
|
|
|
assert event is not None
|
|
|
|
|
message_type = event.message_type
|
|
|
|
|
group_id = event.group_id if message_type == "group" else None
|
|
|
|
|
user_id = event.user.id if isinstance(event, satori.event.Event) else event.user_id
|
|
|
|
|
session_id = user_id if message_type == "private" else group_id
|
|
|
|
|
else:
|
|
|
|
|
pass
|
2024-03-31 06:22:53 +08:00
|
|
|
|
try:
|
2024-04-24 20:09:23 +08:00
|
|
|
|
raise TencentBannedMarkdownError("Tencent banned markdown")
|
|
|
|
|
forward_id = await bot.call_api(
|
|
|
|
|
"send_private_forward_msg",
|
|
|
|
|
messages=[
|
2024-05-16 20:09:20 +08:00
|
|
|
|
{
|
|
|
|
|
"type": "node",
|
|
|
|
|
"data": {
|
|
|
|
|
"content": [
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"content": "{\"content\":\"%s\"}" % formatted_md,
|
|
|
|
|
},
|
|
|
|
|
"type": "markdown"
|
2024-04-24 20:09:23 +08:00
|
|
|
|
}
|
2024-05-16 20:09:20 +08:00
|
|
|
|
],
|
|
|
|
|
"name": "[]",
|
|
|
|
|
"uin": bot.self_id
|
2024-04-24 20:09:23 +08:00
|
|
|
|
}
|
2024-05-16 20:09:20 +08:00
|
|
|
|
}
|
2024-04-24 20:09:23 +08:00
|
|
|
|
],
|
|
|
|
|
user_id=bot.self_id
|
|
|
|
|
|
|
|
|
|
)
|
2024-03-20 00:44:36 +08:00
|
|
|
|
data = await bot.send_msg(
|
2024-03-31 06:22:53 +08:00
|
|
|
|
user_id=session_id,
|
|
|
|
|
group_id=session_id,
|
2024-03-20 00:44:36 +08:00
|
|
|
|
message_type=message_type,
|
2024-03-31 06:22:53 +08:00
|
|
|
|
message=[
|
2024-05-16 20:09:20 +08:00
|
|
|
|
{
|
|
|
|
|
"type": "longmsg",
|
|
|
|
|
"data": {
|
|
|
|
|
"id": forward_id
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-03-31 06:22:53 +08:00
|
|
|
|
],
|
2024-03-22 12:41:38 +08:00
|
|
|
|
**kwargs
|
2024-03-20 00:44:36 +08:00
|
|
|
|
)
|
2024-04-02 20:32:28 +08:00
|
|
|
|
except BaseException as e:
|
|
|
|
|
nonebot.logger.error(f"send markdown error, retry as image: {e}")
|
|
|
|
|
# 发送失败,渲染为图片发送
|
2024-04-24 15:20:32 +08:00
|
|
|
|
# if not retry_as_image:
|
|
|
|
|
# return None
|
2024-04-02 20:32:28 +08:00
|
|
|
|
|
2024-04-24 15:20:32 +08:00
|
|
|
|
plain_markdown = markdown.replace("[🔗", "[")
|
2024-04-02 20:32:28 +08:00
|
|
|
|
md_image_bytes = await md_to_pic(
|
|
|
|
|
md=plain_markdown,
|
|
|
|
|
width=540,
|
|
|
|
|
device_scale_factor=4
|
|
|
|
|
)
|
2024-05-16 20:09:20 +08:00
|
|
|
|
if isinstance(bot, satori.Bot):
|
|
|
|
|
msg_seg = satori.MessageSegment.image(raw=md_image_bytes,mime="image/png")
|
|
|
|
|
data = await bot.send(
|
|
|
|
|
event=event,
|
|
|
|
|
message=msg_seg
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
data = await bot.send_msg(
|
|
|
|
|
message_type=message_type,
|
|
|
|
|
group_id=session_id,
|
|
|
|
|
user_id=session_id,
|
|
|
|
|
message=v11.MessageSegment.image(md_image_bytes),
|
|
|
|
|
)
|
2024-03-31 06:22:53 +08:00
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def send_image(
|
|
|
|
|
image: bytes | str,
|
|
|
|
|
bot: T_Bot, *,
|
|
|
|
|
message_type: str = None,
|
|
|
|
|
session_id: str | int = None,
|
|
|
|
|
event: T_MessageEvent = None,
|
|
|
|
|
**kwargs
|
|
|
|
|
) -> dict:
|
|
|
|
|
"""
|
|
|
|
|
发送单张装逼大图
|
|
|
|
|
Args:
|
|
|
|
|
image: 图片字节流或图片本地路径,链接请使用Markdown.image_async方法获取后通过send_md发送
|
|
|
|
|
bot: bot instance
|
|
|
|
|
message_type: message type
|
|
|
|
|
session_id: session id
|
|
|
|
|
event: event
|
|
|
|
|
kwargs: other arguments
|
|
|
|
|
Returns:
|
|
|
|
|
dict: response data
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
2024-03-31 06:22:53 +08:00
|
|
|
|
"""
|
|
|
|
|
if isinstance(image, str):
|
|
|
|
|
async with aiofiles.open(image, "rb") as f:
|
|
|
|
|
image = await f.read()
|
2024-04-03 14:10:10 +08:00
|
|
|
|
method = 2
|
2024-04-01 23:56:03 +08:00
|
|
|
|
# 1.轻雪图床方案
|
2024-04-03 14:10:10 +08:00
|
|
|
|
# if method == 1:
|
|
|
|
|
# image_url = await liteyuki_api.upload_image(image)
|
|
|
|
|
# image_size = Image.open(io.BytesIO(image)).size
|
|
|
|
|
# image_md = Markdown.image(image_url, image_size)
|
|
|
|
|
# data = await Markdown.send_md(image_md, bot, message_type=message_type, session_id=session_id, event=event,
|
|
|
|
|
# retry_as_image=False,
|
|
|
|
|
# **kwargs)
|
|
|
|
|
|
|
|
|
|
# Lagrange.OneBot方案
|
|
|
|
|
if method == 2:
|
|
|
|
|
base64_string = base64.b64encode(image).decode("utf-8")
|
|
|
|
|
data = await bot.call_api("upload_image", file=f"base64://{base64_string}")
|
2024-05-16 20:09:20 +08:00
|
|
|
|
await MarkdownMessage.send_md(MarkdownMessage.image(data, Image.open(io.BytesIO(image)).size), bot,
|
|
|
|
|
event=event, message_type=message_type,
|
2024-04-14 21:39:27 +08:00
|
|
|
|
session_id=session_id, **kwargs)
|
2024-04-03 14:10:10 +08:00
|
|
|
|
|
|
|
|
|
# 其他实现端方案
|
|
|
|
|
else:
|
|
|
|
|
image_message_id = (await bot.send_private_msg(
|
|
|
|
|
user_id=bot.self_id,
|
|
|
|
|
message=[
|
2024-05-16 20:09:20 +08:00
|
|
|
|
v11.MessageSegment.image(file=image)
|
2024-04-03 14:10:10 +08:00
|
|
|
|
]
|
|
|
|
|
))["message_id"]
|
|
|
|
|
image_url = (await bot.get_msg(message_id=image_message_id))["message"][0]["data"]["url"]
|
|
|
|
|
image_size = Image.open(io.BytesIO(image)).size
|
2024-04-14 21:39:27 +08:00
|
|
|
|
image_md = MarkdownMessage.image(image_url, image_size)
|
2024-05-16 20:09:20 +08:00
|
|
|
|
return await MarkdownMessage.send_md(image_md, bot, message_type=message_type, session_id=session_id,
|
|
|
|
|
event=event, **kwargs)
|
2024-04-03 14:10:10 +08:00
|
|
|
|
|
2024-04-02 20:32:28 +08:00
|
|
|
|
if data is None:
|
|
|
|
|
data = await bot.send_msg(
|
|
|
|
|
message_type=message_type,
|
|
|
|
|
group_id=session_id,
|
|
|
|
|
user_id=session_id,
|
|
|
|
|
message=v11.MessageSegment.image(image),
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
return data
|
2024-04-01 23:56:03 +08:00
|
|
|
|
|
2024-03-31 06:22:53 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
async def get_image_url(image: bytes | str, bot: T_Bot) -> str:
|
|
|
|
|
"""把图片上传到图床,返回链接
|
|
|
|
|
Args:
|
|
|
|
|
bot: 发送的bot
|
|
|
|
|
image: 图片字节流或图片本地路径
|
|
|
|
|
Returns:
|
|
|
|
|
"""
|
|
|
|
|
# 等林文轩修好Lagrange.OneBot再说
|
2024-03-20 22:30:52 +08:00
|
|
|
|
|
2024-03-21 12:10:24 +08:00
|
|
|
|
@staticmethod
|
2024-04-07 00:35:53 +08:00
|
|
|
|
def btn_cmd(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-31 09:03:28 +08:00
|
|
|
|
if "" not in config.get("command_start", ["/"]) and config.get("alconna_use_command_start", False):
|
2024-03-31 08:20:20 +08:00
|
|
|
|
cmd = f"{config['command_start'][0]}{cmd}"
|
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
|
2024-04-07 00:35:53 +08:00
|
|
|
|
def btn_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
|
|
|
|
|
2024-03-31 06:22:53 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
def image(url: str, size: tuple[int, int]) -> str:
|
2024-04-01 12:29:04 +08:00
|
|
|
|
"""构建图片链接
|
2024-03-31 06:22:53 +08:00
|
|
|
|
Args:
|
|
|
|
|
size:
|
|
|
|
|
url: 图片链接
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
markdown格式的图片
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
return f"![image #{size[0]}px #{size[1]}px]({url})"
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def image_async(url: str) -> str:
|
2024-04-01 12:29:04 +08:00
|
|
|
|
"""获取图片,自动请求获取大小,异步
|
2024-03-31 06:22:53 +08:00
|
|
|
|
Args:
|
|
|
|
|
url: 图片链接
|
|
|
|
|
|
|
|
|
|
Returns:
|
2024-04-01 12:29:04 +08:00
|
|
|
|
图片Markdown语法: ![image #{width}px #{height}px](link)
|
2024-03-31 06:22:53 +08:00
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
async with session.get(url) as resp:
|
|
|
|
|
image = Image.open(io.BytesIO(await resp.read()))
|
2024-04-14 21:39:27 +08:00
|
|
|
|
return MarkdownMessage.image(url, image.size)
|
2024-03-31 06:22:53 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
nonebot.logger.error(f"get image error: {e}")
|
|
|
|
|
return "[Image Error]"
|
|
|
|
|
|
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
|