From 768398b9915f3941bb1a22fe6de86e691b273bd6 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Thu, 6 Mar 2025 22:26:58 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=94=AF=E6=8C=81=20gewech?= =?UTF-8?q?at=20=E5=9B=BE=E7=89=87=E7=AD=89=E6=9B=B4=E5=A4=9A=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E4=B8=BB=E5=8A=A8=E6=B6=88=E6=81=AF=20#710?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sources/gewechat/gewechat_event.py | 32 +++++++++---------- .../gewechat/gewechat_platform_adapter.py | 18 ++++++----- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/astrbot/core/platform/sources/gewechat/gewechat_event.py b/astrbot/core/platform/sources/gewechat/gewechat_event.py index 4a90065c2..6a13c5bf6 100644 --- a/astrbot/core/platform/sources/gewechat/gewechat_event.py +++ b/astrbot/core/platform/sources/gewechat/gewechat_event.py @@ -37,12 +37,9 @@ class GewechatPlatformEvent(AstrMessageEvent): self.client = client @staticmethod - async def send_with_client(message: MessageChain, user_name: str): - pass - - async def send(self, message: MessageChain): - to_wxid = self.message_obj.raw_message.get("to_wxid", None) - + async def send_with_client( + message: MessageChain, to_wxid: str, client: SimpleGewechatClient + ): if not to_wxid: logger.error("无法获取到 to_wxid。") return @@ -70,7 +67,7 @@ class GewechatPlatformEvent(AstrMessageEvent): payload["content"] = text payload["ats"] = ats has_at = True - await self.client.post_text(**payload) + await client.post_text(**payload) elif isinstance(comp, Image): img_url = comp.file @@ -90,9 +87,9 @@ class GewechatPlatformEvent(AstrMessageEvent): img_path = save_temp_img(f.read()) file_id = os.path.basename(img_path) - img_url = f"{self.client.file_server_url}/{file_id}" + img_url = f"{client.file_server_url}/{file_id}" logger.debug(f"gewe callback img url: {img_url}") - await self.client.post_image(to_wxid, img_url) + await client.post_image(to_wxid, img_url) elif isinstance(comp, Record): # 默认已经存在 data/temp 中 record_url = comp.file @@ -110,16 +107,14 @@ class GewechatPlatformEvent(AstrMessageEvent): duration = await wav_to_tencent_silk(record_path, silk_path) except Exception as e: logger.error(traceback.format_exc()) - await self.send( - MessageChain().message(f"语音文件转换失败。{str(e)}") - ) + await client.post_text(to_wxid, f"语音文件转换失败。{str(e)}") logger.info("Silk 语音文件格式转换至: " + record_path) if duration == 0: duration = get_wav_duration(record_path) file_id = os.path.basename(silk_path) - record_url = f"{self.client.file_server_url}/{file_id}" + record_url = f"{client.file_server_url}/{file_id}" logger.debug(f"gewe callback record url: {record_url}") - await self.client.post_voice(to_wxid, record_url, duration * 1000) + await client.post_voice(to_wxid, record_url, duration * 1000) elif isinstance(comp, File): file_path = comp.file file_name = comp.name @@ -131,12 +126,17 @@ class GewechatPlatformEvent(AstrMessageEvent): file_path = file_path file_id = os.path.basename(file_path) - file_url = f"{self.client.file_server_url}/{file_id}" + file_url = f"{client.file_server_url}/{file_id}" logger.debug(f"gewe callback file url: {file_url}") - await self.client.post_file(to_wxid, file_url, file_id) + await client.post_file(to_wxid, file_url, file_id) elif isinstance(comp, At): pass else: logger.debug(f"gewechat 忽略: {comp.type}") + async def send(self, message: MessageChain): + to_wxid = self.message_obj.raw_message.get("to_wxid", None) + await GewechatPlatformEvent.send_with_client( + message, to_wxid, self.client + ) await super().send(message) diff --git a/astrbot/core/platform/sources/gewechat/gewechat_platform_adapter.py b/astrbot/core/platform/sources/gewechat/gewechat_platform_adapter.py index c4751c7b1..fe770500d 100644 --- a/astrbot/core/platform/sources/gewechat/gewechat_platform_adapter.py +++ b/astrbot/core/platform/sources/gewechat/gewechat_platform_adapter.py @@ -45,14 +45,16 @@ class GewechatPlatformAdapter(Platform): async def send_by_session( self, session: MessageSesion, message_chain: MessageChain ): - to_wxid = session.session_id - if not to_wxid: - logger.error("无法获取到 to_wxid。") - return + session_id = session.session_id + if "#" in session_id: + # unique session + to_wxid = session_id.split("#")[1] + else: + to_wxid = session_id - for comp in message_chain.chain: - if isinstance(comp, Plain): - await self.client.post_text(to_wxid, comp.text) + await GewechatPlatformEvent.send_with_client( + message_chain, to_wxid, self.client + ) await super().send_by_session(session, message_chain) @@ -81,7 +83,7 @@ class GewechatPlatformAdapter(Platform): async def handle_msg(self, message: AstrBotMessage): if message.type == MessageType.GROUP_MESSAGE: if self.settingss["unique_session"]: - message.session_id = message.sender.user_id + "_" + message.group_id + message.session_id = message.sender.user_id + "#" + message.group_id message_event = GewechatPlatformEvent( message_str=message.message_str,