Skip to content

Comments

Add support for Json, Xml, and other components#5205

Closed
NanoRocky wants to merge 6 commits intoAstrBotDevs:masterfrom
NanoRocky:master
Closed

Add support for Json, Xml, and other components#5205
NanoRocky wants to merge 6 commits intoAstrBotDevs:masterfrom
NanoRocky:master

Conversation

@NanoRocky
Copy link
Contributor

@NanoRocky NanoRocky commented Feb 18, 2026

修复发送 JSON 消息时遇到 “消息为空,跳过发送阶段” 的问题。

Modifications / 改动点

  • /astrbot/core/pipeline/respond/stage.py : 补全 JSON 消息判断,并顺带补全其它缺失的消息类型判断。

  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

[2026-02-18 18:59:19.431] [Core] [INFO] [core.event_bus:59]: [default] [NapCat(aiocqhttp)] 皮皮喵酪灰/3525987739: 点歌1 [2026-02-18 18:59:19.432] [Core] [DBUG] [waking_check.stage:157]: enabled_plugins_name: ['*'] [2026-02-18 18:59:19.436] [Core] [DBUG] [method.star_request:44]: plugin -> session_controller - handle_session_control_agent [2026-02-18 18:59:19.436] [Core] [DBUG] [method.star_request:44]: plugin -> session_controller - handle_empty_mention [2026-02-18 18:59:19.436] [Core] [DBUG] [method.star_request:44]: plugin -> astrbot_plugin_meting - play_song_by_index [2026-02-18 18:59:19.718] [Plug] [INFO] [astrbot_plugin_meting.main:175]: 音乐卡片签名成功,发送卡片 [2026-02-18 18:59:19.719] [Plug] [INFO] [astrbot_plugin_meting.main:176]: 卡片数据: type=<ComponentType.Json: 'Json'> data={'app': 'com.tencent.music.lua', 'bizsrc': 'qqconnect.sdkshare_music', 'config': {'ctime': 1771411966, 'forward': 1, 'token': 'e79a964c581706afcbbfa0fb963e18eb', 'type': 'normal'}, 'extra': {'app_type': 1, 'appid': 100495085, 'uin': 3264925726}, 'meta': {'music': {'app_type': 1, 'appid': 100495085, 'ctime': 1771411966, 'desc': '小星星Aurora', 'jumpUrl': 'https://music.163.com/#/song?id=1351520305', 'musicUrl': 'https://metingapi.nanorocky.top/?server=netease&type=url&id=1351520305', 'preview': 'https://p3.music.126.net/-kDO5LiKki3bmeF21MaCuQ==/109951163917806959.jpg?param=320x320', 'tag': '网易云音乐', 'tagIcon': 'https://i.gtimg.cn/open/app_icon/00/49/50/85/100495085_100_m.png', 'title': '坠落星空', 'uin': 3264925726}}, 'prompt': '[分享]坠落星空', 'ver': '0.0.0.1', 'view': 'music'} [2026-02-18 18:59:19.719] [Core] [DBUG] [result_decorate.stage:165]: hook(on_decorating_result) -> meme_manager - on_decorating_result [2026-02-18 18:59:19.719] [Plug] [DBUG] [astrbot_plugin_meme_manager.main:766]: [meme_manager] on_decorating_result 开始处理 [2026-02-18 18:59:19.719] [Plug] [DBUG] [astrbot_plugin_meme_manager.main:914]: [meme_manager] on_decorating_result 处理完成 [2026-02-18 18:59:19.721] [Core] [INFO] [respond.stage:170]: Prepare to send - 皮皮喵酪灰/3525987739: [ComponentType.Json] [2026-02-18 18:59:19.844] [Core] [DBUG] [pipeline.context_utils:95]: hook(OnAfterMessageSentEvent) -> meme_manager - after_message_sent [2026-02-18 18:59:19.845] [Core] [DBUG] [pipeline.context_utils:95]: hook(OnAfterMessageSentEvent) -> astrbot - after_message_sent [2026-02-18 18:59:19.845] [Core] [DBUG] [method.star_request:44]: plugin -> meme_manager - handle_upload_image [2026-02-18 18:59:19.845] [Core] [DBUG] [method.star_request:44]: plugin -> astrbot - on_message [2026-02-18 18:59:19.845] [Core] [DBUG] [pipeline.scheduler:88]: pipeline 执行完毕。


Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ 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.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

A short story

Image_1771415347973_644

close #5198

Co-authored-by: Pizero zhaory200707@outlook.com

Copilot AI review requested due to automatic review settings February 18, 2026 12:10
@auto-assign auto-assign bot requested review from Fridemn and anka-afk February 18, 2026 12:10
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 18, 2026
@dosubot
Copy link

