Skip to content

fix: split long telegram final segments#7432

Merged
Soulter merged 5 commits intoAstrBotDevs:masterfrom
Shujakuinkuraudo:fix/telegram-final-segment-splitting
Apr 10, 2026
Merged

fix: split long telegram final segments#7432
Soulter merged 5 commits intoAstrBotDevs:masterfrom
Shujakuinkuraudo:fix/telegram-final-segment-splitting

Conversation

@Shujakuinkuraudo
Copy link
Copy Markdown
Contributor

@Shujakuinkuraudo Shujakuinkuraudo commented Apr 9, 2026

Summary

  • add a shared helper to split Telegram text by the 4096-character limit before sending
  • reuse the helper for regular plain text sends and streaming final segments
  • add regression tests for both Markdown and plain-text fallback paths

Problem

Telegram streaming replies could fail with Message is too long when the final accumulated segment exceeded Telegram's single-message limit.

The regular plain-text send path already split long messages, but _send_final_segment() still tried to send the whole final reply as one message. That made long streaming replies fail even though the adapter already had splitting logic available.

Testing

  • ruff check astrbot/core/platform/sources/telegram/tg_event.py tests/test_telegram_adapter.py
  • ruff format --check astrbot/core/platform/sources/telegram/tg_event.py tests/test_telegram_adapter.py
  • pytest tests/test_telegram_adapter.py -q

Summary by Sourcery

Ensure Telegram text messages, including streaming final replies, are split according to Telegram’s length limit using a shared sending helper.

Bug Fixes:

  • Prevent Telegram streaming final segments from failing when the accumulated reply exceeds the single-message length limit by splitting into multiple messages.

Enhancements:

  • Introduce a shared helper to send long Telegram text messages with MarkdownV2 formatting and plain-text fallback, reused by both regular sends and streaming final segments.

Tests:

  • Add regression tests to verify long Markdown-formatted final segments are split correctly and that plain-text fallback also splits correctly when Markdown conversion fails.

@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 Apr 9, 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:

  • The test helpers _load_telegram_adapter and _load_telegram_platform_event duplicate the same mocked module setup; consider extracting a shared helper to build the patched sys.modules dict so future updates to the mock set don’t need to be made in multiple places.
  • In test_telegram_final_segment_splits_long_plaintext_when_markdown_fails, reaching into event._send_final_segment.__func__.__globals__ to get telegramify_markdown is quite brittle; it would be more robust to patch the module at its import path (e.g., astrbot.core.platform.sources.telegram.tg_event.telegramify_markdown) so refactors to the method signature or scoping don’t silently break the test.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The test helpers `_load_telegram_adapter` and `_load_telegram_platform_event` duplicate the same mocked module setup; consider extracting a shared helper to build the patched `sys.modules` dict so future updates to the mock set don’t need to be made in multiple places.
- In `test_telegram_final_segment_splits_long_plaintext_when_markdown_fails`, reaching into `event._send_final_segment.__func__.__globals__` to get `telegramify_markdown` is quite brittle; it would be more robust to patch the module at its import path (e.g., `astrbot.core.platform.sources.telegram.tg_event.telegramify_markdown`) so refactors to the method signature or scoping don’t silently break the test.

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 helper method, _send_text_chunks, in astrbot/core/platform/sources/telegram/tg_event.py to centralize the logic for sending potentially long Telegram messages. This method handles text splitting, MarkdownV2 conversion, and gracefully falls back to plain text if Markdown conversion or sending fails. The existing send_with_client and _send_final_segment methods are refactored to utilize this new helper, reducing code duplication. Additionally, new tests have been added to tests/test_telegram_adapter.py to verify the message splitting and fallback behavior. A review comment suggests improving the exception handling in _send_text_chunks by catching more specific exceptions like ValueError and BadRequest instead of a broad Exception.

Shujakuinkuraudo and others added 2 commits April 9, 2026 17:32
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 Apr 10, 2026
@Soulter Soulter merged commit 9c14a50 into AstrBotDevs:master Apr 10, 2026
1 check passed
foreveruand added a commit to foreveruand/AstrBot that referenced this pull request Apr 11, 2026
…ng origin patches

Upstream changes integrated:
- feat(discord): add configurable bot message filtering (AstrBotDevs#6505)
- docs: fix path concatenation error in storage.md (AstrBotDevs#7448)
- chore: remove lxml and bs4 deps (AstrBotDevs#7449)
- fix: make desktop plugin dependency loading safer on Windows (AstrBotDevs#7446)
- fix: split long telegram final segments (AstrBotDevs#7432) - adopted _send_text_chunks method
- perf: merge 3 cron tools into 1 cron manage tool with edit capability (AstrBotDevs#7445)
- chore: update logo in README.md

Origin patches preserved:
- Telegram enhancements: multi-image/video, spoiler, caption, reply_markup support
- Telegram inline query and callback query event handlers
- Telegram streaming improvements with draft API
- Gemini provider fixes and enhancements
- WeChat session renewal fix
- Shell timeout parameter support

Conflicts resolved in tg_event.py: adopted upstream's _send_text_chunks method
while preserving origin's advanced message handling features.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Shujakuinkuraudo Shujakuinkuraudo deleted the fix/telegram-final-segment-splitting branch April 11, 2026 20:58
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.

2 participants