Skip to content

feat: add message-level markdown control for QQ Official platform#6980

Open
KBVsent wants to merge 2 commits intoAstrBotDevs:masterfrom
KBVsent:feat/message-level-markdown-control
Open

feat: add message-level markdown control for QQ Official platform#6980
KBVsent wants to merge 2 commits intoAstrBotDevs:masterfrom
KBVsent:feat/message-level-markdown-control

Conversation

@KBVsent
Copy link
Copy Markdown
Contributor

@KBVsent KBVsent commented Mar 26, 2026

Related Issues: #5845
QQ Official platform currently always attempts to send messages as Markdown (msg_type=2) with an automatic fallback to plain text on rejection. There is no way for plugins to control this on a per-message basis. Some messages (e.g. structured data, plain notifications) should be sent as plain text directly without the Markdown attempt and fallback overhead. This change gives plugins explicit control over the sending mode, and the field is designed to be reusable by other Markdown-capable platforms in the future.

Modifications / 改动点

  • astrbot/core/message/message_event_result.py: Add use_markdown_: bool | None field and use_markdown() chainable method to MessageChain, following the same pattern as use_t2i_.
  • astrbot/core/platform/sources/qqofficial/qqofficial_message_event.py: Read send_buffer.use_markdown_ in _post_send() to construct plain text payload (msg_type=0, content) when False, otherwise preserve existing Markdown behavior.
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

event.plain_result(msg) event.plain_result(msg).use_markdown(False)
# Force plain text (skip Markdown)
yield event.make_result().message("plain text").use_markdown(False)

# Force Markdown (default behavior, explicit)
yield event.make_result().message("**bold**").use_markdown(True)

# Follow platform default (no change needed)
yield event.plain_result("default behavior")

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Add message-level control over Markdown usage and preserve message metadata across derived message chains.

New Features:

  • Introduce a use_markdown_ flag and chainable use_markdown() API on MessageChain to control Markdown vs plain-text sending per message.
  • Enable QQ Official messages to be sent either as Markdown or plain text based on the MessageChain configuration instead of always attempting Markdown first.

Enhancements:

  • Add a derive() helper on MessageChain to create new chains that inherit metadata such as use_t2i_, use_markdown_, and type.
  • Update response pipeline stages to build derived MessageChains instead of fresh ones so platform-related metadata is preserved when splitting or reusing chains.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the messaging capabilities for the QQ Official platform by providing granular control over message formatting. Previously, messages were always attempted as Markdown with an automatic fallback. This change allows plugins to explicitly dictate whether a message should be sent as plain text or Markdown, optimizing message delivery for various content types and reducing unnecessary overhead.

Highlights

  • Message-level Markdown Control: Introduced a new use_markdown_ field and a chainable use_markdown() method to the MessageChain class, allowing plugins to explicitly control whether a message should be sent as Markdown or plain text.
  • QQ Official Platform Integration: Implemented logic within the QQ Official platform's message sending mechanism to respect the use_markdown_ flag, enabling direct plain text sending (msg_type=0) when specified, bypassing the default Markdown attempt and fallback.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 26, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • In _post_send, consider avoiding getattr(self.send_buffer, "use_markdown_", None) and instead giving send_buffer a concrete type (or cast) that includes use_markdown_, so type checkers can catch misuse and refactors remain safer.
  • The hard-coded msg_type values 0 and 2 would be clearer and less error-prone if they were defined as named constants or an enum for QQ Official message types and reused here.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `_post_send`, consider avoiding `getattr(self.send_buffer, "use_markdown_", None)` and instead giving `send_buffer` a concrete type (or cast) that includes `use_markdown_`, so type checkers can catch misuse and refactors remain safer.
- The hard-coded `msg_type` values `0` and `2` would be clearer and less error-prone if they were defined as named constants or an enum for QQ Official message types and reused here.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new feature to control Markdown usage when sending messages. It adds a use_markdown_ attribute to the MessageChain class and a corresponding use_markdown method, allowing users to explicitly enable or disable Markdown for messages. The message sending logic in qqofficial_message_event.py has been updated to respect this new flag, enabling messages to be sent as either Markdown or plain text based on the use_markdown_ setting. Feedback includes a suggestion to remove unnecessary parentheses in the use_markdown_ attribute definition for better style consistency and a recommendation to refactor the if/else logic in qqofficial_message_event.py to reduce code duplication and improve maintainability.

@dosubot dosubot bot added area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Mar 26, 2026
@KBVsent KBVsent marked this pull request as draft March 26, 2026 04:43
@KBVsent KBVsent marked this pull request as ready for review March 26, 2026 05:17
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The new derive() helper on MessageChain manually copies a fixed set of attributes (use_t2i_, use_markdown_, type), which is easy to forget to update when new metadata fields are added; consider using dataclasses.replace or a more generic copy mechanism so future fields are automatically preserved.
  • In post_send for QQ Official, the logic assumes that the platform default is always Markdown (msg_type=2) when use_markdown is None; if you ever change the platform default behavior, you may want to centralize this decision (e.g., a helper on MessageChain or the platform class) so that the meaning of None stays consistent with the actual default.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new derive() helper on MessageChain manually copies a fixed set of attributes (use_t2i_, use_markdown_, type), which is easy to forget to update when new metadata fields are added; consider using dataclasses.replace or a more generic copy mechanism so future fields are automatically preserved.
- In _post_send for QQ Official, the logic assumes that the platform default is always Markdown (msg_type=2) when use_markdown_ is None; if you ever change the platform default behavior, you may want to centralize this decision (e.g., a helper on MessageChain or the platform class) so that the meaning of None stays consistent with the actual default.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant