Repository navigation
Conversation
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>
Contributor
Reviewer's GuideImplementationDetect the base64 audio container and set the upload filename and MIME type before media registration, using the detector introduced for this path.
TestsCover container detection, fallback behavior, and the metadata passed to media registration for base64 audio uploads.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📋 Description
On a
WHATSAPP-BUSINESS(Cloud API) instance withoutAUDIO_CONVERTER.API_URL,POST /message/sendWhatsAppAudiowith a base64audioalways named the upload<number>-<timestamp>.mp3and looked the mimetype up from that name. An Ogg/Opus voice note was therefore uploaded to Meta asaudio/mpeg: the send returns201with awamid, but the message is never delivered and no status webhook ever arrives (details in #2719).A second, smaller problem in the same branch:
prepareMedia.mimetypewas only set aftergetIdMedia(), so the upload itself had no explicit content type (typeFilewasundefined).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 toaudio/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: trueflag 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
🧪 Testing
test/whatsapp-business-audio.test.ts(Node's built-in test runner throughtsx;/test/is in.gitignore, so it was added withgit add -f. Move or drop it if you prefer). It callsBusinessStartupService.prototype.processAudiowith a stubbedgetIdMediaand synthetic audio headers (no real audio, no network):develop.mp3/undefinedat upload,audio/mpegin the message.ogg/audio/oggundefinedaudio/mpegprocessAudiocases)npx tsc --noEmit,eslintandprettier --checkare clean on the changed files. I did not test against a live Meta account.✅ Checklist
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:
Enhancements:
Tests: