Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): answer the held phone in the wwebjs number check
Yudhi Armyndharisbfbe6e7d1c9e
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex c19e3d327..9118220db 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -9353,11 +9353,60 @@ describe('WhatsAppWebJsAdapter raw-id extraction hardening', () => {['a number with a plus', '+628111'],])('getNumberId answers %s of a migrated contact with the phone id, not its lid', async (_label, input) => {const getNumberId = jest.fn().mockResolvedValue({ _serialized: '159442138038327@lid' });- const adapter = readyAdapter({ getNumberId });+ const getContactLidAndPhone = jest.fn().mockResolvedValue([{ lid: '159442138038327@lid', pn: '[email protected]' }]);+ const adapter = readyAdapter({ getNumberId, getContactLidAndPhone });await expect(adapter.getNumberId(input)).resolves.toBe('[email protected]');await expect(adapter.checkNumberExists(input)).resolves.toBe(true);expect(getNumberId).toHaveBeenCalledWith(input);+ expect(getContactLidAndPhone).toHaveBeenCalledWith(['159442138038327@lid']);+ });++ // Brazil, Mexico and Argentina numbers exist on WhatsApp under another digit string than the one typed (the+ // ninth digit). The number check reports the id WhatsApp knows, as the Baileys engine does.+ it('getNumberId answers the phone WhatsApp holds for a migrated contact, not the digits typed', async () => {+ const getNumberId = jest.fn().mockResolvedValue({ _serialized: '159442138038327@lid' });+ const getContactLidAndPhone = jest+ .fn()+ .mockResolvedValue([{ lid: '159442138038327@lid', pn: '[email protected]' }]);+ const adapter = readyAdapter({ getNumberId, getContactLidAndPhone });++ await expect(adapter.getNumberId('5511987654321')).resolves.toBe('[email protected]');+ });++ it.each([+ ['no mapping', []],+ ['an empty phone', [{ lid: '159442138038327@lid', pn: '' }]],+ ['no phone', [{ lid: '159442138038327@lid' }]],+ ])('getNumberId keeps the typed number for a migrated contact WhatsApp Web has %s for', async (_label, mapping) => {+ const getNumberId = jest.fn().mockResolvedValue({ _serialized: '159442138038327@lid' });+ const adapter = readyAdapter({ getNumberId, getContactLidAndPhone: jest.fn().mockResolvedValue(mapping) });++ await expect(adapter.getNumberId('+5511987654321')).resolves.toBe('[email protected]');+ });++ it('getNumberId does not read a phone for a contact that is not migrated', async () => {+ const getNumberId = jest.fn().mockResolvedValue({ _serialized: '[email protected]' });+ const getContactLidAndPhone = jest.fn();+ const adapter = readyAdapter({ getNumberId, getContactLidAndPhone });++ await expect(adapter.getNumberId('5511987654321')).resolves.toBe('[email protected]');+ expect(getContactLidAndPhone).not.toHaveBeenCalled();+ });++ it('getNumberId fails with the phone read instead of answering a number that may be wrong', async () => {+ const getNumberId = jest.fn().mockResolvedValue({ _serialized: '159442138038327@lid' });+ const failing = (error: Error): WhatsAppWebJsAdapter =>+ readyAdapter({ getNumberId, getContactLidAndPhone: jest.fn().mockRejectedValue(error) });++ await expect(failing(new Error('Evaluation failed')).getNumberId('5511987654321')).rejects.toThrow(+ 'Evaluation failed',+ );+ await expect(+ failing(new Error('Protocol error: Target closed')).getNumberId('5511987654321'),+ ).rejects.toBeInstanceOf(EngineTransportError);+ // Existence is all checkNumberExists asks, so the phone read never reaches it.+ await expect(failing(new Error('Evaluation failed')).checkNumberExists('5511987654321')).resolves.toBe(true);});it('getSubscribedChannels reads $1 on a renamed build, never the literal "undefined"', async () => {diff --git a/src/engine/adapters/wwebjs-client-teardown.spec.ts b/src/engine/adapters/wwebjs-client-teardown.spec.tsindex d9081f43c..b2b70da9f 100644--- a/src/engine/adapters/wwebjs-client-teardown.spec.ts+++ b/src/engine/adapters/wwebjs-client-teardown.spec.ts@@ -158,6 +158,18 @@ const CASES: Case[] = [},run: adapter => adapter.muteChat(CHAT, Date.now() + 60_000),},+ {+ // The phone behind the lid is read after the lookup; contacts has no other read after an await.+ name: 'a number check of a migrated contact',+ build: parked => {+ const getContactLidAndPhone = jest.fn();+ return {+ client: { getNumberId: () => parked.park().then(() => found(LID)), getContactLidAndPhone },+ unreached: getContactLidAndPhone,+ };+ },+ run: adapter => adapter.getNumberId('5511987654321'),+ },{// The client is read again after the lookup, inside a catch that answers a missing group with null.name: 'a group info read',diff --git a/src/engine/adapters/wwebjs-contacts.ts b/src/engine/adapters/wwebjs-contacts.tsindex e48717553..c2085109c 100644--- a/src/engine/adapters/wwebjs-contacts.ts+++ b/src/engine/adapters/wwebjs-contacts.ts@@ -255,10 +255,14 @@ export class WwebjsContacts {async getNumberId(number: string): Promise<string | null> {const wid = await this.lookupNumberId(number);- // The contract is the neutral `<phone>@c.us`, which is what the Baileys engine answers. A migrated- // contact's lookup names its `@lid`, the engine's own scheme; the phone the caller supplied is the- // stable neutral id, and a send to it re-resolves to the lid.- return wid?.endsWith('@lid') ? `${userPart(number).replace(/\D/g, '')}@c.us` : wid;+ if (!wid?.endsWith('@lid')) return wid;+ // The contract is the neutral `<phone>@c.us` WhatsApp holds for the account, which is what the Baileys+ // engine answers. It can differ from the digits typed (a Brazilian, Mexican or Argentine number with or+ // without its extra digit). A migrated contact's lookup names its `@lid`, the engine's own scheme, so the+ // phone is read back through it. When WhatsApp Web knows none, the typed number stands in, and a send to+ // it re-resolves to the lid. A failed read propagates, as the lookup above does.+ const phone = await withPage(this.host, 'getNumberId', () => this.resolveContactPhone(wid));+ return `${phone ?? userPart(number).replace(/\D/g, '')}@c.us`;}async checkNumberExists(number: string): Promise<boolean> {