From 872ef5cc53617d22d8642a7eb51e4ca135d113b9 Mon Sep 17 00:00:00 2001 From: KainanP Date: Sat, 28 Nov 2026 19:33:44 -0300 Subject: [PATCH 1/2] fix(meta): detect audio container in sendWhatsAppAudio base64 uploads Without an audio converter, processAudio named every base64 upload ".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 #2719 Co-Authored-By: Claude Opus 5.5 --- .../channel/meta/whatsapp.business.service.ts | 8 +- src/utils/detectAudioType.ts | 33 ++++++++ test/whatsapp-business-audio.test.ts | 84 +++++++++++++++++++ 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/utils/detectAudioType.ts create mode 100644 test/whatsapp-business-audio.test.ts diff --git a/src/api/integrations/channel/meta/whatsapp.business.service.ts b/src/api/integrations/channel/meta/whatsapp.business.service.ts index 098654c2d0..2e0ea7b84b 100644 --- a/src/api/integrations/channel/meta/whatsapp.business.service.ts +++ b/src/api/integrations/channel/meta/whatsapp.business.service.ts @@ -23,6 +23,7 @@ import { Events, wa } from '@api/types/wa.types'; import { AudioConverter, Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@config/env.config'; import { BadRequestException, InternalServerErrorException } from '@exceptions'; import { createJid } from '@utils/createJid'; +import { detectAudioType } from '@utils/detectAudioType'; import { status } from '@utils/renderStatus'; import { sendTelemetry } from '@utils/sendTelemetry'; import axios from 'axios'; @@ -1481,7 +1482,12 @@ export class BusinessStartupService extends ChannelStartupService { prepareMedia.id = audio; prepareMedia.type = 'link'; } else if (audio && !file) { - mimetype = mimeTypes.lookup(prepareMedia.fileName); + // The base64 body carries no name or type, so use the real container instead of a fixed .mp3: + // Ogg/Opus voice notes uploaded as audio/mpeg are accepted by Meta but never delivered. + const audioType = detectAudioType(Buffer.from(audio.slice(0, 64), 'base64')); + prepareMedia.fileName = `${hash}.${audioType.extension}`; + prepareMedia.mimetype = audioType.mimetype; + mimetype = audioType.mimetype; const id = await this.getIdMedia(prepareMedia); prepareMedia.id = id; prepareMedia.type = 'id'; diff --git a/src/utils/detectAudioType.ts b/src/utils/detectAudioType.ts new file mode 100644 index 0000000000..7646800e34 --- /dev/null +++ b/src/utils/detectAudioType.ts @@ -0,0 +1,33 @@ +export type AudioType = { mimetype: string; extension: string }; + +const MPEG: AudioType = { mimetype: 'audio/mpeg', extension: 'mp3' }; + +/** + * Detects the container of an audio buffer from its magic bytes, limited to the + * 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; + + if (buffer.toString('latin1', 0, 4) === 'OggS') { + return { mimetype: 'audio/ogg', extension: 'ogg' }; + } + + if (buffer.toString('latin1', 0, 5) === '#!AMR') { + return { mimetype: 'audio/amr', extension: 'amr' }; + } + + if (buffer.length >= 8 && buffer.toString('latin1', 4, 8) === 'ftyp') { + return { mimetype: 'audio/mp4', extension: 'm4a' }; + } + + if (buffer.toString('latin1', 0, 3) === 'ID3') return MPEG; + + // ADTS (raw AAC): 12-bit sync word plus layer bits 00; MPEG audio frames use layer bits != 00. + if (buffer[0] === 0xff && (buffer[1] & 0xf6) === 0xf0) { + return { mimetype: 'audio/aac', extension: 'aac' }; + } + + return MPEG; +} diff --git a/test/whatsapp-business-audio.test.ts b/test/whatsapp-business-audio.test.ts new file mode 100644 index 0000000000..b619438ed2 --- /dev/null +++ b/test/whatsapp-business-audio.test.ts @@ -0,0 +1,84 @@ +// Run: npx tsx --test test/whatsapp-business-audio.test.ts +// Importing the service needs a generated Prisma client (npm run db:generate). +import '../src/api/server.module'; + +import { BusinessStartupService } from '../src/api/integrations/channel/meta/whatsapp.business.service'; +import { detectAudioType } from '../src/utils/detectAudioType'; +import assert from 'node:assert/strict'; +import { after, describe, it } from 'node:test'; + +after(() => setImmediate(() => process.exit(0))); + +// Synthetic headers only: enough bytes for the magic numbers, no real audio. +const oggOpus = Buffer.concat([ + Buffer.from('OggS', 'latin1'), + Buffer.from([0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 19]), + Buffer.from('OpusHead', 'latin1'), + Buffer.alloc(32), +]); +const mp3Id3 = Buffer.concat([Buffer.from('ID3', 'latin1'), Buffer.from([4, 0, 0, 0, 0, 0, 0]), Buffer.alloc(32)]); +const mp3Frame = Buffer.concat([Buffer.from([0xff, 0xfb, 0x90, 0x64]), Buffer.alloc(32)]); +const aacAdts = Buffer.concat([Buffer.from([0xff, 0xf1, 0x50, 0x80]), Buffer.alloc(32)]); +const m4a = Buffer.concat([Buffer.from([0, 0, 0, 0x20]), Buffer.from('ftypM4A ', 'latin1'), Buffer.alloc(32)]); +const amr = Buffer.concat([Buffer.from('#!AMR\n', 'latin1'), Buffer.alloc(32)]); + +function fakeService() { + const uploads: any[] = []; + const self = { + configService: { get: () => ({}) }, + logger: { verbose: () => undefined }, + getIdMedia: async (media: any) => { + uploads.push({ ...media }); + return 'media-id'; + }, + }; + return { self, uploads }; +} + +describe('BusinessStartupService.processAudio (base64, no audio converter)', () => { + it('uploads Ogg/Opus voice notes as audio/ogg, not audio/mpeg', async () => { + const { self, uploads } = fakeService(); + const result = await BusinessStartupService.prototype.processAudio.call( + self, + oggOpus.toString('base64'), + '+55 11 90000-0000', + undefined, + ); + + assert.equal(uploads.length, 1); + assert.equal(uploads[0].mimetype, 'audio/ogg'); + assert.match(uploads[0].fileName, /\.ogg$/); + assert.equal(result.mimetype, 'audio/ogg'); + assert.equal(result.id, 'media-id'); + }); + + it('keeps MP3 uploads as audio/mpeg', async () => { + const { self, uploads } = fakeService(); + const result = await BusinessStartupService.prototype.processAudio.call( + self, + mp3Id3.toString('base64'), + '5511900000000', + undefined, + ); + + assert.equal(uploads[0].mimetype, 'audio/mpeg'); + assert.match(uploads[0].fileName, /\.mp3$/); + assert.equal(result.mimetype, 'audio/mpeg'); + }); +}); + +describe('detectAudioType', () => { + it('recognises the containers accepted by the Cloud API', () => { + assert.deepEqual(detectAudioType(oggOpus), { mimetype: 'audio/ogg', extension: 'ogg' }); + assert.deepEqual(detectAudioType(mp3Id3), { mimetype: 'audio/mpeg', extension: 'mp3' }); + assert.deepEqual(detectAudioType(mp3Frame), { mimetype: 'audio/mpeg', extension: 'mp3' }); + assert.deepEqual(detectAudioType(aacAdts), { mimetype: 'audio/aac', extension: 'aac' }); + assert.deepEqual(detectAudioType(m4a), { mimetype: 'audio/mp4', extension: 'm4a' }); + assert.deepEqual(detectAudioType(amr), { mimetype: 'audio/amr', extension: 'amr' }); + }); + + it('falls back to audio/mpeg for unknown or empty input', () => { + assert.deepEqual(detectAudioType(Buffer.from('hello world')), { mimetype: 'audio/mpeg', extension: 'mp3' }); + assert.deepEqual(detectAudioType(Buffer.alloc(0)), { mimetype: 'audio/mpeg', extension: 'mp3' }); + }); +}); From 73c7f73b86659ede7f0c25057e9ce97348e251d1 Mon Sep 17 00:00:00 2001 From: KainanP Date: Sat, 28 Nov 2026 20:09:12 -0300 Subject: [PATCH 2/2] fix(meta): only label Ogg/Opus audio as audio/ogg 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 --- src/utils/detectAudioType.ts | 19 ++++++++++-- test/whatsapp-business-audio.test.ts | 43 +++++++++++++++++++++------- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/utils/detectAudioType.ts b/src/utils/detectAudioType.ts index 7646800e34..960be4f8aa 100644 --- a/src/utils/detectAudioType.ts +++ b/src/utils/detectAudioType.ts @@ -2,16 +2,31 @@ export type AudioType = { mimetype: string; extension: string }; const MPEG: AudioType = { mimetype: 'audio/mpeg', extension: 'mp3' }; +// Ogg page header: 'OggS', version, header type, granule (8), serial (4), sequence (4), CRC (4), segment count. +const OGG_PAGE_HEADER_SIZE = 27; + +/** + * Whether an Ogg stream carries Opus: the first page of an Ogg/Opus stream holds only the + * identification header, which starts with the 'OpusHead' magic right after the segment table. + */ +function isOggOpus(buffer: Buffer): boolean { + if (buffer.length <= OGG_PAGE_HEADER_SIZE) return false; + const start = OGG_PAGE_HEADER_SIZE + buffer[OGG_PAGE_HEADER_SIZE - 1]; + return buffer.length >= start + 8 && buffer.toString('latin1', start, start + 8) === 'OpusHead'; +} + /** * Detects the container of an audio buffer from its magic bytes, limited to the * formats the WhatsApp Cloud API accepts (ogg/opus, mp3, aac, m4a, amr). - * Unknown content falls back to audio/mpeg, which was the previous behaviour. + * Meta only accepts Ogg with the Opus codec ("base audio/ogg not supported"), so Opus is + * labelled with its codec, while other Ogg codecs (e.g. Vorbis) and unknown content fall + * back to audio/mpeg, which was the previous behaviour. */ 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' }; + return isOggOpus(buffer) ? { mimetype: 'audio/ogg; codecs=opus', extension: 'ogg' } : MPEG; } if (buffer.toString('latin1', 0, 5) === '#!AMR') { diff --git a/test/whatsapp-business-audio.test.ts b/test/whatsapp-business-audio.test.ts index b619438ed2..eb28a9cc6f 100644 --- a/test/whatsapp-business-audio.test.ts +++ b/test/whatsapp-business-audio.test.ts @@ -10,12 +10,16 @@ import { after, describe, it } from 'node:test'; after(() => setImmediate(() => process.exit(0))); // Synthetic headers only: enough bytes for the magic numbers, no real audio. -const oggOpus = Buffer.concat([ - Buffer.from('OggS', 'latin1'), - Buffer.from([0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 19]), - Buffer.from('OpusHead', 'latin1'), - Buffer.alloc(32), -]); +// First Ogg page: 27-byte header ending in the segment count, a one-entry segment table, then the codec header. +const oggPage = (packet: Buffer) => + Buffer.concat([ + Buffer.from('OggS', 'latin1'), + Buffer.from([0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, packet.length]), + packet, + Buffer.alloc(32), + ]); +const oggOpus = oggPage(Buffer.concat([Buffer.from('OpusHead', 'latin1'), Buffer.alloc(11)])); +const oggVorbis = oggPage(Buffer.concat([Buffer.from([1]), Buffer.from('vorbis', 'latin1'), Buffer.alloc(23)])); const mp3Id3 = Buffer.concat([Buffer.from('ID3', 'latin1'), Buffer.from([4, 0, 0, 0, 0, 0, 0]), Buffer.alloc(32)]); const mp3Frame = Buffer.concat([Buffer.from([0xff, 0xfb, 0x90, 0x64]), Buffer.alloc(32)]); const aacAdts = Buffer.concat([Buffer.from([0xff, 0xf1, 0x50, 0x80]), Buffer.alloc(32)]); @@ -36,7 +40,7 @@ function fakeService() { } describe('BusinessStartupService.processAudio (base64, no audio converter)', () => { - it('uploads Ogg/Opus voice notes as audio/ogg, not audio/mpeg', async () => { + it('uploads Ogg/Opus voice notes as audio/ogg with the opus codec, not audio/mpeg', async () => { const { self, uploads } = fakeService(); const result = await BusinessStartupService.prototype.processAudio.call( self, @@ -46,12 +50,26 @@ describe('BusinessStartupService.processAudio (base64, no audio converter)', () ); assert.equal(uploads.length, 1); - assert.equal(uploads[0].mimetype, 'audio/ogg'); + assert.equal(uploads[0].mimetype, 'audio/ogg; codecs=opus'); assert.match(uploads[0].fileName, /\.ogg$/); - assert.equal(result.mimetype, 'audio/ogg'); + assert.equal(result.mimetype, 'audio/ogg; codecs=opus'); assert.equal(result.id, 'media-id'); }); + it('does not label Ogg/Vorbis as audio/ogg, which Meta only accepts with Opus', async () => { + const { self, uploads } = fakeService(); + const result = await BusinessStartupService.prototype.processAudio.call( + self, + oggVorbis.toString('base64'), + '5511900000000', + undefined, + ); + + assert.equal(uploads[0].mimetype, 'audio/mpeg'); + assert.match(uploads[0].fileName, /\.mp3$/); + assert.equal(result.mimetype, 'audio/mpeg'); + }); + it('keeps MP3 uploads as audio/mpeg', async () => { const { self, uploads } = fakeService(); const result = await BusinessStartupService.prototype.processAudio.call( @@ -69,7 +87,7 @@ describe('BusinessStartupService.processAudio (base64, no audio converter)', () describe('detectAudioType', () => { it('recognises the containers accepted by the Cloud API', () => { - assert.deepEqual(detectAudioType(oggOpus), { mimetype: 'audio/ogg', extension: 'ogg' }); + assert.deepEqual(detectAudioType(oggOpus), { mimetype: 'audio/ogg; codecs=opus', extension: 'ogg' }); assert.deepEqual(detectAudioType(mp3Id3), { mimetype: 'audio/mpeg', extension: 'mp3' }); assert.deepEqual(detectAudioType(mp3Frame), { mimetype: 'audio/mpeg', extension: 'mp3' }); assert.deepEqual(detectAudioType(aacAdts), { mimetype: 'audio/aac', extension: 'aac' }); @@ -77,6 +95,11 @@ describe('detectAudioType', () => { assert.deepEqual(detectAudioType(amr), { mimetype: 'audio/amr', extension: 'amr' }); }); + it('only treats Ogg as supported when the first page carries OpusHead', () => { + assert.deepEqual(detectAudioType(oggVorbis), { mimetype: 'audio/mpeg', extension: 'mp3' }); + assert.deepEqual(detectAudioType(oggOpus.subarray(0, 30)), { mimetype: 'audio/mpeg', extension: 'mp3' }); + }); + it('falls back to audio/mpeg for unknown or empty input', () => { assert.deepEqual(detectAudioType(Buffer.from('hello world')), { mimetype: 'audio/mpeg', extension: 'mp3' }); assert.deepEqual(detectAudioType(Buffer.alloc(0)), { mimetype: 'audio/mpeg', extension: 'mp3' });