Skip to content

fix(telegram): preserve attachment captions#7020

Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom
Clhikari:fix(telegram)--propagate-document-and-video-captions
Mar 27, 2026
Merged

fix(telegram): preserve attachment captions#7020
Soulter merged 2 commits intoAstrBotDevs:masterfrom
Clhikari:fix(telegram)--propagate-document-and-video-captions

Conversation

@Clhikari
Copy link
Copy Markdown
Contributor

@Clhikari Clhikari commented Mar 26, 2026

Summary

这次 PR 修复了 Telegram 附件消息 caption 处理不一致的问题。

之前 photo 会保留 caption,但 documentvideo 不会,导致用户在 Telegram 中给文件或视频填写的说明文字不会进入 message_strComp.Plain(...),上层依赖这些字段的逻辑拿不到用户说明。

Changes

  • 为 Telegram documentvideo 消息补上 caption 注入
  • 将 caption 写入 message.message_str
  • 追加对应的 Comp.Plain(...)
  • 保持 caption_entitiesmention 的处理行为一致
  • 新增文件 caption 和视频 caption 两条适配器测试
  • 补齐测试 mock 中缺失的 ContextTypes.DEFAULT_TYPE

Validation

已执行:

  • ruff check astrbot/core/platform/sources/telegram/tg_adapter.py tests/test_telegram_adapter.py tests/fixtures/mocks/telegram.py
  • python -m pytest tests/test_telegram_adapter.py -q

结果:

  • 2 passed

Scope

这次只改了:

  • astrbot/core/platform/sources/telegram/tg_adapter.py
  • tests/fixtures/mocks/telegram.py
  • tests/test_telegram_adapter.py

Closes #7000

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

Ensure Telegram adapter preserves and propagates captions for media messages while adding coverage for these behaviors.

Bug Fixes:

  • Preserve captions and mention entities for Telegram document and video messages in the converted message payload, aligning behavior with photos.

Enhancements:

  • Refactor caption handling in the Telegram adapter into a shared helper to keep media processing consistent.

Tests:

  • Add adapter tests verifying that Telegram document and video captions populate message text, plain components, and mention mentions correctly.
  • Extend Telegram mocks to provide ContextTypes.DEFAULT_TYPE for more accurate adapter testing.

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels 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:

  • Consider passing message and update as parameters into _apply_caption instead of relying on closure variables to make the helper side effects clearer and easier to reuse or refactor later.
  • In _apply_caption, caption_entities are processed even if caption is falsy; adding a guard so entities are only handled when message.message_str is set would avoid relying on Telegram always sending a caption alongside caption entities.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider passing `message` and `update` as parameters into `_apply_caption` instead of relying on closure variables to make the helper side effects clearer and easier to reuse or refactor later.
- In `_apply_caption`, `caption_entities` are processed even if `caption` is falsy; adding a guard so entities are only handled when `message.message_str` is set would avoid relying on Telegram always sending a caption alongside caption entities.

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 refactors the Telegram adapter to consistently handle captions and mentions across various media types, including photos, documents, and videos, by introducing a centralized _apply_caption helper function. It also includes updates to the test mocks and new unit tests for document and video captions. A review comment suggests improving the robustness of the caption entity processing by explicitly checking for the existence of the caption before slicing and using the original caption string for more reliable entity extraction.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 27, 2026
@Soulter Soulter merged commit a5e86c8 into AstrBotDevs:master Mar 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]Telegram 适配器对附件消息的 caption 处理不一致

2 participants