diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index 35f9ad620..07ea7cb05 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -558,8 +558,10 @@ class TelegramChannel(BaseChannel): await self._remove_reaction(chat_id, int(reply_to_message_id)) except ValueError: pass + 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, @@ -575,15 +577,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 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(