From 718c221d01606607207b9f8f92f6ccb200fb14ee Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 24 Aug 2024 16:26:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=AE=98?= =?UTF-8?q?=E6=96=B9=E6=9C=BA=E5=99=A8=E4=BA=BA=E7=A7=81=E4=BF=A1=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/sources/qqbotpy.py | 44 ++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/pkg/platform/sources/qqbotpy.py b/pkg/platform/sources/qqbotpy.py index faa5f10..f607dd6 100644 --- a/pkg/platform/sources/qqbotpy.py +++ b/pkg/platform/sources/qqbotpy.py @@ -23,10 +23,12 @@ from ...config import manager as cfg_mgr class OfficialGroupMessage(mirai.GroupMessage): pass +class OfficialFriendMessage(mirai.FriendMessage): + pass event_handler_mapping = { mirai.GroupMessage: ["on_at_message_create", "on_group_at_message_create"], - mirai.FriendMessage: ["on_direct_message_create"], + mirai.FriendMessage: ["on_direct_message_create", "on_c2c_message_create"], } @@ -193,7 +195,7 @@ class OfficialMessageConverter(adapter_model.MessageConverter): @staticmethod def extract_message_chain_from_obj( - message: typing.Union[botpy_message.Message, botpy_message.DirectMessage], + message: typing.Union[botpy_message.Message, botpy_message.DirectMessage, botpy_message.GroupMessage, botpy_message.C2CMessage], message_id: str = None, bot_account_id: int = 0, ) -> mirai.MessageChain: @@ -206,7 +208,7 @@ class OfficialMessageConverter(adapter_model.MessageConverter): ) ) - if type(message) is not botpy_message.DirectMessage: + if type(message) not in [botpy_message.DirectMessage, botpy_message.C2CMessage]: yiri_msg_list.append(mirai.At(target=bot_account_id)) if hasattr(message, "mentions"): @@ -255,7 +257,7 @@ class OfficialEventConverter(adapter_model.EventConverter): def target2yiri( self, - event: typing.Union[botpy_message.Message, botpy_message.DirectMessage] + event: typing.Union[botpy_message.Message, botpy_message.DirectMessage, botpy_message.GroupMessage, botpy_message.C2CMessage], ) -> mirai.Event: import mirai.models.entities as mirai_entities @@ -295,7 +297,7 @@ class OfficialEventConverter(adapter_model.EventConverter): ).timestamp() ), ) - elif type(event) == botpy_message.DirectMessage: # 私聊,转私聊事件 + elif type(event) == botpy_message.DirectMessage: # 频道私聊,转私聊事件 return mirai.FriendMessage( sender=mirai_entities.Friend( id=event.guild_id, @@ -311,7 +313,7 @@ class OfficialEventConverter(adapter_model.EventConverter): ).timestamp() ), ) - elif type(event) == botpy_message.GroupMessage: + elif type(event) == botpy_message.GroupMessage: # 群聊,转群聊事件 replacing_member_id = self.member_openid_mapping.save_openid(event.author.member_openid) @@ -339,6 +341,25 @@ class OfficialEventConverter(adapter_model.EventConverter): ).timestamp() ), ) + elif type(event) == botpy_message.C2CMessage: # 私聊,转私聊事件 + + user_id_alter = self.member_openid_mapping.save_openid(event.author.user_openid) # 实测这里的user_openid与group的member_openid是一样的 + + return OfficialFriendMessage( + sender=mirai_entities.Friend( + id=user_id_alter, + nickname=user_id_alter, + remark=user_id_alter, + ), + message_chain=OfficialMessageConverter.extract_message_chain_from_obj( + event, event.id + ), + time=int( + datetime.datetime.strptime( + event.timestamp, "%Y-%m-%dT%H:%M:%S%z" + ).timestamp() + ), + ) @adapter_model.adapter_class("qq-botpy") @@ -466,6 +487,17 @@ class OfficialAdapter(adapter_model.MessageSourceAdapter): args["msg_seq"] = self.group_msg_seq self.group_msg_seq += 1 await self.bot.api.post_group_message(**args) + elif type(message_source) == OfficialFriendMessage: + if "image" in args or "file_image" in args: + continue + args["openid"] = self.member_openid_mapping.getkey( + message_source.sender.id + ) + + args["msg_id"] = cached_message_ids[ + str(message_source.message_chain.message_id) + ] + await self.bot.api.post_c2c_message(**args) async def is_muted(self, group_id: int) -> bool: return False From 4c8fedef6ecae016415f989b61c8639cb05da7b9 Mon Sep 17 00:00:00 2001 From: RockChinQ <1010553892@qq.com> Date: Sat, 24 Aug 2024 17:01:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20QQ=E5=AE=98=E6=96=B9api=E7=BE=A4?= =?UTF-8?q?=E8=81=8A=E5=92=8C=E7=A7=81=E8=81=8A=E6=94=AF=E6=8C=81=E5=9B=BE?= =?UTF-8?q?=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/sources/qqbotpy.py | 40 ++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/pkg/platform/sources/qqbotpy.py b/pkg/platform/sources/qqbotpy.py index f607dd6..a79013c 100644 --- a/pkg/platform/sources/qqbotpy.py +++ b/pkg/platform/sources/qqbotpy.py @@ -3,16 +3,15 @@ from __future__ import annotations import logging import typing import datetime -import asyncio import re import traceback -import json -import threading import mirai import botpy import botpy.message as botpy_message import botpy.types.message as botpy_message_type +import pydantic +import pydantic.networks from .. import adapter as adapter_model from ...pipeline.longtext.strategies import forward @@ -389,6 +388,7 @@ class OfficialAdapter(adapter_model.MessageSourceAdapter): group_openid_mapping: OpenIDMapping[str, int] = None group_msg_seq = None + c2c_msg_seq = None def __init__(self, cfg: dict, ap: app.Application): """初始化适配器""" @@ -396,6 +396,7 @@ class OfficialAdapter(adapter_model.MessageSourceAdapter): self.ap = ap self.group_msg_seq = 1 + self.c2c_msg_seq = 1 switchs = {} @@ -475,28 +476,57 @@ class OfficialAdapter(adapter_model.MessageSourceAdapter): ] await self.bot.api.post_dms(**args) elif type(message_source) == OfficialGroupMessage: - if "image" in args or "file_image" in args: + + if "file_image" in args: # 暂不支持发送文件图片 continue + args["group_openid"] = self.group_openid_mapping.getkey( message_source.sender.group.id ) + if "image" in args: + uploadMedia = await self.bot.api.post_group_file( + group_openid=args["group_openid"], + file_type=1, + url=str(args['image']) + ) + + del args['image'] + args['media'] = uploadMedia + args['msg_type'] = 7 + args["msg_id"] = cached_message_ids[ str(message_source.message_chain.message_id) ] args["msg_seq"] = self.group_msg_seq self.group_msg_seq += 1 + await self.bot.api.post_group_message(**args) elif type(message_source) == OfficialFriendMessage: - if "image" in args or "file_image" in args: + if "file_image" in args: continue args["openid"] = self.member_openid_mapping.getkey( message_source.sender.id ) + if "image" in args: + uploadMedia = await self.bot.api.post_c2c_file( + openid=args["openid"], + file_type=1, + url=str(args['image']) + ) + + del args['image'] + args['media'] = uploadMedia + args['msg_type'] = 7 + args["msg_id"] = cached_message_ids[ str(message_source.message_chain.message_id) ] + + args["msg_seq"] = self.c2c_msg_seq + self.c2c_msg_seq += 1 + await self.bot.api.post_c2c_message(**args) async def is_muted(self, group_id: int) -> bool: