fix: add rich_messages config to disable sendRichMessage for Telegram Web (#4488)
This commit is contained in:
@@ -351,6 +351,9 @@ class TelegramConfig(Base):
|
|||||||
streaming: bool = True
|
streaming: bool = True
|
||||||
# Enable inline keyboard buttons in Telegram messages.
|
# Enable inline keyboard buttons in Telegram messages.
|
||||||
inline_keyboards: bool = False
|
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)
|
stream_edit_interval: float = Field(default=_STREAM_EDIT_INTERVAL_DEFAULT, ge=0.1)
|
||||||
webhook_url: str = ""
|
webhook_url: str = ""
|
||||||
webhook_listen_host: str = "127.0.0.1"
|
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.
|
# latches off permanently if the server doesn't support it.
|
||||||
if (
|
if (
|
||||||
not render_as_blockquote
|
not render_as_blockquote
|
||||||
|
and self.config.rich_messages
|
||||||
and not getattr(self, "_rich_send_disabled", False)
|
and not getattr(self, "_rich_send_disabled", False)
|
||||||
):
|
):
|
||||||
rich_ok = await self._try_send_rich(
|
rich_ok = await self._try_send_rich(
|
||||||
@@ -911,7 +915,7 @@ class TelegramChannel(BaseChannel):
|
|||||||
# Skip when a streaming preview already exists to avoid the
|
# Skip when a streaming preview already exists to avoid the
|
||||||
# delete-and-resend pattern that causes flickering and drops
|
# delete-and-resend pattern that causes flickering and drops
|
||||||
# line breaks (issue #4470).
|
# 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
|
reply_params = None
|
||||||
if reply_to_message_id := meta.get("message_id"):
|
if reply_to_message_id := meta.get("message_id"):
|
||||||
reply_params = {"message_id": int(reply_to_message_id), "allow_sending_without_reply": True}
|
reply_params = {"message_id": int(reply_to_message_id), "allow_sending_without_reply": True}
|
||||||
|
|||||||
@@ -505,6 +505,23 @@ async def test_send_rich_bad_request_does_not_latch_capability() -> None:
|
|||||||
assert len(channel._app.bot.sent_messages) == 1
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_on_error_logs_network_issues_as_warning(monkeypatch) -> None:
|
async def test_on_error_logs_network_issues_as_warning(monkeypatch) -> None:
|
||||||
from telegram.error import NetworkError
|
from telegram.error import NetworkError
|
||||||
|
|||||||
Reference in New Issue
Block a user