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 360dddff5..17b9c82d3 100644 --- a/astrbot/core/platform/sources/gewechat/client.py +++ b/astrbot/core/platform/sources/gewechat/client.py @@ -203,7 +203,12 @@ class SimpleGewechatClient: else: user_real_name = self.userrealnames[abm.group_id][user_id] else: - user_real_name = d.get("PushContent", "unknown : ").split(" : ")[0] + 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 if at_me: abm.message.insert(0, At(qq=abm.self_id, name=self.nickname)) @@ -261,9 +266,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) + 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 476c37644..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") @@ -57,22 +67,44 @@ 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("