From d191997a3984f07ed3ffc3a257601a303ac3d98d Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 27 Jul 2024 11:07:26 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20aiocqhttp=20=E9=80=82=E9=85=8D=E5=99=A8?= =?UTF-8?q?=E9=80=82=E9=85=8D=E4=B8=BB=E5=8A=A8=E5=8F=91=E9=80=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/platform/qq_aiocqhttp.py | 29 ++++++++++++++++++++++++++--- model/platform/qq_official.py | 11 ++++++----- util/plugin_dev/api/v1/platform.py | 3 ++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/model/platform/qq_aiocqhttp.py b/model/platform/qq_aiocqhttp.py index e0d81053b..fadb49df8 100644 --- a/model/platform/qq_aiocqhttp.py +++ b/model/platform/qq_aiocqhttp.py @@ -165,7 +165,7 @@ class AIOCQHTTP(Platform): await self._reply(message, res) - async def _reply(self, message: AstrBotMessage, message_chain: List[BaseMessageComponent]): + async def _reply(self, message: Union[AstrBotMessage, Dict], message_chain: List[BaseMessageComponent]): if isinstance(message_chain, str): message_chain = [Plain(text=message_chain), ] @@ -179,7 +179,15 @@ class AIOCQHTTP(Platform): image_idx.append(idx) ret.append(d) try: - await self.bot.send(message.raw_message, ret) + if isinstance(message, AstrBotMessage): + await self.bot.send(message.raw_message, ret) + if isinstance(message, dict): + if 'group_id' in message: + await self.bot.send_group_msg(group_id=message['group_id'], message=ret) + elif 'user_id' in message: + await self.bot.send_private_msg(user_id=message['user_id'], message=ret) + else: + raise Exception("aiocqhttp: 无法识别的消息来源。仅支持 group_id 和 user_id。") except ActionFailed as e: logger.error(traceback.format_exc()) logger.error(f"回复消息失败: {e}") @@ -195,4 +203,19 @@ class AIOCQHTTP(Platform): logger.info(f"上传成功。") ret[idx]['data']['file'] = image_url ret[idx]['data']['path'] = image_url - await self.bot.send(message.raw_message, ret) \ No newline at end of file + await self.bot.send(message.raw_message, ret) + + async def send_msg(self, target: Dict[str, int], result_message: Union[List[BaseMessageComponent], str]): + ''' + 以主动的方式给QQ用户、QQ群发送一条消息。 + + `target` 接收一个 dict 类型的值引用。 + + - 要发给 QQ 下的某个用户,请添加 key `user_id`,值为 int 类型的 qq 号; + - 要发给某个群聊,请添加 key `group_id`,值为 int 类型的 qq 群号; + + ''' + if isinstance(result_message, str): + result_message = [Plain(text=result_message), ] + + await self._reply(target, result_message) \ No newline at end of file diff --git a/model/platform/qq_official.py b/model/platform/qq_official.py index 8c7fc2fe9..34750600f 100644 --- a/model/platform/qq_official.py +++ b/model/platform/qq_official.py @@ -306,8 +306,7 @@ class QQOfficial(Platform): async def _reply(self, **kwargs): if 'group_openid' in kwargs: # QQ群组消息 - # qq群组消息需要自行上传,暂时不处理 - if 'file_image' in kwargs: + if 'file_image' in kwargs and kwargs['file_image']: file_image_path = kwargs['file_image'].replace("file:///", "") if file_image_path: @@ -325,7 +324,7 @@ class QQOfficial(Platform): await self.client.api.post_group_message(**kwargs) elif 'channel_id' in kwargs: # 频道消息 - if 'file_image' in kwargs: + if 'file_image' in kwargs and kwargs['file_image']: kwargs['file_image'] = kwargs['file_image'].replace("file:///", "") # 频道消息发图只支持本地 if kwargs['file_image'].startswith("http"): @@ -333,7 +332,7 @@ class QQOfficial(Platform): await self.client.api.post_message(**kwargs) else: # 频道私聊消息 - if 'file_image' in kwargs: + if 'file_image' in kwargs and kwargs['file_image']: kwargs['file_image'] = kwargs['file_image'].replace("file:///", "") if kwargs['file_image'].startswith("http"): kwargs['file_image'] = await download_image_by_url(kwargs['file_image']) @@ -349,6 +348,7 @@ class QQOfficial(Platform): - 如果目标是 频道消息,请添加 key `channel_id`。 - 如果目标是 频道私聊,请添加 key `guild_id`。 ''' + image_path = None if isinstance(result_message, list): plain_text, image_path = await self._parse_to_qqofficial(result_message) else: @@ -356,9 +356,10 @@ class QQOfficial(Platform): payload = { 'content': plain_text, - 'file_image': image_path, **target } + if image_path: + payload['file_image'] = image_path await self._reply(**payload) def wait_for_message(self, channel_id: int) -> AstrBotMessage: diff --git a/util/plugin_dev/api/v1/platform.py b/util/plugin_dev/api/v1/platform.py index 47bd617e9..24ad51ba0 100644 --- a/util/plugin_dev/api/v1/platform.py +++ b/util/plugin_dev/api/v1/platform.py @@ -8,4 +8,5 @@ Platform类是消息平台的抽象类,定义了消息平台的基本接口。 from model.platform import Platform from model.platform.qq_nakuru import QQGOCQ -from model.platform.qq_official import QQOfficial \ No newline at end of file +from model.platform.qq_official import QQOfficial +from model.platform.qq_aiocqhttp import AIOCQHTTP \ No newline at end of file