Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): answer the wwebjs number check with the phone id
Yudhi Armyndharis6f19ad8a823a
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex e21082d2f..e32bada73 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -9268,6 +9268,21 @@ describe('WhatsAppWebJsAdapter raw-id extraction hardening', () => {await expect(missing.checkNumberExists('628111')).resolves.toBe(false);});+ // The interface answers in the neutral dialect (`<phone>@c.us`), as the Baileys engine does. WhatsApp+ // Web names a migrated contact by its lid, an engine-private id that the number check must not expose.+ it.each([+ ['a bare number', '628111'],+ ['a phone id', '[email protected]'],+ ['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 });++ await expect(adapter.getNumberId(input)).resolves.toBe('[email protected]');+ await expect(adapter.checkNumberExists(input)).resolves.toBe(true);+ expect(getNumberId).toHaveBeenCalledWith(input);+ });+it('getSubscribedChannels reads $1 on a renamed build, never the literal "undefined"', async () => {const getChannels = jest.fn().mockResolvedValue([{ id: { $1: '120363@newsletter' }, name: 'Renamed' }]);diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.tsindex 8caa7ebe7..0e1e84503 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts@@ -219,7 +219,9 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEnginisPageTransportError: error => this.isPageTransportError(error),reportIfPageTransportError: (error, context) => this.reportIfPageTransportError(error, context),ensureNotChannelRecipient: chatId => this.ensureNotChannelRecipient(chatId),- getNumberId: number => this.getNumberId(number),+ // The raw answer, `@lid` included: the send resolver addresses a migrated contact by it, while the+ // public getNumberId reports the neutral phone id.+ getNumberId: number => this.contacts.lookupNumberId(number),capInboundMediaFor: (msg, maxBytesOverride, ticket) => this.capInboundMediaFor(msg, maxBytesOverride, ticket),config: this.config,getCallbacks: () => this.callbacks,diff --git a/src/engine/adapters/wwebjs-contacts.ts b/src/engine/adapters/wwebjs-contacts.tsindex a149f30d8..1f9a6909b 100644--- a/src/engine/adapters/wwebjs-contacts.ts+++ b/src/engine/adapters/wwebjs-contacts.ts@@ -211,7 +211,12 @@ export class WwebjsContacts {}}- async getNumberId(number: string): Promise<string | null> {+ /**+ * The lookup as WhatsApp Web answers it: `<lid>@lid` for a contact migrated to privacy-id addressing,+ * else `<phone>@c.us`, or null when the number is not registered. The send path needs the `@lid`+ * (see WwebjsMessaging.resolveSendId); {@link getNumberId} is the answer that crosses the engine boundary.+ */+ async lookupNumberId(number: string): Promise<string | null> {this.host.ensureReady();const numberId = await withPage(this.host, 'getNumberId', () => this.client().getNumberId(number));// Read both property names: a WA Web build that renamed `_serialized` would otherwise make@@ -219,8 +224,16 @@ export class WwebjsContacts {return readWid(numberId) ?? null;}+ 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;+ }+async checkNumberExists(number: string): Promise<boolean> {- return (await this.getNumberId(number)) !== null;+ return (await this.lookupNumberId(number)) !== null;}async resolveContactPhone(contactId: string): Promise<string | null> {