From c930aa37130f31057d147007deb853bea9f21f40 Mon Sep 17 00:00:00 2001 From: axelray-dev <110029405+axelray-dev@users.noreply.github.com> Date: Wed, 24 Jun 2026 15:28:03 +0800 Subject: [PATCH] fix: add rich_messages config to disable sendRichMessage for Telegram Web (#4488) --- nanobot/channels/telegram.py | 6 +++++- tests/channels/test_telegram_channel.py | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/nanobot/channels/telegram.py b/nanobot/channels/telegram.py index 8ce7641a..743f7b24 100644 --- a/nanobot/channels/telegram.py +++ b/nanobot/channels/telegram.py @@ -351,6 +351,9 @@ class TelegramConfig(Base): streaming: bool = True # Enable inline keyboard buttons in Telegram messages. inline_keyboards: bool = False + # Use Bot API 10.1 sendRichMessage for markdown rendering. + # Disable if Telegram Web shows "message not supported" errors. + rich_messages: bool = True stream_edit_interval: float = Field(default=_STREAM_EDIT_INTERVAL_DEFAULT, ge=0.1) webhook_url: str = "" webhook_listen_host: str = "127.0.0.1" @@ -803,6 +806,7 @@ class TelegramChannel(BaseChannel): # latches off permanently if the server doesn't support it. if ( not render_as_blockquote + and self.config.rich_messages and not getattr(self, "_rich_send_disabled", False) ): rich_ok = await self._try_send_rich( @@ -911,7 +915,7 @@ class TelegramChannel(BaseChannel): # Skip when a streaming preview already exists to avoid the # delete-and-resend pattern that causes flickering and drops # line breaks (issue #4470). - if not buf.message_id and not getattr(self, "_rich_send_disabled", False): + if not buf.message_id and self.config.rich_messages and not getattr(self, "_rich_send_disabled", False): reply_params = None if reply_to_message_id := meta.get("message_id"): reply_params = {"message_id": int(reply_to_message_id), "allow_sending_without_reply": True} diff --git a/tests/channels/test_telegram_channel.py b/tests/channels/test_telegram_channel.py index 90b1e7e4..2d3f5c57 100644 --- a/tests/channels/test_telegram_channel.py +++ b/tests/channels/test_telegram_channel.py @@ -505,6 +505,23 @@ async def test_send_rich_bad_request_does_not_latch_capability() -> None: assert len(channel._app.bot.sent_messages) == 1 +@pytest.mark.asyncio +async def test_rich_messages_config_disabled_skips_sendRichMessage() -> None: + """When rich_messages=False, sendRichMessage should not be called.""" + channel = TelegramChannel( + TelegramConfig(enabled=True, token="123:abc", allow_from=["*"], rich_messages=False), + MessageBus(), + ) + channel._app = _FakeApp(lambda: None) + channel._app.bot.do_api_request = AsyncMock() + + await channel.send(OutboundMessage(channel="telegram", chat_id="123", content="**hello**")) + + channel._app.bot.do_api_request.assert_not_called() + assert len(channel._app.bot.sent_messages) == 1 + assert channel._app.bot.sent_messages[0]["text"] + + @pytest.mark.asyncio async def test_on_error_logs_network_issues_as_warning(monkeypatch) -> None: from telegram.error import NetworkError