Skip to content

fix(telegram): download reply-to-message photo for agent vision - #22251

Closed
alblez wants to merge 1 commit into
NousResearch:mainfrom
alblez:fix/reply-to-message-media-not-forwarded
Closed

fix(telegram): download reply-to-message photo for agent vision#22251
alblez wants to merge 1 commit into
NousResearch:mainfrom
alblez:fix/reply-to-message-media-not-forwarded

Conversation

@alblez

@alblez alblez commented May 9, 2026

Copy link
Copy Markdown
Contributor

Summary

When a user replies to a photo message with text (e.g., "What do you think about this?" as a reply to a screenshot), the agent could not see the referenced image. Only reply_to_text/caption was extracted — the photo itself was never downloaded or forwarded.

Fixes #22250

Changes

gateway/platforms/telegram.py

  • New method _fetch_reply_to_photo() — async helper that:

    1. Checks if message.reply_to_message has a photo
    2. Downloads the largest PhotoSize variant via get_file()
    3. Caches locally via cache_image_from_bytes()
    4. Attaches path to event.media_urls / event.media_types
    5. Sets reply_to_text = "[photo]" when no caption exists (so the disambiguation pointer in run.py still fires)
  • Called from _handle_text_message() — right after building the event, before enqueueing

tests/gateway/test_telegram_reply_to_photo.py

6 regression tests covering:

  • Photo download + attachment to event
  • Caption preservation (existing reply_to_text not overwritten)
  • No-op when replying to text-only message
  • No-op when message is not a reply
  • Graceful handling of download failures
  • PNG extension detection (iPhone screenshots)

How It Works

The fix reuses the existing image-routing pipeline in run.py — the replied-to photo flows through the same native/vision-analyze path as directly-sent photos. No changes needed to run.py or base.py.

How to Test

  1. Send a photo to a Telegram chat with the bot
  2. Reply to that photo with a text message asking about it
  3. The agent should now be able to see and discuss the photo

Platforms Tested

  • macOS (development) — unit tests pass
  • Telegram gateway (real-world reproduction)

When a user sends a text message replying to a photo message, the agent
could not see the referenced image because only reply_to_text/caption was
extracted — the photo itself was never downloaded.

Add _fetch_reply_to_photo() to the Telegram adapter that:
- Detects when reply_to_message contains a photo
- Downloads the largest PhotoSize variant via get_file()
- Caches locally via cache_image_from_bytes()
- Attaches path to event.media_urls/media_types for downstream routing
- Sets reply_to_text="[photo]" when no caption exists

The fix reuses the existing image-routing pipeline in run.py so the
replied-to photo flows through the same native/vision-analyze path as
directly-sent photos.

Fixes NousResearch#22250
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter tool/vision Vision analysis and image generation P2 Medium — degraded but workaround exists labels May 9, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the focused reproduction and regression coverage. This is an automated hermes-sweeper review; current main already implements the requested behavior through a broader reply-media fix.

  • efbe1635dd2ee544afb850a23e0939560e3e0418 (fix(gateway): include replied-to media attachments, fix(gateway): include replied-to media attachments #46107) added _cache_replied_media.
  • plugins/platforms/telegram/adapter.py:7505-7509 invokes it for text replies before the event is queued.
  • plugins/platforms/telegram/adapter.py:7217-7239 downloads, caches, and appends replied-to media to event.media_urls / event.media_types.
  • tests/gateway/test_telegram_group_gating.py:1148-1188 covers a text reply to a captionless PNG photo and verifies the cached image reaches the event.

The current implementation also handles additional replied-to media types, so this photo-only PR is redundant.

@teknium1 teknium1 closed this Jul 13, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter sweeper:implemented-on-main Sweeper: behavior already present on current main tool/vision Vision analysis and image generation type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(telegram): Reply-to-message photo not forwarded to agent

3 participants