Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): label generic URL status media with the route's type
Yudhi Armyndharisf72f8b99d047
diff --git a/src/engine/adapters/baileys-status.ts b/src/engine/adapters/baileys-status.tsindex b8c1f2853..a23741d92 100644--- a/src/engine/adapters/baileys-status.ts+++ b/src/engine/adapters/baileys-status.ts@@ -61,7 +61,9 @@ export class BaileysStatus {options: StatusPostOptions,): Promise<StatusResult> {this.host.ensureReady();- const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());+ // The kind's default labels a URL whose host serves a generic type, rather than octet-stream.+ const fallbackType = kind === 'image' ? 'image/jpeg' : kind === 'video' ? 'video/mp4' : 'audio/ogg; codecs=opus';+ const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl(), fallbackType);// A voice status carries no caption: WhatsApp has nowhere to render one on a status voice note,// and `ptt` is what makes it a voice note rather than an audio file. Baileys reads that same flag// to decide a status may take a background colour, so the colour `postStatus` already forwardsdiff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex d3732384b..4d2a18ded 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -1829,9 +1829,7 @@ describe('BaileysAdapter messaging', () => {(adapter as unknown as { sock: unknown }).sock = null;return Promise.reject(new Error('Connection Closed'));});- await expect(adapter.sendTextMessage('[email protected]', 'hello')).rejects.toBeInstanceOf(- EngineNotReadyError,- );+ await expect(adapter.sendTextMessage('[email protected]', 'hello')).rejects.toBeInstanceOf(EngineNotReadyError);});it('a send that fails on a socket still in place rethrows the failure as is', async () => {@@ -6946,6 +6944,26 @@ describe('BaileysAdapter status posting', () => {);});+ // A URL posted without a declared type carries the octet-stream placeholder, so the fetched+ // Content-Type labels the bytes, and a host that serves a generic one falls back to the kind's default.+ it.each([+ ['image', 'image/png', 'image/png'],+ ['image', 'application/octet-stream', 'image/jpeg'],+ ['video', '', 'video/mp4'],+ ] as const)('a %s status from a URL served as %j goes out as %s', async (kind, served, expected) => {+ (loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([9]), mimetype: served });+ fakeSock.sendMessage.mockResolvedValue({ key: { id: 'URL1' }, messageTimestamp: 1719600000 });+ const adapter = await ready();+ const media = { mimetype: 'application/octet-stream', data: 'https://cdn.example/m' };+ const options = { recipients: ['[email protected]'] };+ await (kind === 'image' ? adapter.postImageStatus(media, options) : adapter.postVideoStatus(media, options));+ expect(fakeSock.sendMessage).toHaveBeenCalledWith(+ 'status@broadcast',+ expect.objectContaining({ [kind]: Buffer.from([9]), mimetype: expected }),+ expect.anything(),+ );+ });+it('postStatus rejects an absent/empty recipients list with a 400 (Baileys posts to exactly the allow-list)', async () => {const adapter = await ready();await expect(adapter.postTextStatus('hello', {})).rejects.toBeInstanceOf(BadRequestException);diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex dfdc80025..37f4b4bcf 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -4864,6 +4864,29 @@ describe('outbound document mode (#989)', () => {);});+ // Status media takes the same route default: a URL posted without a declared type keeps a specific+ // fetched type, and a generic one would otherwise reach WA Web and turn a photo into a document.+ it.each<['postImageStatus' | 'postVideoStatus' | 'postVoiceStatus', string, string]>([+ ['postImageStatus', 'image/png', 'image/png'],+ ['postImageStatus', 'application/octet-stream', 'image/jpeg'],+ ['postVideoStatus', '', 'video/mp4'],+ ['postVoiceStatus', 'binary/octet-stream', 'audio/ogg; codecs=opus'],+ ])('%s from a URL served as %j goes out as %s', async (post, fetched, expected) => {+ (undiciFetch as jest.Mock).mockResolvedValue(remoteResponse({ 'content-type': fetched }));+ const sendMessage = jest.fn().mockResolvedValue(sentMessage);++ await ready({ sendMessage })[post](+ { mimetype: 'application/octet-stream', data: 'https://files.example.com/status' },+ {},+ );++ expect(sendMessage).toHaveBeenCalledWith(+ 'status@broadcast',+ expect.objectContaining({ mimetype: expected }),+ expect.anything(),+ );+ });+it('leaves a document with a generic fetched type as it is', async () => {(undiciFetch as jest.Mock).mockResolvedValue(remoteResponse({ 'content-type': 'binary/octet-stream' }));const sendMessage = jest.fn().mockResolvedValue(sentMessage);diff --git a/src/engine/adapters/wwebjs-status.ts b/src/engine/adapters/wwebjs-status.tsindex e132d8e4b..e39723b0f 100644--- a/src/engine/adapters/wwebjs-status.ts+++ b/src/engine/adapters/wwebjs-status.ts@@ -161,28 +161,30 @@ export class WwebjsStatus {}async postImageStatus(media: MediaInput, options: StatusPostOptions): Promise<StatusResult> {- return this.postMediaStatus(media, options);+ return this.postMediaStatus(media, options, 'image/jpeg');}async postVideoStatus(media: MediaInput, options: StatusPostOptions): Promise<StatusResult> {- return this.postMediaStatus(media, options);+ return this.postMediaStatus(media, options, 'video/mp4');}async postVoiceStatus(media: MediaInput, options: StatusPostOptions): Promise<StatusResult> {// `sendAudioAsVoice` is what makes the bubble a voice note rather than an audio file: it becomes// `isPtt` inside the page. The waveform is separate — whatsapp-web.js already generates one for// any audio going to status, so it appears either way, but only this flag changes the bubble.- return this.postMediaStatus(media, options, { sendAudioAsVoice: true });+ return this.postMediaStatus(media, options, 'audio/ogg; codecs=opus', { sendAudioAsVoice: true });}+ /** `fallbackType` labels a URL whose host serves a generic type, which WA Web would deliver as a document. */private async postMediaStatus(media: MediaInput,options: StatusPostOptions,+ fallbackType: string,extra?: { sendAudioAsVoice: true },): Promise<StatusResult> {this.host.ensureReady();this.warnStatusRecipientsOnce(options);- const messageMedia = await toMessageMedia(media, this.host.config.proxy?.url);+ const messageMedia = await toMessageMedia(media, this.host.config.proxy?.url, { fallbackType });// Non-idempotent: a replayed post would publish the status twice. See reportPageDeath.const msg = await reportPageDeath(this.host, 'postMediaStatus', () =>this.client().sendMessage('status@broadcast', messageMedia, {