2023-03-04 13:53:18 +08:00
|
|
|
|
# 长消息处理相关
|
2023-03-04 21:14:10 +08:00
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import base64
|
|
|
|
|
|
2023-03-04 13:53:18 +08:00
|
|
|
|
import config
|
2023-03-04 21:14:10 +08:00
|
|
|
|
from mirai.models.message import MessageComponent, MessageChain, Image
|
2023-03-04 13:53:18 +08:00
|
|
|
|
from mirai.models.message import ForwardMessageNode
|
|
|
|
|
from mirai.models.base import MiraiBaseModel
|
2023-03-04 21:14:10 +08:00
|
|
|
|
from typing import List
|
2023-03-04 13:53:18 +08:00
|
|
|
|
import pkg.utils.context as context
|
2023-03-04 21:14:10 +08:00
|
|
|
|
import pkg.utils.text2img as text2img
|
2023-03-04 13:53:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ForwardMessageDiaplay(MiraiBaseModel):
|
|
|
|
|
title: str = "群聊的聊天记录"
|
|
|
|
|
brief: str = "[聊天记录]"
|
|
|
|
|
source: str = "聊天记录"
|
|
|
|
|
preview: List[str] = []
|
|
|
|
|
summary: str = "查看x条转发消息"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Forward(MessageComponent):
|
|
|
|
|
"""合并转发。"""
|
|
|
|
|
type: str = "Forward"
|
|
|
|
|
"""消息组件类型。"""
|
|
|
|
|
display: ForwardMessageDiaplay
|
|
|
|
|
"""显示信息"""
|
|
|
|
|
node_list: List[ForwardMessageNode]
|
|
|
|
|
"""转发消息节点列表。"""
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
self.node_list = args[0]
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return '[聊天记录]'
|
|
|
|
|
|
|
|
|
|
|
2023-03-04 21:14:10 +08:00
|
|
|
|
def text_to_image(text: str) -> MessageComponent:
|
|
|
|
|
"""将文本转换成图片"""
|
|
|
|
|
# 检查temp文件夹是否存在
|
|
|
|
|
if not os.path.exists('temp'):
|
|
|
|
|
os.mkdir('temp')
|
|
|
|
|
img_path = text2img.text_to_image(text_str=text, save_as='temp/{}.png'.format(int(time.time())))
|
|
|
|
|
|
2023-03-04 21:36:07 +08:00
|
|
|
|
compressed_path, size = text2img.compress_image(img_path, outfile="temp/{}_compressed.png".format(int(time.time())))
|
2023-03-04 21:14:10 +08:00
|
|
|
|
# 读取图片,转换成base64
|
2023-03-04 21:36:07 +08:00
|
|
|
|
with open(compressed_path, 'rb') as f:
|
2023-03-04 21:14:10 +08:00
|
|
|
|
img = f.read()
|
|
|
|
|
|
|
|
|
|
b64 = base64.b64encode(img)
|
|
|
|
|
|
|
|
|
|
# 删除图片
|
|
|
|
|
os.remove(img_path)
|
2023-03-04 23:53:22 +08:00
|
|
|
|
|
|
|
|
|
# 判断compressed_path是否存在
|
|
|
|
|
if os.path.exists(compressed_path):
|
|
|
|
|
os.remove(compressed_path)
|
2023-03-04 21:14:10 +08:00
|
|
|
|
# 返回图片
|
|
|
|
|
return Image(base64=b64.decode('utf-8'))
|
|
|
|
|
|
2023-03-04 13:53:18 +08:00
|
|
|
|
|
|
|
|
|
def check_text(text: str) -> list:
|
|
|
|
|
"""检查文本是否为长消息,并转换成该使用的消息链组件"""
|
|
|
|
|
if len(text) > config.blob_message_threshold:
|
2023-03-04 23:53:22 +08:00
|
|
|
|
|
|
|
|
|
# logging.info("长消息: {}".format(text))
|
2023-03-04 13:53:18 +08:00
|
|
|
|
if config.blob_message_strategy == 'image':
|
|
|
|
|
# 转换成图片
|
2023-03-04 21:14:10 +08:00
|
|
|
|
return [text_to_image(text)]
|
2023-03-04 13:53:18 +08:00
|
|
|
|
elif config.blob_message_strategy == 'forward':
|
|
|
|
|
|
|
|
|
|
# 包装转发消息
|
|
|
|
|
display = ForwardMessageDiaplay(
|
|
|
|
|
title='群聊的聊天记录',
|
|
|
|
|
brief='[聊天记录]',
|
|
|
|
|
source='聊天记录',
|
|
|
|
|
preview=["bot: "+text],
|
|
|
|
|
summary="查看1条转发消息"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
node = ForwardMessageNode(
|
|
|
|
|
sender_id=config.mirai_http_api_config['qq'],
|
|
|
|
|
sender_name='bot',
|
|
|
|
|
message_chain=MessageChain([text])
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
forward = Forward(
|
|
|
|
|
display=display,
|
|
|
|
|
node_list=[node]
|
|
|
|
|
)
|
|
|
|
|
|
2023-03-04 21:14:10 +08:00
|
|
|
|
return [forward]
|
|
|
|
|
else:
|
|
|
|
|
return [text]
|