diff --git a/GenshinUID/__init__.py b/GenshinUID/__init__.py index 4cd08347..745a422e 100644 --- a/GenshinUID/__init__.py +++ b/GenshinUID/__init__.py @@ -135,6 +135,26 @@ async def send_char_adv(bot: Bot, ev: Event): at_list = [Message('at', i) for i in _at_list] at_list.pop(0) message.extend(at_list) + # OneBot V12 (仅在 ComWechatClient 测试) + if bot.adapter.get_name() == 'OneBot V12': + # v12msgid = raw_data['id'] # V12的消息id + # time = raw_data['time'] # 返回格式 2023-04-01 16:38:51+00:00 + + messages = raw_data['original_message'] + # self = raw_data['self'] # 返回 platform='xxx' user_id='wxid_xxxxx' + # platform = self.platform # 机器人平台 + self_id = bot.self_id # 机器人账号ID + msg_id = raw_data['message_id'] # 消息ID + sp_bot_id = 'onebot_v12' + + if 'group_id' in raw_data: + group_id = raw_data['group_id'] + user_id = raw_data['user_id'] + sp_user_type = 'group' + else: + user_id = raw_data['user_id'] + sp_user_type = 'direct' + # V12还支持频道等其他平台,速速Pr! if sp_bot_id: bot_id = sp_bot_id @@ -206,15 +226,26 @@ def convert_message(_msg: Any, message: List[Message]): message.append( Message( 'text', - _msg.data['text'].replace('/', '') + _msg.data['text'] if 'text' in _msg.data - else _msg.data['content'].replace('/', ''), + else _msg.data['content'], ) ) elif _msg.type == 'image': - message.append(Message('image', _msg.data['url'])) + file_id = _msg.data.get('file_id') + if file_id in _msg.data.values(): + message.append(Message('image', _msg.data['file_id'])) + logger.debug(_msg.data["file_id"]) + else: + message.append(Message('image', _msg.data['url'])) elif _msg.type == 'at': message.append(Message('at', _msg.data['qq'])) elif _msg.type == 'reply': - message.append(Message('reply', _msg.data['id'])) + message_id = _msg.data.get('message_id') + if message_id in _msg.data.values(): + message.append(Message('reply', _msg.data['message_id'])) + else: + message.append(Message('reply', _msg.data['id'])) + elif _msg.type == 'mention': + message.append(Message('at', _msg.data['user_id'])) return message diff --git a/GenshinUID/client.py b/GenshinUID/client.py index 0b4263f4..97af51a4 100644 --- a/GenshinUID/client.py +++ b/GenshinUID/client.py @@ -1,5 +1,6 @@ import os import json +import time import base64 import asyncio from pathlib import Path @@ -113,8 +114,9 @@ class GsClient: pass # 根据bot_id字段发送消息 - # OneBot v11 & v12 + for bot in bot_list: + # OneBot v11 if msg.bot_id == 'onebot': await onebot_send( bot, @@ -126,6 +128,18 @@ class GsClient: msg.target_id, msg.target_type, ) + # OneBot v12 + elif msg.bot_id == 'onebot_v12': + await onebot_v12_send( + bot, + content, + image, + node, + file, + at_list, + msg.target_id, + msg.target_type, + ) # ntchat elif msg.bot_id == 'ntchat': await ntchat_send( @@ -425,6 +439,7 @@ async def kaiheila_send( doc = f.read() url = await bot.upload_file(doc, file_name) # type:ignore result['content'] = url + del_file(path) else: result['content'] = content @@ -468,6 +483,7 @@ async def telegram_send( with open(path, 'rb') as f: doc = f.read() result['document'] = doc + del_file(path) if content: await bot.call_api('send_message', chat_id=target_id, **result) @@ -566,3 +582,74 @@ async def feishu_send( await _send(_msg['data'], None) else: await _send(content, image) + + +async def onebot_v12_send( + bot: Bot, + content: Optional[str], + image: Optional[str], + node: Optional[List[Dict]], + file: Optional[str], + at_list: Optional[List[str]], + target_id: Optional[str], + target_type: Optional[str], +): + async def _send(content: Optional[str], image: Optional[str]): + async def send_file_message(params, file_type, file_id): + params["message"] = [ + {"type": file_type, "data": {"file_id": file_id}} + ] + await bot.call_api('send_message', **params) + + if not any([content, image, file]): + return + + params = {} + if target_type == "group": + params["detail_type"] = "group" + params["group_id"] = target_id + elif target_type == "direct": + params["detail_type"] = "private" + params["user_id"] = target_id + + if content: + params["message"] = [ + {"type": "text", "data": {"text": f"{content}"}} + ] + if at_list and target_type == "group": + params["message"].insert( + 0, {"type": "at", "data": {"user_id": f"{at_list[0]}"}} + ) + await bot.call_api('send_message', **params) + elif image: + img_bytes = base64.b64decode(image.replace('base64://', '')) + timestamp = time.time() + file_name = f'{target_id}_{timestamp}.png' + up_data = await bot.call_api( + 'upload_file', + type="data", + data=img_bytes, + name=f"{file_name}", + ) + file_id = up_data['file_id'] + await send_file_message(params, "image", file_id) + elif file: + file_name, file_content = file.split('|') + file_content = base64.b64decode(file) + up_data = await bot.call_api( + 'upload_file', + type="data", + data=file_content, + name=f"{file_name}", + ) + file_id = up_data['file_id'] + await send_file_message(params, "file", file_id) + + if node: + for _msg in node: + if _msg['type'] == 'image': + await _send(None, _msg['data']) + else: + await _send(_msg['data'], None) + else: + await _send(content, image)