Skip to content

feat(media): extend Telegram bot support to documents, audio, voice, and video#4

Open
Martins6 wants to merge 1 commit into
mainfrom
feat/extend-media-support
Open

feat(media): extend Telegram bot support to documents, audio, voice, and video#4
Martins6 wants to merge 1 commit into
mainfrom
feat/extend-media-support

Conversation

@Martins6

Copy link
Copy Markdown
Owner

Summary

Previously DefaultHandler in internal/bot/handlers.go only processed photo attachments; documents, audio files, voice messages, and videos were silently dropped even though their media types were already detected. This PR routes all five attachment types through a single media-processing path that mirrors the existing photo schema (download from Telegram → save under <workspace>/downloads/<type>/<UTC-timestamped-filename> → forward a prompt that references the local file to OpenCode).

Changes

  • internal/media/downloader.go

    • Add MediaMetadata struct (Kind, FileSize, MimeType, OriginalName).

    • Rewrite BuildPrompt(path, meta, caption) to emit a metadata-rich prompt:

      File located at: <abs-path>
      File type: <photo|document|audio|voice|video>
      File size: <bytes> bytes
      Original name: <name>      ← only when Telegram provides FileName
      MIME type: <mime>          ← only when Telegram provides MimeType
      
      User message: <caption>
      
    • Add ExtractFileRef(message) returning (fileID, fileSize, mimeType, originalName, mediaType, ext, ok) for any recognized attachment.

  • internal/bot/handlers.go

    • Extract the photo-only branch into a private processMediaAttachment helper that handles all five media types through one path.
    • Add mediaKindLabel for capitalized log labels per attachment kind.
    • Simplify the empty-message skip guard to rely on the resolved userMessage.
  • internal/media/downloader_test.go

    • Update 2 existing BuildPrompt tests for the new signature.
    • Add 11 new tests covering metadata-rich prompts (full / partial / zero-size) and ExtractFileRef for each media type plus the empty-message case.
  • docs/features/feat-telegram-bot.md — describe the new behavior and prompt format.

  • docs/features/LOG-FEATS.md — log entry.

  • docs/features/OVERVIEW.md — refresh the file description.

Behavior

  • Photos, documents, audio files, voice messages, and videos are all downloaded from Telegram and saved under <workspace>/downloads/<type>/<UTC-timestamped-filename>.
  • The prompt to OpenCode now includes file type, size, and (when Telegram provides them) the original filename and MIME type.
  • Photos gain a metadata block in their prompt; everything else about photo behavior is preserved.
  • Stickers, contacts, and locations remain unsupported.
  • No explicit size limits added; relies on Telegram Bot API's 20 MB cap and the existing 30 s HTTP timeout.

Verification

  • go build ./... clean
  • go vet ./... clean
  • go test ./... all packages green (20 tests in internal/media/...)

Test plan (requires a running bot + OpenCode CLI)

  1. Send a photo with caption → saved under downloads/images/, prompt contains File type: photo, File size: <N> bytes.
  2. Send a PDF document → saved under downloads/documents/, prompt contains Original name: …pdf, MIME type: application/pdf.
  3. Send a voice message → saved under downloads/audio/ with .ogg.
  4. Send an MP3 audio file → saved under downloads/audio/ with .mp3.
  5. Send a short video → saved under downloads/videos/ with .mp4.
  6. Regression — plain text message still works; empty/whitespace-only message still skipped.

…and video

Previously the bot's DefaultHandler only processed photos; documents, audio,
voice messages, and videos were silently dropped (their media types were
detected but never wired through). This change routes all five attachment
types through a single media-processing path.

- Add `media.MediaMetadata` struct and rewrite `BuildPrompt` to emit a
  metadata-rich prompt (file type, size, original name and MIME type when
  Telegram provides them).
- Add `media.ExtractFileRef` helper that pulls the Telegram FileID, size,
  MIME type, original filename, MediaType subdirectory, and extension out
  of any `*models.Message` carrying a recognized attachment.
- Refactor `internal/bot/handlers.go` DefaultHandler into a private
  `processMediaAttachment` helper that handles all five media types. The
  empty-message skip guard is simplified to rely on the resolved
  `userMessage` only.
- Stickers, contacts, and locations remain unsupported.
- Update `docs/features/feat-telegram-bot.md` to document the new
  behavior and prompt format; log entry in `LOG-FEATS.md`; refreshed
  `OVERVIEW.md` one-liner.
- Add 11 unit tests covering the new helpers and prompt format; existing
  BuildPrompt tests updated for the new signature. All media tests pass.

Verification:
- `go build ./...` clean
- `go vet ./...` clean
- `go test ./...` all packages green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant