Skip to content

Fix: Support media extraction from templateMessage in getBase64FromMediaMessage #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AlexisJusviack
Copy link

@AlexisJusviack AlexisJusviack commented Jul 12, 2025

Fix: Add support for templateMessage media in getBase64FromMediaMessage

What this does

Adds support to download media from templateMessage structures in getBase64FromMediaMessage, by checking for hydratedTemplate and hydratedFourRowTemplate.

Why it's needed

Currently, media inside templates (e.g. imageMessage, videoMessage, documentMessage) is not processed by the method, which leads to errors or media being skipped.

How it works

If a templateMessage is detected, the code looks into the inner hydrated template and assigns the correct mediaMessage and mediaType. Then it proceeds as usual with the download logic.

Example message

{
  "message": {
    "templateMessage": {
      "hydratedTemplate": {
        "imageMessage": {
          "mimetype": "image/jpeg",
          "fileLength": 123456,
          "url": "https://..."
        }
      }
    }
  }
}

## Summary by Sourcery

Support media extraction from templateMessage structures in getBase64FromMediaMessage

Bug Fixes:
- Process media inside hydratedTemplate and hydratedFourRowTemplate to correctly retrieve mediaMessage and mediaType from templateMessage

Enhancements:
- Throw an explicit error when a templateMessage lacks a supported media type

…diaMessage

### Fix: Add support for templateMessage media in getBase64FromMediaMessage

#### What this does
Adds support to download media from `templateMessage` structures in `getBase64FromMediaMessage`, by checking for `hydratedTemplate` and `hydratedFourRowTemplate`.

#### Why it's needed
Currently, media inside templates (e.g. `imageMessage`, `videoMessage`, `documentMessage`) is not processed by the method, which leads to errors or media being skipped.

#### How it works
If a `templateMessage` is detected, the code looks into the inner hydrated template and assigns the correct `mediaMessage` and `mediaType`. Then it proceeds as usual with the download logic.

#### Example message
```json
{
  "message": {
    "templateMessage": {
      "hydratedTemplate": {
        "imageMessage": {
          "mimetype": "image/jpeg",
          "fileLength": 123456,
          "url": "https://..."
        }
      }
    }
  }
}
Copy link
Contributor

sourcery-ai bot commented Jul 12, 2025

Reviewer's Guide

Enhance media extraction in getBase64FromMediaMessage by detecting and normalizing media embedded within templateMessage structures before falling back to the existing logic.

Sequence diagram for media extraction from templateMessage in getBase64FromMediaMessage

sequenceDiagram
    participant Caller
    participant BaileysService
    participant Template
    Caller->>BaileysService: getBase64FromMediaMessage(msg)
    alt msg contains templateMessage
        BaileysService->>Template: Access hydratedTemplate or hydratedFourRowTemplate
        alt Template contains supported media type
            BaileysService->>BaileysService: Extract mediaMessage and mediaType
            BaileysService->>BaileysService: Normalize msg.message
        else Template does not contain supported media
            BaileysService->>Caller: Throw error
        end
    else msg does not contain templateMessage
        BaileysService->>BaileysService: Fallback to existing media extraction logic
        alt No supported media type found
            BaileysService->>Caller: Throw error
        end
    end
    BaileysService-->>Caller: Return base64 media or error
Loading

Class diagram for updated media extraction logic in BaileysStartupService

classDiagram
    class BaileysStartupService {
        +getBase64FromMediaMessage(msg)
    }
    class Message {
        +templateMessage
        +imageMessage
        +videoMessage
        +documentMessage
    }
    class TemplateMessage {
        +hydratedTemplate
        +hydratedFourRowTemplate
    }
    class HydratedTemplate {
        +imageMessage
        +videoMessage
        +documentMessage
    }
    BaileysStartupService --> Message
    Message --> TemplateMessage
    TemplateMessage --> HydratedTemplate
    HydratedTemplate --> Message
Loading

File-Level Changes

Change Details Files
Support templateMessage media extraction
  • Add check for msg.message.templateMessage
  • Extract hydratedTemplate or hydratedFourRowTemplate
  • Loop through TypeMediaMessage on template to set mediaMessage and mediaType
  • Remap msg.message to include media url from staticUrl
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Introduce template-specific error handling
  • Throw when no supported media type found in templateMessage
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Re-scope original media detection under else
  • Move existing TypeMediaMessage loop into else branch
  • Retain throw for non-media messages within else block
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @AlexisJusviack - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts:3441` </location>
<code_context>
-          mediaType = type;
-          break;
+      if (msg.message?.templateMessage) {
+        const template =
+          msg.message.templateMessage.hydratedTemplate || msg.message.templateMessage.hydratedFourRowTemplate;
+
</code_context>

<issue_to_address>
Potential edge case if both hydratedTemplate and hydratedFourRowTemplate are undefined.

Accessing template[type] without verifying that template is defined may cause a runtime error. Please add a check to ensure template is defined before using it.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

mediaType = type;
break;
if (msg.message?.templateMessage) {
const template =
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: Potential edge case if both hydratedTemplate and hydratedFourRowTemplate are undefined.

Accessing template[type] without verifying that template is defined may cause a runtime error. Please add a check to ensure template is defined before using it.

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