Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): force document mode on whatsapp-web.js (#996)
Yudhi Armyndharisf4704d8677f0
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex 566ee8080..a17472f29 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -2635,6 +2635,55 @@ describe('outbound voice note (PTT)', () => {});});+describe('outbound document mode (#989)', () => {+ const ready = (client: unknown): WhatsAppWebJsAdapter => {+ const adapter = new WhatsAppWebJsAdapter({ sessionId: 's', sessionDataPath: './data/sessions', puppeteer: {} });+ (adapter as unknown as { status: EngineStatus }).status = EngineStatus.READY;+ (adapter as unknown as { client: unknown }).client = client;+ return adapter;+ };+ const sentMessage = { id: { _serialized: 'OUT1' }, timestamp: 1700000001 };+ const bytes = Buffer.from([1]).toString('base64');++ // The regression itself: an image mimetype is exactly what WA Web would reclassify into a photo+ // bubble, so the flag — not the mimetype — has to decide that this is a document.+ it('sendDocumentMessage forces sendMediaAsDocument even for an image mimetype', async () => {+ const sendMessage = jest.fn().mockResolvedValue(sentMessage);+ await ready({ sendMessage }).sendDocumentMessage('[email protected]', {+ mimetype: 'image/png',+ data: bytes,+ filename: 'chart.png',+ });+ expect(sendMessage).toHaveBeenCalledWith(+ '[email protected]',+ expect.objectContaining({ mimetype: 'image/png', filename: 'chart.png' }),+ expect.objectContaining({ sendMediaAsDocument: true }),+ );+ });++ // whatsapp-web.js returns null for ANY @broadcast recipient once the flag is set, and a null send+ // throws — so these two must keep taking the unflagged path.+ it.each(['status@broadcast', '1234567890@broadcast'])('withholds sendMediaAsDocument for %s', async chatId => {+ const sendMessage = jest.fn().mockResolvedValue(sentMessage);+ await ready({ sendMessage }).sendDocumentMessage(chatId, {+ mimetype: 'application/pdf',+ data: bytes,+ filename: 'report.pdf',+ });+ expect(sendMessage).toHaveBeenCalledWith(+ chatId,+ expect.anything(),+ expect.not.objectContaining({ sendMediaAsDocument: true }),+ );+ });++ it('leaves the other media senders off the document path', async () => {+ const sendMessage = jest.fn().mockResolvedValue(sentMessage);+ await ready({ sendMessage }).sendImageMessage('[email protected]', { mimetype: 'image/png', data: bytes });+ expect(sendMessage).toHaveBeenCalledWith('[email protected]', expect.anything(), { caption: undefined });+ });+});+describe('LID resolution for individual sends (#573 — WhatsApp @c.us → @lid migration)', () => {const ready = (client: unknown): WhatsAppWebJsAdapter => {const adapter = new WhatsAppWebJsAdapter({ sessionId: 's', sessionDataPath: './data/sessions', puppeteer: {} });diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.tsindex f87ccc4a1..3b617e34f 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts@@ -1565,14 +1565,28 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEnginreturn this.sendMediaMessage(chatId, media, media.ptt ? { sendAudioAsVoice: true } : undefined);}+ /**+ * Without `sendMediaAsDocument` whatsapp-web.js lets WA Web classify the attachment from its declared+ * mimetype (`Injected/Utils.js` `processMediaData` -> `prepRawMedia`), so an `image/*`, `video/*` or+ * `audio/*` payload posted here reached the recipient as a photo/video/audio bubble — re-encoded and+ * stripped of its filename — instead of a document (#989). Baileys has always forced it via the+ * explicit `document:` content key, so the two engines disagreed on the same request.+ *+ * The flag is withheld for `status@broadcast` and broadcast lists: whatsapp-web.js refuses every+ * `@broadcast` recipient outright once it is set (`Client.js` returns `null`, which `toMessageResult`+ * surfaces as a failed send), so setting it there would turn a working send into an error rather than+ * improve it. Those recipients keep the classification they have today.+ */async sendDocumentMessage(chatId: string, media: MediaInput): Promise<MessageResult> {- return this.sendMediaMessage(chatId, media);+ const kind = chatKind(chatId);+ const asDocument = kind !== 'status' && kind !== 'broadcast';+ return this.sendMediaMessage(chatId, media, asDocument ? { sendMediaAsDocument: true } : undefined);}private async sendMediaMessage(chatId: string,media: MediaInput,- extraOptions?: { sendAudioAsVoice?: boolean },+ extraOptions?: { sendAudioAsVoice?: boolean; sendMediaAsDocument?: boolean },): Promise<MessageResult> {this.ensureReady();this.ensureNotChannelRecipient(chatId);@@ -1583,7 +1597,8 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEnginthis.client!.sendMessage(to, messageMedia, {caption: media.caption,...(media.mentions?.length ? { mentions: media.mentions } : {}),- // sendAudioAsVoice only for audio; {...undefined} contributes no keys.+ // sendAudioAsVoice only for audio, sendMediaAsDocument only for documents;+ // {...undefined} contributes no keys....extraOptions,}),);