增强的MessageSegment字段

This commit is contained in:
Wuyi无疑 2023-03-12 14:46:11 +08:00
parent 1a6cd1dea3
commit 44b312a492
3 changed files with 42 additions and 10 deletions

View File

@ -1,4 +1,5 @@
from io import BytesIO
from pathlib import Path
from base64 import b64encode
from typing import List, Union, Literal
@ -12,7 +13,7 @@ class MessageSegment:
return [self, other]
@staticmethod
def image(img: Union[str, Image.Image, bytes]) -> Message:
def image(img: Union[str, Image.Image, bytes, Path]) -> Message:
if isinstance(img, Image.Image):
img = img.convert('RGB')
result_buffer = BytesIO()
@ -20,6 +21,9 @@ class MessageSegment:
img = result_buffer.getvalue()
elif isinstance(img, bytes):
pass
elif isinstance(img, Path):
with open(str(img), 'rb') as fp:
img = fp.read()
else:
if img.startswith('base64://'):
return Message(type='image', data=img)
@ -37,13 +41,41 @@ class MessageSegment:
return Message(type='at', data=user)
@staticmethod
def node(content_list: List[Message]) -> Message:
return Message(type='node', data=content_list)
def node(
content_list: Union[List[Message], List[str], List[bytes]]
) -> Message:
msg_list: List[Message] = []
for msg in content_list:
if isinstance(msg, Message):
msg_list.append(msg)
elif isinstance(msg, bytes):
msg_list.append(MessageSegment.image(msg))
else:
if msg.startswith('base64://'):
msg_list.append(Message(type='image', data=msg))
else:
msg_list.append(MessageSegment.text(msg))
return Message(type='node', data=msg_list)
@staticmethod
def record(content: str) -> Message:
return Message(type='record', data=content)
@staticmethod
def file(content: Union[Path, str, bytes], file_name: str) -> Message:
if isinstance(content, Path):
with open(str(content), 'rb') as fp:
file = fp.read()
elif isinstance(content, bytes):
file = content
else:
with open(content, 'rb') as fp:
file = fp.read()
return Message(
type='file',
data=f'{file_name}|base64://{b64encode(file).decode()}',
)
@staticmethod
def log(
type: Literal['INFO', 'WARNING', 'ERROR', 'SUCCESS'], content: str

12
poetry.lock generated
View File

@ -902,14 +902,14 @@ socks = ["socksio (>=1.0.0,<2.0.0)"]
[[package]]
name = "identify"
version = "2.5.19"
version = "2.5.20"
description = "File identification library for Python"
category = "dev"
optional = false
python-versions = ">=3.7"
files = [
{file = "identify-2.5.19-py2.py3-none-any.whl", hash = "sha256:3ee3533e7f6f5023157fbebbd5687bb4b698ce6f305259e0d24b2d7d9efb72bc"},
{file = "identify-2.5.19.tar.gz", hash = "sha256:4102ecd051f6884449e7359e55b38ba6cd7aafb6ef27b8e2b38495a5723ea106"},
{file = "identify-2.5.20-py2.py3-none-any.whl", hash = "sha256:5dfef8a745ca4f2c95f27e9db74cb4c8b6d9916383988e8791f3595868f78a33"},
{file = "identify-2.5.20.tar.gz", hash = "sha256:c8b288552bc5f05a08aff09af2f58e6976bf8ac87beb38498a0e3d98ba64eb18"},
]
[package.extras]
@ -1320,14 +1320,14 @@ websockets = ["websockets (>=10.0,<11.0)"]
[[package]]
name = "openpyxl"
version = "3.1.1"
version = "3.1.2"
description = "A Python library to read/write Excel 2010 xlsx/xlsm files"
category = "main"
optional = false
python-versions = ">=3.6"
files = [
{file = "openpyxl-3.1.1-py2.py3-none-any.whl", hash = "sha256:a0266e033e65f33ee697254b66116a5793c15fc92daf64711080000df4cfe0a8"},
{file = "openpyxl-3.1.1.tar.gz", hash = "sha256:f06d44e2c973781068bce5ecf860a09bcdb1c7f5ce1facd5e9aa82c92c93ae72"},
{file = "openpyxl-3.1.2-py2.py3-none-any.whl", hash = "sha256:f91456ead12ab3c6c2e9491cf33ba6d08357d802192379bb482f1033ade496f5"},
{file = "openpyxl-3.1.2.tar.gz", hash = "sha256:a6f5977418eff3b2d5500d54d9db50c8277a368436f4e4f8ddb1be3422870184"},
]
[package.dependencies]

View File

@ -33,7 +33,7 @@ msgspec==0.13.1 ; python_full_version >= "3.8.1" and python_full_version < "4.0.
multidict==6.0.4 ; python_full_version >= "3.8.1" and python_version < "4.0"
nonebot-plugin-apscheduler==0.2.0 ; python_full_version >= "3.8.1" and python_version < "4.0"
nonebot2==2.0.0rc3 ; python_full_version >= "3.8.1" and python_version < "4.0"
openpyxl==3.1.1 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
openpyxl==3.1.2 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
passlib==1.7.4 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
pillow==9.4.0 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"
pydantic==1.10.6 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0"