From 61a23617303f938b6c2e8366c8b037318a61fb3f Mon Sep 17 00:00:00 2001 From: fdc310 <82008029+fdc310@users.noreply.github.com> Date: Sun, 15 Jun 2025 17:17:08 +0800 Subject: [PATCH] feat:add new messagetyps WeChatFile and add wechat file is accepted and transmitted in base64 format. (#1531) --- pkg/platform/sources/wechatpad.py | 40 +++++++++++++++++++++++++++---- pkg/platform/types/message.py | 19 +++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/pkg/platform/sources/wechatpad.py b/pkg/platform/sources/wechatpad.py index 2f58f0e1..fdd4a69b 100644 --- a/pkg/platform/sources/wechatpad.py +++ b/pkg/platform/sources/wechatpad.py @@ -235,6 +235,7 @@ class WeChatPadMessageConverter(adapter.MessageConverter): '57': self._handler_compound_quote, '5': self._handler_compound_link, '6': self._handler_compound_file, + '74': self._handler_compound_file, '33': self._handler_compound_mini_program, '36': self._handler_compound_mini_program, '2000': partial(self._handler_compound_unsupported, text="[转账消息]"), @@ -320,10 +321,41 @@ class WeChatPadMessageConverter(adapter.MessageConverter): xml_data: ET.Element ) -> platform_message.MessageChain: """处理文件消息 (data_type=6)""" - xml_data_str = ET.tostring(xml_data, encoding='unicode') - return platform_message.MessageChain([ - platform_message.WeChatForwardFile(xml_data=xml_data_str) - ]) + file_data = xml_data.find('.//appmsg') + + if file_data.findtext('.//type', "") == "74": + return None + + else: + xml_data_str = ET.tostring(xml_data, encoding='unicode') + # print(xml_data_str) + + # 提取img标签的属性 + # print(xml_data) + file_name = file_data.find('title').text + file_id = file_data.find('md5').text + # file_szie = file_data.find('totallen') + + # print(file_data) + if file_data is not None: + aeskey = xml_data.findtext('.//appattach/aeskey') + cdnthumburl = xml_data.findtext('.//appattach/cdnattachurl') + # cdnmidimgurl = img_tag.get('cdnmidimgurl') + + # print(aeskey,cdnthumburl) + + file_data = self.bot.cdn_download(aeskey=aeskey, file_type=5, file_url=cdnthumburl) + + file_base64 = file_data["Data"]['FileData'] + # print(file_data) + file_size = file_data["Data"]['TotalSize'] + + # print(file_base64) + return platform_message.MessageChain([ + platform_message.WeChatFile(file_id=file_id, file_name=file_name, file_size=file_size, + file_base64=file_base64), + platform_message.WeChatForwardFile(xml_data=xml_data_str) + ]) async def _handler_compound_link( self, diff --git a/pkg/platform/types/message.py b/pkg/platform/types/message.py index e137ae46..88251cd1 100644 --- a/pkg/platform/types/message.py +++ b/pkg/platform/types/message.py @@ -922,3 +922,22 @@ class WeChatForwardQuote(MessageComponent): def __str__(self): return self.app_msg + + +class WeChatFile(MessageComponent): + """文件。""" + + type: str = 'File' + """消息组件类型。""" + file_id: str = '' + """文件识别 ID。""" + file_name: str = '' + """文件名称。""" + file_size: int = '' + """文件大小。""" + file_path: str = '' + """文件地址""" + file_base64: str = '' + """base64""" + def __str__(self): + return f'[文件]{self.file_name}' \ No newline at end of file