1
0
forked from bot/app

feat: 国产某聊天软件的图床支持

This commit is contained in:
远野千束 2024-04-03 14:10:10 +08:00
parent 955d9f6d62
commit 86e6397fa7
3 changed files with 54 additions and 43 deletions

View File

@ -17,7 +17,7 @@ category: 使用手册
[S]liteyuki # 查看轻雪信息 [S]liteyuki # 查看轻雪信息
[S]config set <key> value # 添加配置项,若存在则会覆盖,输入值会被执行,以便于转换为正确的值,"10"和10是不一样的 [S]config set <key> value # 添加配置项,若存在则会覆盖,输入值会被执行,以便于转换为正确的值,"10"和10是不一样的
[S]config get [key] # 查询配置项不带key返回配置项列表推荐私聊使用 [S]config get [key] # 查询配置项不带key返回配置项列表推荐私聊使用
[S]switch-image-mode # 切换图片模式该功能需要commit:44477b9及以后的Lagrange.OneBot版本在普通图片和Markdown图片之间切换后者更大但有失败的可能 [S]switch-image-mode # 切换图片模式该功能需要commit:505468b及以后的Lagrange.OneBot版本在普通图片和Markdown图片之间切换后者更大但有失败的可能
# 上述两个命令修改的配置项在数据库中保存,但是优先级低于配置文件,如果配置文件中存在相同的配置项,将会使用配置文件中的配置 # 上述两个命令修改的配置项在数据库中保存,但是优先级低于配置文件,如果配置文件中存在相同的配置项,将会使用配置文件中的配置
------ ------
别名: reload-liteyuki 重启轻雪, update-liteyuki 更新轻雪, config 配置, set 设置, get 查询 别名: reload-liteyuki 重启轻雪, update-liteyuki 更新轻雪, config 配置, set 设置, get 查询

View File

@ -66,6 +66,8 @@ async def _(result: Arparma, event: T_MessageEvent, bot: T_Bot):
keywords: list[str] = result.subcommands["search"].args.get("keywords") keywords: list[str] = result.subcommands["search"].args.get("keywords")
rs = await npm_search(keywords) rs = await npm_search(keywords)
max_show = 10 max_show = 10
for p in rs:
print(p.module_name, p.homepage)
if len(rs): if len(rs):
reply = f"{ulang.get('npm.search_result')} | {ulang.get('npm.total', TOTAL=len(rs))}\n***" reply = f"{ulang.get('npm.search_result')} | {ulang.get('npm.total', TOTAL=len(rs))}\n***"
for plugin in rs[:min(max_show, len(rs))]: for plugin in rs[:min(max_show, len(rs))]:

View File

@ -1,4 +1,5 @@
import asyncio import asyncio
import base64
import io import io
from urllib.parse import quote from urllib.parse import quote
@ -19,7 +20,8 @@ from nonebot_plugin_htmlrender import md_to_pic
config = load_from_yaml("config.yml") config = load_from_yaml("config.yml")
can_send_markdown={} # 用于存储机器人是否支持发送markdown消息id->bool can_send_markdown = {} # 用于存储机器人是否支持发送markdown消息id->bool
class Markdown: class Markdown:
@staticmethod @staticmethod
@ -55,21 +57,21 @@ class Markdown:
forward_id = await bot.call_api( forward_id = await bot.call_api(
api="send_forward_msg", api="send_forward_msg",
messages=[ messages=[
v11.MessageSegment( v11.MessageSegment(
type="node", type="node",
data={ data={
"name": "Liteyuki.OneBot", "name" : "Liteyuki.OneBot",
"uin": bot.self_id, "uin" : bot.self_id,
"content": [ "content": [
{ {
"type": "markdown", "type": "markdown",
"data": { "data": {
"content": '{"content":"%s"}' % formatted_md "content": '{"content":"%s"}' % formatted_md
} }
}, },
] ]
}, },
) )
] ]
) )
# 发送Markdown longmsg并获取相应数据 # 发送Markdown longmsg并获取相应数据
@ -78,12 +80,12 @@ class Markdown:
group_id=session_id, group_id=session_id,
message_type=message_type, message_type=message_type,
message=[ message=[
v11.MessageSegment( v11.MessageSegment(
type="longmsg", type="longmsg",
data={ data={
"id": forward_id "id": forward_id
} }
), ),
], ],
**kwargs **kwargs
) )
@ -132,29 +134,36 @@ class Markdown:
if isinstance(image, str): if isinstance(image, str):
async with aiofiles.open(image, "rb") as f: async with aiofiles.open(image, "rb") as f:
image = await f.read() image = await f.read()
method = 2
# 1.轻雪图床方案 # 1.轻雪图床方案
image_url = await liteyuki_api.upload_image(image) # if method == 1:
image_size = Image.open(io.BytesIO(image)).size # image_url = await liteyuki_api.upload_image(image)
image_md = Markdown.image(image_url, image_size) # image_size = Image.open(io.BytesIO(image)).size
data = await Markdown.send_md(image_md, bot, message_type=message_type, session_id=session_id, event=event, # image_md = Markdown.image(image_url, image_size)
retry_as_image=False, # data = await Markdown.send_md(image_md, bot, message_type=message_type, session_id=session_id, event=event,
**kwargs) # 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}")
await Markdown.send_md(Markdown.image(data, Image.open(io.BytesIO(image)).size), bot, event=event, message_type=message_type,
session_id=session_id, **kwargs)
# 其他实现端方案
else:
image_message_id = (await bot.send_private_msg(
user_id=bot.self_id,
message=[
v11.MessageSegment.image(file=image)
]
))["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
image_md = Markdown.image(image_url, image_size)
return await Markdown.send_md(image_md, bot, message_type=message_type, session_id=session_id, event=event, **kwargs)
# 2.此方案等林文轩修好后再用QQ图床再嵌入markdown发送
# image_message_id = (await bot.send_private_msg(
# user_id=bot.self_id,
# message=[
# v11.MessageSegment.image(file=image)
# ]
# ))["message_id"]
# await asyncio.sleep(3)
# await bot.delete_msg(message_id=image_message_id)
# image_message = await bot.get_msg(message_id=image_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
# image_md = Markdown.image(image_url, image_size)
# return await Markdown.send_md(image_md, bot, message_type=message_type, session_id=session_id, event=event, **kwargs)
if data is None: if data is None:
data = await bot.send_msg( data = await bot.send_msg(
message_type=message_type, message_type=message_type,