From 2f4d434a51db19d710d5409b5e5c72d60a674c66 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:29:40 +0800 Subject: [PATCH 1/2] fix(qqofficial): stop inventing Reply on passive echoes and recover flattened quoted images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QQ tags a passive reply with message_type 103 and puts the previous message into msg_elements even when the user only @-mentioned the bot. The adapter built a Reply whenever that echo produced any text, so quote-preferring plugins started verifying a message nobody had quoted. A quoted message id is what distinguishes a real quote from the echo, so the Reply is now built only when message_reference.message_id or the quoted element's own id is present. The echo text is dropped rather than presented as a quote. QQ also pre-renders a quoted attachment into the quoted text as an [附件N] 类型:图片 ... URL: line and leaves attachments empty, so quoted images reached plugins as prose. Those lines are lifted back into Image components and removed from the text. A populated attachments list still wins, so the existing path is unchanged. --- .../qqofficial/qqofficial_platform_adapter.py | 50 ++++++++- tests/test_qqofficial_group_message_create.py | 106 ++++++++++++++++++ 2 files changed, 155 insertions(+), 1 deletion(-) diff --git a/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py b/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py index bdccecd48eb..00d0b530fb8 100644 --- a/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py +++ b/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py @@ -4,6 +4,7 @@ import logging import os import random +import re import time from pathlib import Path from types import SimpleNamespace @@ -716,6 +717,32 @@ async def _append_attachments( else: msg.append(File(name=filename, file=url, url=url)) + #: QQ renders a quoted attachment into the quoted text as + #: ``[附件N] 类型:图片 ... URL:`` and leaves ``attachments`` empty, so the + #: image would otherwise reach plugins as unstructured prose. + _FLATTENED_IMAGE_LINE_RE = re.compile( + r"^\[附件\d+\]\s*类型:图片\b[^\n]*?URL:(\S+)[^\n]*\n?", + re.MULTILINE, + ) + + @staticmethod + def _extract_flattened_quoted_images(content: str) -> tuple[str, list[str]]: + """Split flattened quoted image lines back out of pre-rendered text. + + Returns the remaining text and the image URLs in document order. Only + image attachments are lifted; other attachment kinds stay as text. + """ + urls: list[str] = [] + + def _collect(match: re.Match[str]) -> str: + urls.append(match.group(1)) + return "" + + return QQOfficialPlatformAdapter._FLATTENED_IMAGE_LINE_RE.sub( + _collect, + content, + ).strip(), urls + @staticmethod def _parse_face_message(content: str) -> str: """Parse QQ official face message format and convert to readable text. @@ -836,14 +863,35 @@ async def _parse_from_qqofficial( quoted_message_str = QQOfficialPlatformAdapter._parse_face_message( str(quoted_content or "").strip() ) + quoted_message_str, flattened_image_urls = ( + QQOfficialPlatformAdapter._extract_flattened_quoted_images( + quoted_message_str + ) + ) if quoted_message_str: quoted_chain.append(Plain(quoted_message_str)) + has_attachments = isinstance(quoted_attachments, list) and bool( + quoted_attachments + ) if isinstance(quoted_attachments, list): await QQOfficialPlatformAdapter._append_attachments( quoted_chain, quoted_attachments, ) - if quoted_message_id or quoted_element_message_id or quoted_chain: + if not has_attachments: + for image_url in flattened_image_urls: + quoted_chain.append( + Image.fromURL( + QQOfficialPlatformAdapter._normalize_attachment_url( + image_url + ) + ) + ) + # QQ tags a passive reply with message_type 103 and stuffs the previous + # message into msg_elements even when the user quoted nothing, so a + # message id is the only thing that marks a real quote. Building a Reply + # out of the echo makes downstream plugins verify the wrong message. + if quoted_message_id or quoted_element_message_id: msg.append( Reply( id=str(quoted_message_id or quoted_element_message_id or ""), diff --git a/tests/test_qqofficial_group_message_create.py b/tests/test_qqofficial_group_message_create.py index 64373549845..659c6a3f99a 100644 --- a/tests/test_qqofficial_group_message_create.py +++ b/tests/test_qqofficial_group_message_create.py @@ -1128,3 +1128,109 @@ async def test_webhook_send_by_session_use_markdown_config_false_sends_content() assert kwargs["content"] == "webhook plain content" assert "markdown" not in kwargs assert "msg_type" not in kwargs + + +@pytest.mark.asyncio +async def test_passive_reply_echo_builds_no_reply_component(): + """A `message_type=103` echo with no message id is not a user quote. + + QQ tags a passive reply with 103 and puts the previous message in + `msg_elements`, so an ordinary `@bot` after a reply arrives looking like a + quote. Downstream plugins then verify the wrong message. + """ + _, message = _dispatch_group_message( + _make_group_payload( + content="is the earth flat?", + message_type=103, + message_reference=None, + msg_elements=[ + { + "content": "the previous bot answer", + "attachments": [], + } + ], + ) + ) + + abm = await QQOfficialPlatformAdapter._parse_from_qqofficial( + message, + MessageType.GROUP_MESSAGE, + ) + + assert not any(isinstance(component, Reply) for component in abm.message) + assert [ + component.text for component in abm.message if isinstance(component, Plain) + ] == ["is the earth flat?"] + + +@pytest.mark.asyncio +async def test_quoted_element_id_still_builds_reply_without_reference(): + """A quoted element carrying its own id is a real quote even without `message_reference`.""" + _, message = _dispatch_group_message( + _make_group_payload( + content="answer", + message_type=103, + message_reference=None, + msg_elements=[ + { + "id": "element-7", + "content": "quoted text", + "attachments": [], + } + ], + ) + ) + + abm = await QQOfficialPlatformAdapter._parse_from_qqofficial( + message, + MessageType.GROUP_MESSAGE, + ) + + reply = abm.message[0] + assert isinstance(reply, Reply) + assert reply.id == "element-7" + assert reply.message_str == "quoted text" + + +@pytest.mark.asyncio +async def test_quoted_images_flattened_into_text_are_recovered_as_images(): + """QQ pre-renders quoted attachments into `content`, leaving `attachments` empty.""" + _, message = _dispatch_group_message( + _make_group_payload( + content="answer", + message_type=103, + message_reference={"message_id": "quoted-1"}, + msg_elements=[ + { + "content": ( + "=== 消息 1 ===\n" + "[消息类型] 引用消息\n" + "[附件1] 类型:图片 文件名:chart.png 尺寸:2117x287 大小:57.3KB " + "URL:https://multimedia.example.com/chart.png\n" + "\n" + "=== 消息 2 ===\n" + "[消息内容] the old message text" + ), + "attachments": [], + } + ], + ) + ) + + abm = await QQOfficialPlatformAdapter._parse_from_qqofficial( + message, + MessageType.GROUP_MESSAGE, + ) + + reply = abm.message[0] + assert isinstance(reply, Reply) + assert reply.id == "quoted-1" + images = [component for component in reply.chain if isinstance(component, Image)] + assert [component.file for component in images] == [ + "https://multimedia.example.com/chart.png" + ] + plain_text = "".join( + component.text for component in reply.chain if isinstance(component, Plain) + ) + assert "the old message text" in plain_text + assert "URL:" not in plain_text From f790a032d118390f3dc2e24c3e009f7e05fcfe70 Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:11:23 +0800 Subject: [PATCH 2/2] fix(qqofficial): preserve ambiguous quoted attachment text --- .../qqofficial/qqofficial_platform_adapter.py | 45 +---------------- tests/test_qqofficial_group_message_create.py | 49 ++++++++++--------- 2 files changed, 26 insertions(+), 68 deletions(-) diff --git a/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py b/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py index 00d0b530fb8..5ce27c1ff4d 100644 --- a/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py +++ b/astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py @@ -4,7 +4,6 @@ import logging import os import random -import re import time from pathlib import Path from types import SimpleNamespace @@ -717,32 +716,6 @@ async def _append_attachments( else: msg.append(File(name=filename, file=url, url=url)) - #: QQ renders a quoted attachment into the quoted text as - #: ``[附件N] 类型:图片 ... URL:`` and leaves ``attachments`` empty, so the - #: image would otherwise reach plugins as unstructured prose. - _FLATTENED_IMAGE_LINE_RE = re.compile( - r"^\[附件\d+\]\s*类型:图片\b[^\n]*?URL:(\S+)[^\n]*\n?", - re.MULTILINE, - ) - - @staticmethod - def _extract_flattened_quoted_images(content: str) -> tuple[str, list[str]]: - """Split flattened quoted image lines back out of pre-rendered text. - - Returns the remaining text and the image URLs in document order. Only - image attachments are lifted; other attachment kinds stay as text. - """ - urls: list[str] = [] - - def _collect(match: re.Match[str]) -> str: - urls.append(match.group(1)) - return "" - - return QQOfficialPlatformAdapter._FLATTENED_IMAGE_LINE_RE.sub( - _collect, - content, - ).strip(), urls - @staticmethod def _parse_face_message(content: str) -> str: """Parse QQ official face message format and convert to readable text. @@ -863,30 +836,14 @@ async def _parse_from_qqofficial( quoted_message_str = QQOfficialPlatformAdapter._parse_face_message( str(quoted_content or "").strip() ) - quoted_message_str, flattened_image_urls = ( - QQOfficialPlatformAdapter._extract_flattened_quoted_images( - quoted_message_str - ) - ) + # Content alone cannot distinguish attachment metadata from quoted prose. if quoted_message_str: quoted_chain.append(Plain(quoted_message_str)) - has_attachments = isinstance(quoted_attachments, list) and bool( - quoted_attachments - ) if isinstance(quoted_attachments, list): await QQOfficialPlatformAdapter._append_attachments( quoted_chain, quoted_attachments, ) - if not has_attachments: - for image_url in flattened_image_urls: - quoted_chain.append( - Image.fromURL( - QQOfficialPlatformAdapter._normalize_attachment_url( - image_url - ) - ) - ) # QQ tags a passive reply with message_type 103 and stuffs the previous # message into msg_elements even when the user quoted nothing, so a # message id is the only thing that marks a real quote. Building a Reply diff --git a/tests/test_qqofficial_group_message_create.py b/tests/test_qqofficial_group_message_create.py index 659c6a3f99a..af0696acf73 100644 --- a/tests/test_qqofficial_group_message_create.py +++ b/tests/test_qqofficial_group_message_create.py @@ -1192,28 +1192,30 @@ async def test_quoted_element_id_still_builds_reply_without_reference(): assert reply.message_str == "quoted text" +@pytest.mark.parametrize( + "quoted_content", + [ + "[附件1] 类型:图片 URL:https://multimedia.example.com/chart.png", + ( + "=== 消息 1 ===\n" + "[消息类型] 引用消息\n" + "[附件1] 类型:图片 文件名:chart.png 尺寸:2117x287 大小:57.3KB " + "URL:https://multimedia.example.com/chart.png\n" + "\n" + "=== 消息 2 ===\n" + "[消息内容] the old message text" + ), + ], +) @pytest.mark.asyncio -async def test_quoted_images_flattened_into_text_are_recovered_as_images(): - """QQ pre-renders quoted attachments into `content`, leaving `attachments` empty.""" +async def test_quoted_attachment_like_text_stays_literal(quoted_content: str): + """Text without structured attachment provenance must not become an image.""" _, message = _dispatch_group_message( _make_group_payload( content="answer", message_type=103, message_reference={"message_id": "quoted-1"}, - msg_elements=[ - { - "content": ( - "=== 消息 1 ===\n" - "[消息类型] 引用消息\n" - "[附件1] 类型:图片 文件名:chart.png 尺寸:2117x287 大小:57.3KB " - "URL:https://multimedia.example.com/chart.png\n" - "\n" - "=== 消息 2 ===\n" - "[消息内容] the old message text" - ), - "attachments": [], - } - ], + msg_elements=[{"content": quoted_content, "attachments": []}], ) ) @@ -1225,12 +1227,11 @@ async def test_quoted_images_flattened_into_text_are_recovered_as_images(): reply = abm.message[0] assert isinstance(reply, Reply) assert reply.id == "quoted-1" - images = [component for component in reply.chain if isinstance(component, Image)] - assert [component.file for component in images] == [ - "https://multimedia.example.com/chart.png" - ] - plain_text = "".join( - component.text for component in reply.chain if isinstance(component, Plain) + assert reply.message_str == quoted_content + assert not any(isinstance(component, Image) for component in reply.chain) + assert ( + "".join( + component.text for component in reply.chain if isinstance(component, Plain) + ) + == quoted_content ) - assert "the old message text" in plain_text - assert "URL:" not in plain_text