diff --git a/astrbot/core/message/components.py b/astrbot/core/message/components.py index 781dfafc..5020d6b2 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 e246e6a2..e5e596b4 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 a7a0452a..1af4a051 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("