Skip to content

fix(baileys): send mediaUrl in webhook without SAVE_DATA.NEW_MESSAGE#2571

Open
renatomjr wants to merge 2 commits into
evolution-foundation:developfrom
renatomjr:fix/media-webhook
Open

fix(baileys): send mediaUrl in webhook without SAVE_DATA.NEW_MESSAGE#2571
renatomjr wants to merge 2 commits into
evolution-foundation:developfrom
renatomjr:fix/media-webhook

Conversation

@renatomjr
Copy link
Copy Markdown

@renatomjr renatomjr commented May 28, 2026

📋 Description

Fixes sending mediaUrl via webhook even when DATABASE.SAVE_DATA.NEW_MESSAGE is not enabled.

🔗 Related Issue

Closes #(issue_number)

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🔧 Refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🧹 Code cleanup
  • 🔒 Security fix

🧪 Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced
  • Tested with different connection types (if applicable)

📸 Screenshots (if applicable)

✅ Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have manually tested my changes thoroughly
  • I have verified the changes work with different scenarios
  • Any dependent changes have been merged and published

Summary by Sourcery

Bug Fixes:

  • Fix missing mediaUrl in webhooks when DATABASE.SAVE_DATA.NEW_MESSAGE is disabled.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 28, 2026

Reviewer's Guide

Ensures media messages uploaded to S3 always get a mediaUrl added to the webhook payload, even when DATABASE.SAVE_DATA.NEW_MESSAGE is disabled, by decoupling media handling from message persistence and guarding DB operations behind presence of a saved message.

Sequence diagram for sending mediaUrl in webhook without SAVE_DATA.NEW_MESSAGE

sequenceDiagram
    participant BaileysStartupService
    participant PrismaRepository
    participant S3Service
    participant WebhookConsumer

    BaileysStartupService->>BaileysStartupService: [isMedia]
    alt SAVE_DATA.NEW_MESSAGE enabled
        BaileysStartupService->>PrismaRepository: message.create(messageData)
        PrismaRepository-->>BaileysStartupService: msg
    else SAVE_DATA.NEW_MESSAGE disabled
        BaileysStartupService->>BaileysStartupService: msg remains null
    end

    BaileysStartupService->>BaileysStartupService: hasValidMediaContent(message)
    alt has valid media
        BaileysStartupService->>BaileysStartupService: getBase64FromMediaMessage(message, true)
        BaileysStartupService-->>BaileysStartupService: buffer, mediaType, fileName, size
        BaileysStartupService->>S3Service: uploadFile(fullName, buffer, size.fileLength.low, headers)
        S3Service-->>BaileysStartupService: upload complete
        BaileysStartupService->>S3Service: getObjectUrl(fullName)
        S3Service-->>BaileysStartupService: mediaUrl
        BaileysStartupService->>BaileysStartupService: messageRaw.message.mediaUrl = mediaUrl
        alt msg.id exists
            BaileysStartupService->>PrismaRepository: media.create({ messageId: msg.id, ... })
            BaileysStartupService->>PrismaRepository: message.update({ id: msg.id }, messageRaw)
        else no persisted message
            BaileysStartupService->>BaileysStartupService: skip DB updates
        end
    else no valid media
        BaileysStartupService->>BaileysStartupService: skip upload
    end

    BaileysStartupService->>WebhookConsumer: sendDataWebhook(MESSAGES_UPSERT, messageRaw)
Loading

File-Level Changes

Change Details Files
Decouple message persistence from media handling so mediaUrl is always populated on webhook payloads, regardless of SAVE_DATA.NEW_MESSAGE configuration.
  • Introduce a reusable msg variable outside the SAVE_DATA.NEW_MESSAGE conditional to hold the persisted message when saving is enabled.
  • Restrict DB writes (message.create, media.create, message.update) to only execute when a message has actually been persisted, using optional checks on msg.id.
  • Keep S3 upload and mediaUrl calculation logic outside the SAVE_DATA.NEW_MESSAGE guard so media is processed and mediaUrl is attached to messageRaw even when messages are not stored in the database.
src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Minor cleanup of message processing logic.
  • Remove leftover console.log of messageRaw to avoid noisy logging in production.
  • Preserve error handling around S3 upload and media persistence while keeping it at the correct scope.
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
Copy Markdown
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 - I've left some high level feedback:

  • Consider giving msg a more specific type than any (e.g., the Prisma message model type or a narrowed interface) to make it clearer which fields are expected and to catch potential misuse at compile time.
  • Now that the media handling block can run even when SAVE_DATA.NEW_MESSAGE is disabled, double-check whether the return inside the isVideo && !SAVE_VIDEO branch is still the desired behavior, since it exits before the webhook send and may skip non-media-related processing for those messages.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider giving `msg` a more specific type than `any` (e.g., the Prisma message model type or a narrowed interface) to make it clearer which fields are expected and to catch potential misuse at compile time.
- Now that the media handling block can run even when `SAVE_DATA.NEW_MESSAGE` is disabled, double-check whether the `return` inside the `isVideo && !SAVE_VIDEO` branch is still the desired behavior, since it exits before the webhook send and may skip non-media-related processing for those messages.

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.

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