From e8e85cd1bcac8b3bceca3b13d9468c8886decc28 Mon Sep 17 00:00:00 2001 From: Michael-lhh Date: Thu, 26 Mar 2026 22:38:40 +0800 Subject: [PATCH 1/2] fix(telegram): split oversized final streamed replies Prevent Telegram Message_too_long failures on stream finalization by editing only the first chunk and sending overflow chunks as follow-up messages. Made-with: Cursor --- nanobot/channels/telegram.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index feb908657..49d9cf257 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -498,8 +498,10 @@ class TelegramChannel(BaseChannel): if stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id: return self._stop_typing(chat_id) + chunks = split_message(buf.text, TELEGRAM_MAX_MESSAGE_LEN) + primary_text = chunks[0] if chunks else buf.text try: - html = _markdown_to_telegram_html(buf.text) + html = _markdown_to_telegram_html(primary_text) await self._call_with_retry( self._app.bot.edit_message_text, chat_id=int_chat_id, message_id=buf.message_id, @@ -515,15 +517,18 @@ class TelegramChannel(BaseChannel): await self._call_with_retry( self._app.bot.edit_message_text, chat_id=int_chat_id, message_id=buf.message_id, - text=buf.text, + text=primary_text, ) except Exception as e2: if self._is_not_modified_error(e2): logger.debug("Final stream plain edit already applied for {}", chat_id) - self._stream_bufs.pop(chat_id, None) - return - logger.warning("Final stream edit failed: {}", e2) - raise # Let ChannelManager handle retry + else: + logger.warning("Final stream edit failed: {}", e2) + raise # Let ChannelManager handle retry + # If final content exceeds Telegram limit, keep the first chunk in + # the edited stream message and send the rest as follow-up messages. + for extra_chunk in chunks[1:]: + await self._send_text(int_chat_id, extra_chunk) self._stream_bufs.pop(chat_id, None) return From bdec2637aefc3042f234bd3dda9f4abee2869f32 Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Mon, 6 Apr 2026 06:39:23 +0000 Subject: [PATCH 2/2] test: add regression test for oversized stream-end splitting Made-with: Cursor --- tests/channels/test_telegram_channel.py | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/channels/test_telegram_channel.py b/tests/channels/test_telegram_channel.py index 1f25dcfa7..7bc212804 100644 --- a/tests/channels/test_telegram_channel.py +++ b/tests/channels/test_telegram_channel.py @@ -385,6 +385,32 @@ async def test_send_delta_stream_end_treats_not_modified_as_success() -> None: assert "123" not in channel._stream_bufs +@pytest.mark.asyncio +async def test_send_delta_stream_end_splits_oversized_reply() -> None: + """Final streamed reply exceeding Telegram limit is split into chunks.""" + from nanobot.channels.telegram import TELEGRAM_MAX_MESSAGE_LEN + + channel = TelegramChannel( + TelegramConfig(enabled=True, token="123:abc", allow_from=["*"]), + MessageBus(), + ) + channel._app = _FakeApp(lambda: None) + channel._app.bot.edit_message_text = AsyncMock() + channel._app.bot.send_message = AsyncMock(return_value=SimpleNamespace(message_id=99)) + + oversized = "x" * (TELEGRAM_MAX_MESSAGE_LEN + 500) + channel._stream_bufs["123"] = _StreamBuf(text=oversized, message_id=7, last_edit=0.0) + + await channel.send_delta("123", "", {"_stream_end": True}) + + channel._app.bot.edit_message_text.assert_called_once() + edit_text = channel._app.bot.edit_message_text.call_args.kwargs.get("text", "") + assert len(edit_text) <= TELEGRAM_MAX_MESSAGE_LEN + + channel._app.bot.send_message.assert_called_once() + assert "123" not in channel._stream_bufs + + @pytest.mark.asyncio async def test_send_delta_new_stream_id_replaces_stale_buffer() -> None: channel = TelegramChannel(