From 13549fc4def570f0974e4695574c19efcf575aba Mon Sep 17 00:00:00 2001 From: Han Fangyuan Date: Tue, 17 Dec 2024 22:11:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20gewechat=E6=94=AF=E6=8C=81=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7=E5=88=86=E4=BA=AB=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/gewechat/gewechat_message.py | 43 +++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/channel/gewechat/gewechat_message.py b/channel/gewechat/gewechat_message.py index 179e68cd9..7580fff3a 100644 --- a/channel/gewechat/gewechat_message.py +++ b/channel/gewechat/gewechat_message.py @@ -101,24 +101,41 @@ def __init__(self, msg, client: GewechatClient): self.ctype = ContextType.IMAGE self.content = TmpDir().path() + str(self.msg_id) + ".png" self._prepare_fn = self.download_image - elif msg_type == 49: # 引用消息,小程序等 - self.ctype = ContextType.TEXT + elif msg_type == 49: # 引用消息,小程序,公众号等 content_xml = msg['Data']['Content']['string'] - # 解析XML获取引用的消息内容 + # 解析XML获取内容 root = ET.fromstring(content_xml) appmsg = root.find('appmsg') - # 引用消息类型 - if appmsg is not None and appmsg.find('type').text == '57': - refermsg = appmsg.find('refermsg') - if refermsg is not None: - displayname = refermsg.find('displayname').text - quoted_content = refermsg.find('content').text - title = appmsg.find('title').text - self.content = f"「引用内容\n{displayname}: {quoted_content}」\n{title}" - else: + + if appmsg is not None: + msg_type = appmsg.find('type') + if msg_type is not None and msg_type.text == '57': # 引用消息 + self.ctype = ContextType.TEXT + refermsg = appmsg.find('refermsg') + if refermsg is not None: + displayname = refermsg.find('displayname').text + quoted_content = refermsg.find('content').text + title = appmsg.find('title').text + self.content = f"「引用内容\n{displayname}: {quoted_content}」\n{title}" + else: + self.content = content_xml + elif msg_type is not None and msg_type.text == '5': # 可能是公众号文章 + title = appmsg.find('title').text if appmsg.find('title') is not None else "无标题" + if "加入群聊" in title: + # 群聊邀请消息 + self.ctype = ContextType.TEXT + self.content = content_xml + else: + # 公众号文章 + self.ctype = ContextType.SHARING + url = appmsg.find('url').text if appmsg.find('url') is not None else "" + self.content = url + + else: # 其他消息类型,暂时不解析,直接返回XML + self.ctype = ContextType.TEXT self.content = content_xml - # 其他消息类型,暂时不解析,直接返回XML else: + self.ctype = ContextType.TEXT self.content = content_xml else: raise NotImplementedError("Unsupported message type: Type:{}".format(msg_type))