Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions gateway/platforms/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4750,13 +4750,12 @@ def _enqueue_photo_event(self, batch_key: str, event: MessageEvent) -> None:

async def _handle_media_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle incoming media messages, downloading images to local cache."""
if not update.message:
msg = self._effective_update_message(update)
if not msg:
return
if not self._should_process_message(update.message):
if not self._should_process_message(msg):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this is salvaged onto current main, carry this normalized msg through the current observed-unmentioned-group branch too; that branch now still reads update.message, which is also None for channel_post updates.

return

msg = update.message


# Determine media type
if msg.sticker:
msg_type = MessageType.STICKER
Expand Down
38 changes: 38 additions & 0 deletions tests/gateway/test_telegram_channel_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,41 @@ async def test_command_handler_uses_effective_message_for_channel_post(telegram_
assert event.message_type == MessageType.COMMAND
assert event.source.chat_type == "channel"
assert event.source.chat_id == "-1003950368353"


@pytest.mark.asyncio
async def test_media_handler_uses_effective_message_for_channel_post(
telegram_adapter_cls,
monkeypatch,
):
adapter = _make_adapter(telegram_adapter_cls)
msg = _make_channel_message(text=None)
msg.caption = "channel photo"
file_obj = SimpleNamespace(
file_path="photos/channel.jpg",
download_as_bytearray=AsyncMock(return_value=bytearray(b"jpg-bytes")),
)
msg.photo = [SimpleNamespace(get_file=AsyncMock(return_value=file_obj))]
msg.video = None
msg.audio = None
msg.voice = None
msg.document = None
msg.sticker = None
update = _make_channel_update(msg)
adapter._enqueue_photo_event = MagicMock()
monkeypatch.setattr(
sys.modules["gateway.platforms.telegram"],
"cache_image_from_bytes",
lambda _data, ext=".jpg": f"/tmp/channel-post{ext}",
)

await adapter._handle_media_message(update, MagicMock())

adapter._enqueue_photo_event.assert_called_once()
event = adapter._enqueue_photo_event.call_args.args[1]
assert event.text == "channel photo"
assert event.message_type == MessageType.PHOTO
assert event.source.chat_type == "channel"
assert event.source.chat_id == "-1003950368353"
assert event.media_urls == ["/tmp/channel-post.jpg"]
assert event.media_types == ["image/jpg"]
Loading