mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-29 03:30:28 +08:00
✨ MessageSegment.image
支持传入链接, 无按钮的消息将不转为Md格式发送
This commit is contained in:
parent
61df4c2810
commit
7a9215042e
@ -43,7 +43,7 @@ class _Bot:
|
|||||||
_message = await convert_message(message, bot_id)
|
_message = await convert_message(message, bot_id)
|
||||||
|
|
||||||
if bot_id in enable_markdown_platform:
|
if bot_id in enable_markdown_platform:
|
||||||
_message = await to_markdown(_message)
|
_message = await to_markdown(_message, None, bot_id)
|
||||||
|
|
||||||
if at_sender and sender_id:
|
if at_sender and sender_id:
|
||||||
_message.append(MessageSegment.at(sender_id))
|
_message.append(MessageSegment.at(sender_id))
|
||||||
@ -191,7 +191,9 @@ class Bot:
|
|||||||
else:
|
else:
|
||||||
_buttons.append(Button(option, option, option))
|
_buttons.append(Button(option, option, option))
|
||||||
|
|
||||||
await self.send(await to_markdown(_reply, _buttons))
|
await self.send(
|
||||||
|
await to_markdown(_reply, _buttons, self.bot_id)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
if unsuported_platform:
|
if unsuported_platform:
|
||||||
_options: List[str] = []
|
_options: List[str] = []
|
||||||
|
@ -12,6 +12,7 @@ from gsuid_core.models import Message
|
|||||||
from gsuid_core.data_store import image_res
|
from gsuid_core.data_store import image_res
|
||||||
from gsuid_core.message_models import Button
|
from gsuid_core.message_models import Button
|
||||||
from gsuid_core.utils.image.convert import text2pic
|
from gsuid_core.utils.image.convert import text2pic
|
||||||
|
from gsuid_core.utils.image.image_tools import sget
|
||||||
from gsuid_core.utils.plugins_config.gs_config import (
|
from gsuid_core.utils.plugins_config.gs_config import (
|
||||||
send_pic_config,
|
send_pic_config,
|
||||||
pic_upload_config,
|
pic_upload_config,
|
||||||
@ -23,6 +24,7 @@ R_text = core_plugins_config.get_config('RandomText').data
|
|||||||
is_text2pic = core_plugins_config.get_config('AutoTextToPic').data
|
is_text2pic = core_plugins_config.get_config('AutoTextToPic').data
|
||||||
text2pic_limit = core_plugins_config.get_config('TextToPicThreshold').data
|
text2pic_limit = core_plugins_config.get_config('TextToPicThreshold').data
|
||||||
enable_pic_srv = core_plugins_config.get_config('EnablePicSrv').data
|
enable_pic_srv = core_plugins_config.get_config('EnablePicSrv').data
|
||||||
|
force_send_md = core_plugins_config.get_config('ForceSendMD').data
|
||||||
pic_srv = core_plugins_config.get_config('PicSrv').data
|
pic_srv = core_plugins_config.get_config('PicSrv').data
|
||||||
SERVER = pic_upload_config.get_config('PicUploadServer').data
|
SERVER = pic_upload_config.get_config('PicUploadServer').data
|
||||||
IS_UPLOAD = pic_upload_config.get_config('PicUpload').data
|
IS_UPLOAD = pic_upload_config.get_config('PicUpload').data
|
||||||
@ -169,7 +171,7 @@ class MessageSegment:
|
|||||||
return Message(type=f'log_{type}', data=content)
|
return Message(type=f'log_{type}', data=content)
|
||||||
|
|
||||||
|
|
||||||
async def _conver_image_to_url(image: Union[bytes, str]) -> List[Message]:
|
async def _image_to_remote_url(image: Union[bytes, str]) -> List[Message]:
|
||||||
if pclient is not None:
|
if pclient is not None:
|
||||||
if isinstance(image, str) and image.startswith('base64://'):
|
if isinstance(image, str) and image.startswith('base64://'):
|
||||||
image_bytes = b64decode(image[9:])
|
image_bytes = b64decode(image[9:])
|
||||||
@ -189,13 +191,51 @@ async def _conver_image_to_url(image: Union[bytes, str]) -> List[Message]:
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
async def _image_to_local_url(image: Union[bytes, str]) -> List[Message]:
|
||||||
|
if isinstance(image, str):
|
||||||
|
if image.startswith('base64://'):
|
||||||
|
image_bytes = b64decode(image[9:])
|
||||||
|
else:
|
||||||
|
return [Message(type='image', data=image.replace('link://', ''))]
|
||||||
|
elif isinstance(image, (memoryview, bytearray)):
|
||||||
|
image_bytes = bytes(image)
|
||||||
|
else:
|
||||||
|
image_bytes = image
|
||||||
|
|
||||||
|
bio = BytesIO(image_bytes)
|
||||||
|
_image = Image.open(bio)
|
||||||
|
name = f'{uuid.uuid1()}.jpg'
|
||||||
|
path = image_res / name
|
||||||
|
path.write_bytes(image_bytes)
|
||||||
|
data = f'link://{pic_srv}/genshinuid/image/{name}'
|
||||||
|
return [
|
||||||
|
Message(type='image', data=data),
|
||||||
|
MessageSegment.image_size(_image.size),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def _image_to_url(
|
||||||
|
image: Union[str, bytes], send_type: str, message: Message
|
||||||
|
):
|
||||||
|
if send_type == 'link_remote':
|
||||||
|
return await _image_to_remote_url(image)
|
||||||
|
elif (send_type == 'link_local') or enable_pic_srv:
|
||||||
|
return await _image_to_local_url(image)
|
||||||
|
elif pclient is not None:
|
||||||
|
return await _image_to_remote_url(image)
|
||||||
|
else:
|
||||||
|
return [message]
|
||||||
|
|
||||||
|
|
||||||
async def _convert_message_to_image(
|
async def _convert_message_to_image(
|
||||||
message: Message, bot_id: str
|
message: Message, bot_id: str
|
||||||
) -> List[Message]:
|
) -> List[Message]:
|
||||||
if message.data is None:
|
if message.data is None:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
send_type = send_pic_config.get_config(bot_id).data
|
||||||
image_b64 = None
|
image_b64 = None
|
||||||
|
|
||||||
if (
|
if (
|
||||||
message.type == 'text'
|
message.type == 'text'
|
||||||
and is_text2pic
|
and is_text2pic
|
||||||
@ -210,7 +250,12 @@ async def _convert_message_to_image(
|
|||||||
image_b64 = img
|
image_b64 = img
|
||||||
image_bytes = b64decode(img[9:])
|
image_bytes = b64decode(img[9:])
|
||||||
elif isinstance(img, str) and img.startswith('link://'):
|
elif isinstance(img, str) and img.startswith('link://'):
|
||||||
return [Message(type='image', data=img)]
|
if send_type == 'base64':
|
||||||
|
resp = await sget(img.replace('link://', ''))
|
||||||
|
image_b64 = b64encode(resp.content).decode('utf-8')
|
||||||
|
return [Message(type='image', data=image_b64)]
|
||||||
|
else:
|
||||||
|
return [Message(type='image', data=img)]
|
||||||
else:
|
else:
|
||||||
image_bytes = img
|
image_bytes = img
|
||||||
else:
|
else:
|
||||||
@ -218,8 +263,6 @@ async def _convert_message_to_image(
|
|||||||
|
|
||||||
assert isinstance(image_bytes, bytes)
|
assert isinstance(image_bytes, bytes)
|
||||||
|
|
||||||
send_type = send_pic_config.get_config(bot_id).data
|
|
||||||
|
|
||||||
if send_type == 'base64':
|
if send_type == 'base64':
|
||||||
return (
|
return (
|
||||||
[Message(type='image', data=image_b64)]
|
[Message(type='image', data=image_b64)]
|
||||||
@ -227,23 +270,7 @@ async def _convert_message_to_image(
|
|||||||
else [MessageSegment.image(image_bytes)]
|
else [MessageSegment.image(image_bytes)]
|
||||||
)
|
)
|
||||||
|
|
||||||
if send_type == 'link_remote':
|
return await _image_to_url(image_bytes, send_type, message)
|
||||||
return await _conver_image_to_url(image_bytes)
|
|
||||||
elif (send_type == 'link_local') or enable_pic_srv:
|
|
||||||
bio = BytesIO(image_bytes)
|
|
||||||
image = Image.open(bio)
|
|
||||||
name = f'{uuid.uuid1()}.jpg'
|
|
||||||
path = image_res / name
|
|
||||||
path.write_bytes(image_bytes)
|
|
||||||
data = f'link://{pic_srv}/genshinuid/image/{name}'
|
|
||||||
return [
|
|
||||||
Message(type='image', data=data),
|
|
||||||
MessageSegment.image_size(image.size),
|
|
||||||
]
|
|
||||||
elif pclient is not None:
|
|
||||||
return await _conver_image_to_url(image_bytes)
|
|
||||||
else:
|
|
||||||
return [message]
|
|
||||||
|
|
||||||
|
|
||||||
async def _convert_message(
|
async def _convert_message(
|
||||||
@ -309,14 +336,24 @@ async def convert_message(
|
|||||||
async def to_markdown(
|
async def to_markdown(
|
||||||
message: List[Message],
|
message: List[Message],
|
||||||
buttons: Optional[Union[List[Button], List[List[Button]]]] = None,
|
buttons: Optional[Union[List[Button], List[List[Button]]]] = None,
|
||||||
|
bot_id: str = 'onebot',
|
||||||
) -> List[Message]:
|
) -> List[Message]:
|
||||||
_markdown_list = []
|
_markdown_list = []
|
||||||
_message = []
|
_message = []
|
||||||
url = None
|
url = None
|
||||||
size = None
|
size = None
|
||||||
|
send_type = send_pic_config.get_config(bot_id).data
|
||||||
|
|
||||||
|
if buttons is None and not force_send_md:
|
||||||
|
return message
|
||||||
|
|
||||||
for m in message:
|
for m in message:
|
||||||
if m.type == 'image':
|
if m.type == 'image':
|
||||||
url = m.data
|
if isinstance(m.data, str):
|
||||||
|
if m.data.startswith('link://'):
|
||||||
|
url = m.data.replace('link://', '')
|
||||||
|
elif m.data.startswith('base64://'):
|
||||||
|
url = await _image_to_url(m.data, send_type, m)
|
||||||
elif m.type == 'image_size':
|
elif m.type == 'image_size':
|
||||||
size = m.data
|
size = m.data
|
||||||
elif m.type == 'text':
|
elif m.type == 'text':
|
||||||
|
@ -66,4 +66,5 @@ CONIFG_DEFAULT: Dict[str, GSC] = {
|
|||||||
'SendButtonsPlatform': GsListStrConfig(
|
'SendButtonsPlatform': GsListStrConfig(
|
||||||
'默认发送按钮的平台列表(用:连接)', '发送按钮的平台列表', []
|
'默认发送按钮的平台列表(用:连接)', '发送按钮的平台列表', []
|
||||||
),
|
),
|
||||||
|
'ForceSendMD': GsBoolConfig('强制使用MD发送图文', '强制使用MD发送图文', False),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user