dosubot bot commented Feb 18, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
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 - 我发现了 1 个问题,并留下了一些整体性反馈:

  • 对于 Comp.Location,使用 bool(comp.lat and comp.lon) 会把 0 当作空值处理,但 (0, 0) 是一个合法坐标;建议改为显式检查 is not None,以避免在赤道/本初子午线上的位置被错误丢弃。
  • 对于 idtype 这类字段(例如 Comp.MusicComp.ForwardComp.Contact),简单使用真值判断会把 0 或空枚举错误地视为缺失;如果 0/假值是合法标识符,建议改用显式的 is not None 检查。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- For `Comp.Location`, using `bool(comp.lat and comp.lon)` will treat `0` as empty even though `(0, 0)` is a valid coordinate; consider checking explicitly for `is not None` instead to avoid dropping locations at the equator/prime-meridian.
- For fields like `id` and `type` (e.g., `Comp.Music`, `Comp.Forward`, `Comp.Contact`), using simple truthiness checks can incorrectly treat `0` or empty enums as missing; prefer explicit `is not None` checks if `0`/falsy values are valid identifiers.

## Individual Comments

### Comment 1
<location> `astrbot/core/pipeline/respond/stage.py:41` </location>
<code_context>
+        Comp.Share: lambda comp: bool(comp.url) or bool(comp.title),
+        Comp.Music: lambda comp: (comp.id and comp.type and comp.type != 'custom') or (comp.type == 'custom' and comp.url and comp.audio and comp.title), # 音乐分享
+        Comp.Forward: lambda comp: bool(comp.id), # 合并转发
+        Comp.Location: lambda comp: bool(comp.lat and comp.lon), # 位置
+        Comp.Contact: lambda comp: bool(comp.type and comp.id), # 推荐好友 or 群
+        Comp.Shake: lambda _: True, # 窗口抖动(戳一戳)
</code_context>

<issue_to_address>
**issue (bug_risk):** 使用真值判断 lat/lon 会把 0 当成非法值,很可能导致合法坐标被判定为无效。

`bool(comp.lat and comp.lon)` 在任一值为 `0`/`0.0`(赤道、本初子午线)时,会对合法坐标返回 `False`,从而将其标记为无效。如果允许 `0`,请改用显式的 `None` 检查,例如 `comp.lat is not None and comp.lon is not None`,或使用其他基于哨兵值的方式。
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得这些 Review 有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进 Review 质量。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • For Comp.Location, using bool(comp.lat and comp.lon) will treat 0 as empty even though (0, 0) is a valid coordinate; consider checking explicitly for is not None instead to avoid dropping locations at the equator/prime-meridian.
  • For fields like id and type (e.g., Comp.Music, Comp.Forward, Comp.Contact), using simple truthiness checks can incorrectly treat 0 or empty enums as missing; prefer explicit is not None checks if 0/falsy values are valid identifiers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- For `Comp.Location`, using `bool(comp.lat and comp.lon)` will treat `0` as empty even though `(0, 0)` is a valid coordinate; consider checking explicitly for `is not None` instead to avoid dropping locations at the equator/prime-meridian.
- For fields like `id` and `type` (e.g., `Comp.Music`, `Comp.Forward`, `Comp.Contact`), using simple truthiness checks can incorrectly treat `0` or empty enums as missing; prefer explicit `is not None` checks if `0`/falsy values are valid identifiers.

## Individual Comments

### Comment 1
<location> `astrbot/core/pipeline/respond/stage.py:41` </location>
<code_context>
+        Comp.Share: lambda comp: bool(comp.url) or bool(comp.title),
+        Comp.Music: lambda comp: (comp.id and comp.type and comp.type != 'custom') or (comp.type == 'custom' and comp.url and comp.audio and comp.title), # 音乐分享
+        Comp.Forward: lambda comp: bool(comp.id), # 合并转发
+        Comp.Location: lambda comp: bool(comp.lat and comp.lon), # 位置
+        Comp.Contact: lambda comp: bool(comp.type and comp.id), # 推荐好友 or 群
+        Comp.Shake: lambda _: True, # 窗口抖动(戳一戳)
</code_context>

<issue_to_address>
**issue (bug_risk):** Using truthiness for lat/lon will treat 0 as invalid, which likely breaks valid coordinates.

`bool(comp.lat and comp.lon)` will evaluate to `False` for valid coordinates where either value is `0`/`0.0` (equator, prime meridian), marking them invalid. If `0` is allowed, use explicit `None` checks instead, e.g. `comp.lat is not None and comp.lon is not None`, or another sentinel-based approach.
</issue_to_address>

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.

@dosubot dosubot bot added the area:core The bug / feature is about astrbot's core, backend label Feb 18, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix issue #5198 where JSON messages were being skipped with a "消息为空,跳过发送阶段" error. The root cause was that the _component_validators dictionary in RespondStage was missing validators for several component types, causing those components to be treated as empty and skipped during the send stage.

Changes:

  • Added 12 new component type validators to the _component_validators dictionary in RespondStage
  • Validators added for: Json, Xml, Share, Music, Forward, Location, Contact, Shake, Anonymous, Dice, Rps, and Unknown component types

NanoRocky and others added 4 commits February 18, 2026 20:20
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@NanoRocky NanoRocky closed this Feb 18, 2026
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 size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 发不出 Json 消息

1 participant