From afa1aa5d932e631eb5dfaa950cec0abd16697c68 Mon Sep 17 00:00:00 2001 From: XiGuang <1046532637@qq.com> Date: Fri, 18 Apr 2025 21:07:39 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20fix:=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9C=9F=E5=AE=9E=E5=A7=93=E5=90=8D=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=B9=E4=B8=BA=E4=BB=8E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E4=B8=AD=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/sources/gewechat/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astrbot/core/platform/sources/gewechat/client.py b/astrbot/core/platform/sources/gewechat/client.py index ccecc0c71..9fe0d0999 100644 --- a/astrbot/core/platform/sources/gewechat/client.py +++ b/astrbot/core/platform/sources/gewechat/client.py @@ -197,7 +197,8 @@ class SimpleGewechatClient: else: user_real_name = self.userrealnames[abm.group_id][user_id] else: - user_real_name = d.get("PushContent", "unknown : ").split(" : ")[0] + info = (await self.get_user_or_group_info(user_id))["data"][0] + user_real_name = info["nickName"] abm.sender = MessageMember(user_id, user_real_name) abm.raw_message = d From d916fda04c42968a084cc56639872ae975c0892f Mon Sep 17 00:00:00 2001 From: XiGuang <1046532637@qq.com> Date: Sat, 19 Apr 2025 12:10:51 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=E5=A2=9E=E5=BC=BA=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=BC=95=E7=94=A8=E5=B5=8C=E5=A5=97=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/sources/gewechat/client.py | 3 +++ .../platform/sources/gewechat/xml_data_parser.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/astrbot/core/platform/sources/gewechat/client.py b/astrbot/core/platform/sources/gewechat/client.py index 9fe0d0999..5045b71f8 100644 --- a/astrbot/core/platform/sources/gewechat/client.py +++ b/astrbot/core/platform/sources/gewechat/client.py @@ -252,6 +252,9 @@ class SimpleGewechatClient: abm_data = data_parser.parse_mutil_49() if abm_data: abm.message.append(abm_data) + abm.message_str = abm_data.message_str + abm.message.append(Plain(abm_data.message_str)) + abm.message[-2].message_str = abm.message[-2].sender_str case 51: # 帐号消息同步? logger.info("消息类型(51):帐号消息同步?") case 10000: # 被踢出群聊/更换群主/修改群名称 diff --git a/astrbot/core/platform/sources/gewechat/xml_data_parser.py b/astrbot/core/platform/sources/gewechat/xml_data_parser.py index 476c37644..ae05860c0 100644 --- a/astrbot/core/platform/sources/gewechat/xml_data_parser.py +++ b/astrbot/core/platform/sources/gewechat/xml_data_parser.py @@ -57,7 +57,20 @@ class GeweDataParser: if displayname is not None: replied_nickname = displayname.text if refermsg_content is not None: - replied_content = refermsg_content.text + # 处理引用嵌套,包括嵌套公众号消息 + if refermsg_content.text.startswith("") or refermsg_content.text.startswith(" Date: Sat, 19 Apr 2025 16:15:47 +0800 Subject: [PATCH 3/5] =?UTF-8?q?bug=20fix:=20=E6=9B=B4=E6=96=B0=E5=BC=95?= =?UTF-8?q?=E7=94=A8=E5=B5=8C=E5=A5=97=E6=B6=88=E6=81=AF=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/platform/sources/gewechat/xml_data_parser.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/astrbot/core/platform/sources/gewechat/xml_data_parser.py b/astrbot/core/platform/sources/gewechat/xml_data_parser.py index ae05860c0..a7a0452a0 100644 --- a/astrbot/core/platform/sources/gewechat/xml_data_parser.py +++ b/astrbot/core/platform/sources/gewechat/xml_data_parser.py @@ -62,9 +62,14 @@ class GeweDataParser: try: logger.debug("gewechat: Reference message is nested") refer_root = eT.fromstring(refermsg_content.text) - refermsg_content_title = refer_root.find("appmsg").find("title") - logger.debug(f"gewechat: Reference message nesting: {refermsg_content_title.text}") - replied_content = refermsg_content_title.text + img=refer_root.find("img") + if img is not None: + replied_content = "[图片]" + else: + app_msg=refer_root.find("appmsg") + refermsg_content_title = app_msg.find("title") + logger.debug(f"gewechat: Reference message nesting: {refermsg_content_title.text}") + replied_content = refermsg_content_title.text except Exception as e: logger.error(f"gewechat: nested failed, {e}") # 处理异常情况 From 0607b95df6d1cae61fbdc48fdd25ea10d2526393 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 20 Apr 2025 15:40:51 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=8E=88=20perf:=20=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/sources/gewechat/client.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/astrbot/core/platform/sources/gewechat/client.py b/astrbot/core/platform/sources/gewechat/client.py index 5045b71f8..e246e6a2c 100644 --- a/astrbot/core/platform/sources/gewechat/client.py +++ b/astrbot/core/platform/sources/gewechat/client.py @@ -197,8 +197,12 @@ class SimpleGewechatClient: else: user_real_name = self.userrealnames[abm.group_id][user_id] else: - info = (await self.get_user_or_group_info(user_id))["data"][0] - user_real_name = info["nickName"] + try: + info = (await self.get_user_or_group_info(user_id))["data"][0] + user_real_name = info["nickName"] + except Exception as e: + logger.debug(f"获取用户 {user_id} 昵称失败: {e}") + user_real_name = user_id abm.sender = MessageMember(user_id, user_real_name) abm.raw_message = d From d921b0f6bd332d82b1403448e2c5112f7e8b39f6 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 20 Apr 2025 16:00:59 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=8E=88=20perf:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=20gewechat=20=E7=9A=84=E5=BC=95=E7=94=A8=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/message/components.py | 12 +++--- .../core/platform/sources/gewechat/client.py | 12 +++--- .../sources/gewechat/xml_data_parser.py | 42 ++++++++++++------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index 781dfafca..5020d6b26 100644 --- a/astrbot/core/message/components.py +++ b/astrbot/core/message/components.py @@ -407,17 +407,15 @@ class Reply(BaseMessageComponent): id: T.Union[str, int] """所引用的消息 ID""" chain: T.Optional[T.List["BaseMessageComponent"]] = [] - """引用的消息段列表""" + """被引用的消息段列表""" sender_id: T.Optional[int] | T.Optional[str] = 0 - """引用的消息发送者 ID""" + """被引用的消息对应的发送者的 ID""" sender_nickname: T.Optional[str] = "" - """引用的消息发送者昵称""" + """被引用的消息对应的发送者的昵称""" time: T.Optional[int] = 0 - """引用的消息发送时间""" + """被引用的消息发送时间""" message_str: T.Optional[str] = "" - """解析后的纯文本消息字符串""" - sender_str: T.Optional[str] = "" - """被引用的消息纯文本""" + """被引用的消息解析后的纯文本消息字符串""" text: T.Optional[str] = "" """deprecated""" diff --git a/astrbot/core/platform/sources/gewechat/client.py b/astrbot/core/platform/sources/gewechat/client.py index e246e6a2c..e5e596b49 100644 --- a/astrbot/core/platform/sources/gewechat/client.py +++ b/astrbot/core/platform/sources/gewechat/client.py @@ -253,12 +253,12 @@ class SimpleGewechatClient: logger.info("消息类型(48):地理位置") case 49: # 公众号/文件/小程序/引用/转账/红包/视频号/群聊邀请 data_parser = GeweDataParser(content, abm.group_id == "") - abm_data = data_parser.parse_mutil_49() - if abm_data: - abm.message.append(abm_data) - abm.message_str = abm_data.message_str - abm.message.append(Plain(abm_data.message_str)) - abm.message[-2].message_str = abm.message[-2].sender_str + segments = data_parser.parse_mutil_49() + if segments: + abm.message.extend(segments) + for seg in segments: + if isinstance(seg, Plain): + abm.message_str += seg.text case 51: # 帐号消息同步? logger.info("消息类型(51):帐号消息同步?") case 10000: # 被踢出群聊/更换群主/修改群名称 diff --git a/astrbot/core/platform/sources/gewechat/xml_data_parser.py b/astrbot/core/platform/sources/gewechat/xml_data_parser.py index a7a0452a0..1af4a051a 100644 --- a/astrbot/core/platform/sources/gewechat/xml_data_parser.py +++ b/astrbot/core/platform/sources/gewechat/xml_data_parser.py @@ -1,6 +1,11 @@ from defusedxml import ElementTree as eT from astrbot.api import logger -from astrbot.api.message_components import WechatEmoji as Emoji, Reply, Plain +from astrbot.api.message_components import ( + WechatEmoji as Emoji, + Reply, + Plain, + BaseMessageComponent, +) class GeweDataParser: @@ -11,7 +16,7 @@ class GeweDataParser: def _format_to_xml(self): return eT.fromstring(self.data) - def parse_mutil_49(self): + def parse_mutil_49(self) -> list[BaseMessageComponent] | None: appmsg_type = self._format_to_xml().find(".//appmsg/type") if appmsg_type is None: return @@ -34,13 +39,18 @@ class GeweDataParser: except Exception as e: logger.error(f"gewechat: parse_emoji failed, {e}") - def parse_reply(self) -> Reply | None: + def parse_reply(self) -> list[Reply, Plain] | None: + """解析引用消息 + + Returns: + list[Reply, Plain]: 一个包含两个元素的列表。Reply 消息对象和引用者说的文本内容。微信平台下引用消息时只能发送文本消息。 + """ try: replied_id = -1 replied_uid = 0 replied_nickname = "" - replied_content = "" - content = "" + replied_content = "" # 被引用者说的内容 + content = "" # 引用者说的内容 root = self._format_to_xml() refermsg = root.find(".//refermsg") @@ -58,17 +68,21 @@ class GeweDataParser: replied_nickname = displayname.text if refermsg_content is not None: # 处理引用嵌套,包括嵌套公众号消息 - if refermsg_content.text.startswith("") or refermsg_content.text.startswith("" + ) or refermsg_content.text.startswith("