From 577dc0d175904d4323ccc002e240e34a04198046 Mon Sep 17 00:00:00 2001 From: Dong_master <2213070223@qq.com> Date: Wed, 23 Apr 2025 02:25:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=AF=AD=E9=9F=B3=E6=B6=88=E6=81=AF=E5=92=8C=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=BE=A4=E8=81=8A=E5=9B=BE=E7=89=87=E6=B6=88=E6=81=AF=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=8F=91=E9=80=81=E8=AF=AD=E9=9F=B3?= =?UTF-8?q?=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/platform/sources/gewechat.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/platform/sources/gewechat.py b/pkg/platform/sources/gewechat.py index cac732a1..ba8f0490 100644 --- a/pkg/platform/sources/gewechat.py +++ b/pkg/platform/sources/gewechat.py @@ -58,6 +58,8 @@ class GewechatMessageConverter(adapter.MessageConverter): elif isinstance(component, platform_message.WeChatLink): content_list.append({'type': 'WeChatLink', 'link_title': component.link_title, 'link_desc': component.link_desc, 'link_thumb_url': component.link_thumb_url, 'link_url': component.link_url}) + elif isinstance(component, platform_message.WeChatForwardLink): + content_list.append({'type': 'WeChatLink', 'xml_data': component.xml_data}) elif isinstance(component, platform_message.Voice): @@ -108,6 +110,9 @@ class GewechatMessageConverter(adapter.MessageConverter): elif message["Data"]["MsgType"] == 3: image_xml = message["Data"]["Content"]["string"] + if image_xml.startswith('wxid'): # 此处处理群聊发送图片会有微信id开头 + xml_list = image_xml.split('\n')[2:] + image_xml = '\n'.join(xml_list) if not image_xml: return platform_message.MessageChain([ platform_message.Plain(text="[图片内容为空]") @@ -135,10 +140,15 @@ class GewechatMessageConverter(adapter.MessageConverter): platform_message.Plain(text=f"[图片处理失败]") ]) elif message["Data"]["MsgType"] == 34: - audio_base64 = message["Data"]["ImgBuf"]["buffer"] - return platform_message.MessageChain( - [platform_message.Voice(base64=f"data:audio/silk;base64,{audio_base64}")] - ) + try: + audio_base64 = message["Data"]["ImgBuf"]["buffer"] + return platform_message.MessageChain( + [platform_message.Voice(base64=f"data:audio/silk;base64,{audio_base64}")] + ) + except Exception as e: + return platform_message.MessageChain( + [platform_message.Plain(text="[无法解析群聊语音的消息]")] # 小测了一下,免费版拿不到群聊语音消息的base64,或者用什么办法解析xml里的url? + ) elif message["Data"]["MsgType"] == 49: # 支持微信聊天记录的消息类型,将 XML 内容转换为 MessageChain 传递 try: @@ -389,6 +399,11 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter): ,title=msg['link_title'], desc=msg['link_desc'] , link_url=msg['link_url'], thumb_url=msg['link_thumb_url']) + elif msg['type'] == 'WeChatForwardMiniPrograms': + self.bot.forward_mini_app(app_id=self.config['app_id'], to_wxid=target_id, xml=msg['xml_data']) + elif msg['type'] == 'voice': + self.bot.post_voice(app_id=self.config['app_id'], to_wxid=target_id, voice_url=msg['voice_url'],voice_duration=msg['length']) + async def reply_message( @@ -437,6 +452,9 @@ class GeWeChatAdapter(adapter.MessagePlatformAdapter): self.bot.post_link(app_id=self.config['app_id'], to_wxid=target_id , title=msg['link_title'], desc=msg['link_desc'] , link_url=msg['link_url'], thumb_url=msg['link_thumb_url']) + elif msg['type'] == 'voice': + self.bot.post_voice(app_id=self.config['app_id'], to_wxid=target_id, voice_url=msg['voice_url'], + voice_duration=msg['length']) async def is_muted(self, group_id: int) -> bool: pass