Skip to content

fix(meta): detect audio container in sendWhatsAppAudio base64 uploads - #2810

Open
KainanP wants to merge 2 commits into
evolution-foundation:developfrom
KainanP:fix/meta-audio-mimetype
Open

KainanP wants to merge 2 commits into
evolution-foundation:developfrom
KainanP:fix/meta-audio-mimetype

Conversation

@KainanP

@KainanP KainanP commented Nov 28, 2026 •

Copy link
Copy Markdown

📋 Description

On a WHATSAPP-BUSINESS (Cloud API) instance without AUDIO_CONVERTER.API_URL, POST /message/sendWhatsAppAudio with a base64 audio always named the upload <number>-<timestamp>.mp3 and looked the mimetype up from that name. An Ogg/Opus voice note was therefore uploaded to Meta as audio/mpeg: the send returns 201 with a wamid, but the message is never delivered and no status webhook ever arrives (details in #2719).

A second, smaller problem in the same branch: prepareMedia.mimetype was only set after getIdMedia(), so the upload itself had no explicit content type (typeFile was undefined).

This PR adds src/utils/detectAudioType.ts, which reads the first bytes of the buffer and recognises the containers the Cloud API accepts (Ogg, AMR, MP4/M4A, MP3 with ID3 or a bare frame, ADTS AAC). processAudio() uses it to choose the file extension and mimetype before the upload. Unknown content still falls back to audio/mpeg, so nothing changes for MP3 senders. The URL, file-upload and audio-converter paths are not touched. Only the first 64 base64 characters (48 bytes) are decoded for the check.

Not included: the voice: true flag mentioned at the end of the issue. It changes how the message is rendered, not whether it is delivered, so it seemed better as a separate PR if you want it.

🔗 Related Issue

Closes #2719

🧪 Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)

🧪 Testing

test/whatsapp-business-audio.test.ts (Node's built-in test runner through tsx; /test/ is in .gitignore, so it was added with git add -f. Move or drop it if you prefer). It calls BusinessStartupService.prototype.processAudio with a stubbed getIdMedia and synthetic audio headers (no real audio, no network):

npm run db:generate   # needs DATABASE_PROVIDER / DATABASE_CONNECTION_URI set; no database is contacted
npx tsx --test test/whatsapp-business-audio.test.ts
develop this branch
Ogg/Opus base64 → uploaded file name / mimetype .mp3 / undefined at upload, audio/mpeg in the message .ogg / audio/ogg
MP3 (ID3) base64 → mimetype at upload undefined audio/mpeg
Test result 2 of 4 fail (both processAudio cases) 4 of 4 pass

npx tsc --noEmit, eslint and prettier --check are clean on the changed files. I did not test against a live Meta account.

✅ 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
  • My changes generate no new warnings

AI use: written by Claude Code (Claude Opus 5.5), including the code, the test and this description; KainanP is the submitter.

🤖 Generated with Claude Code

Summary by Sourcery

Fix WhatsApp Business base64 audio uploads by detecting their container and setting accurate upload metadata before media submission.

Bug Fixes:

  • Detect audio containers in base64 WhatsApp Business uploads so supported formats receive the correct filename extension and MIME type before being sent to Meta.
  • Preserve a safe audio/mpeg fallback for unknown content and unsupported Ogg codecs.

Enhancements:

  • Recognize Ogg/Opus, AMR, MP4/M4A, MP3, and ADTS AAC audio from their container headers.

Tests:

  • Add coverage for audio container detection and base64 upload metadata, including supported formats, unsupported Ogg content, and fallback behavior.

Without an audio converter, processAudio named every base64 upload
"<hash>.mp3" and derived the mimetype from that name, so Ogg/Opus voice
notes were uploaded to the Cloud API as audio/mpeg. Meta accepts the
send (201 + wamid) but never delivers it. The mimetype was also only set
after getIdMedia, so the upload itself carried no explicit type.

Sniff the first bytes (ogg, amr, m4a, mp3, aac) to pick the file name
and mimetype before the upload; unknown content keeps audio/mpeg.

Closes evolution-foundation#2719

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Detect the base64 audio container and set the upload filename and MIME type before media registration, using the detector introduced for this path.

  • src/api/integrations/channel/meta/whatsapp.business.service.ts L26, L1485-1490
  • src/utils/detectAudioType.ts L1-48

Tests

Cover container detection, fallback behavior, and the metadata passed to media registration for base64 audio uploads.

  • test/whatsapp-business-audio.test.ts L1-107

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

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've found 2 issues

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

## Individual Comments

### Comment 1
<location path="src/utils/detectAudioType.ts" line_range="10" />
<code_context>
+ * formats the WhatsApp Cloud API accepts (ogg/opus, mp3, aac, m4a, amr).
+ * Unknown content falls back to audio/mpeg, which was the previous behaviour.
+ */
+export function detectAudioType(buffer: Buffer): AudioType {
+  if (!buffer || buffer.length < 4) return MPEG;
+
</code_context>
<issue_to_address>
**Default imports fail**

When a caller imports this utility as a default export, `detectAudioType.ts` exposes only the named `detectAudioType`, so the import has no default binding and TypeScript compilation fails.

Add a default export for `detectAudioType` while retaining the named export.
</issue_to_address>

### Comment 2
<location path="src/utils/detectAudioType.ts" line_range="13-14" />
<code_context>
+export function detectAudioType(buffer: Buffer): AudioType {
+  if (!buffer || buffer.length < 4) return MPEG;
+
+  if (buffer.toString('latin1', 0, 4) === 'OggS') {
+    return { mimetype: 'audio/ogg', extension: 'ogg' };
+  }
+
</code_context>
<issue_to_address>
**Ogg audio is rejected by Meta**

When the base64 audio is an Ogg container, `detectAudioType` labels every Ogg file as `audio/ogg`, so non-Opus audio is sent as supported and Meta rejects it. For Ogg/Opus, `getIdMedia` uploads without the required `codecs=opus` parameter, so Meta rejects the voice note.

Detect the Ogg codec, send Opus with Meta’s required `codecs=opus` MIME parameter, and do not label other Ogg codecs as supported.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and the detected MIME type and filename are used when uploading audio to Meta, so a misclassification could cause a send to fail or deliver audio that recipients cannot play. Reverting restores the old upload metadata, but any message already delivered cannot be recalled.

Blocking findings: src/utils/detectAudioType.ts:14


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src/utils/detectAudioType.ts Resolved
Comment thread src/utils/detectAudioType.ts Resolved
Meta accepts Ogg audio only with the Opus codec ("base audio/ogg not
supported"). Detect Opus by the OpusHead magic in the first Ogg page and
upload it as "audio/ogg; codecs=opus"; other Ogg codecs such as Vorbis
keep the previous audio/mpeg fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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