fix: split long telegram final segments#7432
Conversation
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The test helpers
_load_telegram_adapterand_load_telegram_platform_eventduplicate the same mocked module setup; consider extracting a shared helper to build the patchedsys.modulesdict 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 intoevent._send_final_segment.__func__.__globals__to gettelegramify_markdownis 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.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
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.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…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>
Summary
Problem
Telegram streaming replies could fail with
Message is too longwhen 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.pyruff format --check astrbot/core/platform/sources/telegram/tg_event.py tests/test_telegram_adapter.pypytest tests/test_telegram_adapter.py -qSummary 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:
Enhancements:
Tests: