fix(qqofficial): render markdown for proactive send_by_session messages - #9914
fix(qqofficial): render markdown for proactive send_by_session messages#9914OMSociety wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py" line_range="400-404" />
<code_context>
return
- payload: dict[str, Any] = {"content": plain_text}
+ use_md = getattr(message_chain, "use_markdown_", None)
+ if use_md is False:
+ payload: dict[str, Any] = {"content": plain_text}
+ else:
+ payload = {
+ "markdown": MarkdownPayload(content=plain_text) if plain_text else None,
+ "msg_type": 2,
</code_context>
<issue_to_address>
**issue (bug_risk):** When a `MessageChain` explicitly sets `use_markdown_=False` and contains text separated by media, `_split_message_chain_by_media()` creates new `MessageChain` objects without copying `use_markdown_`. The recursive calls therefore see `use_md` as `None` and send the text-only chunks as Markdown instead of plain `content`.
**Triggers:** When `send_by_session()` receives a mixed text/media message with Markdown explicitly disabled.
**Suggested fix:** Preserve `use_markdown_` when constructing split message chains, or derive each split chain from the original chain so its metadata is retained.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and the change alters proactive outbound message payloads, replacing plain content with Markdown and msg_type=2 for text sends while changing media payload handling. If the API payload or Markdown interpretation is wrong, messages may fail or be delivered incorrectly; reverting prevents future occurrences but cannot undo messages already sent outside the team.
Blocking findings: astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py:404
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
@sourcery-ai 已按要求修复:split_message_chain_by_media() 改用 message.derive() 保留 use_markdown 等全部元数据,并新增回归测试 test_split_message_chain_by_media_preserves_use_markdown。验证:tests/test_qqofficial_*.py → 44 passed;ruff format / ruff check 通过。请复审。 |
|
Sure! I'm generating a new review now. |
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py" line_range="404-406" />
<code_context>
+ if use_md is False:
+ payload: dict[str, Any] = {"content": plain_text}
+ else:
+ payload = {
+ "markdown": MarkdownPayload(content=plain_text) if plain_text else None,
+ "msg_type": 2,
+ }
if msg_id and not allow_group_proactive_send:
</code_context>
<issue_to_address>
**issue (bug_risk):** The new default Markdown payload is sent directly through `post_group_message` and the C2C posting path without `_send_with_markdown_fallback`. When QQ rejects a Markdown payload because Markdown is unavailable or the payload is invalid for that destination, the exception propagates and the proactive message is not retried as plain `content`, whereas the normal reply path performs that fallback.
**Triggers:** When a proactive group or friend message receives a QQ API error for its Markdown payload.
**Suggested fix:** Route Markdown sends through the same `_send_with_markdown_fallback` helper used by `_post_send_one()`, including a retry with plain `content`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the Markdown payload or message-type selection is wrong, proactive messages can be delivered with incorrect content or formatting to users, and those already-sent messages cannot be undone by reverting the change. The behavior is otherwise bounded and future sends return to the previous implementation after a rollback.
Blocking findings: astrbot/core/platform/sources/qqofficial/qqofficial_platform_adapter.py:406
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Hey @OMSociety, I've posted a new review for you! |
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Sourcery assessment
Needs a human reviewer. The change makes proactive messages use QQ markdown payloads by default, so an incorrect API assumption or fallback could deliver malformed or unexpectedly formatted messages to users. Reverting stops future sends but cannot undo messages already delivered.
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
…end_by_session messages
Closes #7848
使通过
send_by_session()发送的主动/定时消息,在 QQ 官方平台上能够正确渲染为 Markdown。Modifications / 改动点
QQOfficialPlatformAdapter._send_by_session_common()与正常回复路径(QQOfficialMessageEvent._post_send_one())对齐,使主动推送消息(定时任务、send_message_to_user工具等)改用原生 Markdown payload(markdown+msg_type: 2)发送,而不是原来的纯{"content": ...}。MessageChain.use_markdown_:当它显式为False时,仍用纯文本content发送,保留关闭 Markdown 的用户的原有行为。msg_type: 7)会移除markdown字段并回退到content,与_post_send_one()一致。msg_type字段(频道 API 不接受该字段)。MarkdownPayload导入。动机:通过
send_by_session()发送的主动/定时消息,在 QQ 官方平台上一直以纯文本渲染;而普通对话回复却能正确渲染 Markdown(见 #7848)。QQOfficialPlatformAdapter._send_by_session_common()with the normal reply path (QQOfficialMessageEvent._post_send_one()), so proactive messages (cron jobs, thesend_message_to_usertool, etc.) are now sent with the native markdown payload (markdown+msg_type: 2) instead of plain{"content": ...}.MessageChain.use_markdown_: when it is explicitlyFalse, the message is still sent as plaincontent, preserving the previous behavior for users who disabled markdown.msg_type: 7) drop themarkdownfield and fall back tocontent, matching_post_send_one().msg_typefield (the guild API does not accept it).MarkdownPayloadimport.Motivation: proactive/scheduled messages sent via
send_by_session()were always rendered as plain text on QQ Official, while normal chat replies rendered markdown correctly (see #7848).Screenshots or Test Results / 运行截图或测试结果
改动前测试对话:



改动后测试对话:
日志:
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。这是 [Bug] QQ Official: cron/scheduled messages sent via send_by_session lack markdown rendering (msg_type=2) #7848 讨论过的 bug 修复。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Fix QQ Official session-based messages so they render Markdown correctly while preserving compatible plain-text and media sending behavior.
Bug Fixes:
Enhancements:
Tests: