Rebase onto upstream (a4d95fd)
#12
@@ -186,6 +186,11 @@ class TelegramChannel(BaseChannel):
|
||||
logger.warning("Telegram bot not running")
|
||||
return
|
||||
|
||||
# Check for suppression
|
||||
if msg.metadata.get("suppressed", False):
|
||||
logger.debug(f"Suppressed output (not sent to Telegram): {msg.content[:100]}...")
|
||||
return # Don't send to Telegram API
|
||||
|
||||
# Stop typing indicator for this chat
|
||||
self._stop_typing(msg.chat_id)
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
# tests/test_telegram_suppress.py
|
||||
import pytest
|
||||
from nanobot.bus.events import OutboundMessage
|
||||
from nanobot.channels.telegram import TelegramChannel
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_suppressed_message_not_sent():
|
||||
"""Test that messages with suppressed=True metadata are not sent to Telegram API."""
|
||||
|
||||
config = MagicMock()
|
||||
config.token = "test-token"
|
||||
bus = MagicMock()
|
||||
|
||||
channel = TelegramChannel(config, bus)
|
||||
|
||||
# Mock the internal _app and bot directly (skip start())
|
||||
mock_app = MagicMock()
|
||||
mock_bot = AsyncMock()
|
||||
mock_app.bot = mock_bot
|
||||
channel._app = mock_app
|
||||
|
||||
# Send a suppressed message
|
||||
msg = OutboundMessage(
|
||||
channel="telegram",
|
||||
chat_id="12345",
|
||||
content="[HIDDEN] This should not be sent",
|
||||
metadata={"suppressed": True}
|
||||
)
|
||||
|
||||
await channel.send(msg)
|
||||
|
||||
# Verify bot.send_message was NOT called
|
||||
mock_bot.send_message.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_normal_message_sent():
|
||||
"""Test that normal messages are sent to Telegram API."""
|
||||
|
||||
config = MagicMock()
|
||||
config.token = "test-token"
|
||||
bus = MagicMock()
|
||||
|
||||
channel = TelegramChannel(config, bus)
|
||||
|
||||
# Mock the internal _app and bot directly (skip start())
|
||||
mock_app = MagicMock()
|
||||
mock_bot = AsyncMock()
|
||||
mock_app.bot = mock_bot
|
||||
channel._app = mock_app
|
||||
|
||||
# Send a normal message
|
||||
msg = OutboundMessage(
|
||||
channel="telegram",
|
||||
chat_id="12345",
|
||||
content="Normal message",
|
||||
metadata={}
|
||||
)
|
||||
|
||||
await channel.send(msg)
|
||||
|
||||
# Verify bot.send_message WAS called
|
||||
mock_bot.send_message.assert_called_once()
|
||||
Reference in New Issue
Block a user