Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
refactor(wwjs): qualify participant ids through the identity helper
Yudhi Armyndharis4be7c6a01b16
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex 8e5bb6ba3..a8a4cf3f8 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -5873,6 +5873,21 @@ describe('WhatsAppWebJsAdapter honest outcomes (no phantom success)', () => {(adapter as unknown as Record<string, (g: string, p: string[]) => Promise<unknown>>)[op](GROUP, ['628111']),).rejects.toBeInstanceOf(EngineRefusedError);});++ it('qualifies only bare numbers, never double-qualifying an id that carries a domain', async () => {+ // Characterisation: the old rule (`p.includes('@')`) agreed with toParticipantWid on every+ // input that reaches here, so this pins the behaviour rather than driving the change. It is+ // the guard against a future qualifier that appends to an already-domained id.+ const libOp = jest.fn().mockResolvedValue({ status: 200 });+ const chat = groupChat({ [op]: libOp });+ const adapter = readyAdapter({ getChatById: jest.fn().mockResolvedValue(chat) });+ await (adapter as unknown as Record<string, (g: string, p: string[]) => Promise<unknown>>)[op](GROUP, [+ '628111',+ '[email protected]',+ '12345678901234567890@lid',+ ]);+ expect(libOp).toHaveBeenCalledWith(['[email protected]', '[email protected]', '12345678901234567890@lid']);+ });},);diff --git a/src/engine/adapters/wwebjs-groups.ts b/src/engine/adapters/wwebjs-groups.tsindex e4ca49092..b42acdbd4 100644--- a/src/engine/adapters/wwebjs-groups.ts+++ b/src/engine/adapters/wwebjs-groups.ts@@ -11,6 +11,7 @@ import {ParticipantOperationResult,} from '../interfaces/whatsapp-engine.interface';import { GroupChat, GroupMetadataRaw, GroupCreateResult, SerializedWid, readWid } from '../types/whatsapp-web-js.types';+import { toParticipantWid } from '../identity/wa-id';import { EngineRefusedError } from '../../common/errors/engine-refused.error';import { EngineTransportError } from '../../common/errors/engine-transport.error';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';@@ -161,7 +162,7 @@ export class WwebjsGroups {async createGroup(name: string, participants: string[]): Promise<Group> {this.host.ensureReady();// Ensure participant IDs are in correct format- const participantIds = participants.map(p => (p.includes('@') ? p : `${p}@c.us`));+ const participantIds = participants.map(toParticipantWid);const result = await this.client().createGroup(name, participantIds);// whatsapp-web.js reports a failed creation by RESOLVING with a plain string@@ -191,7 +192,7 @@ export class WwebjsGroups {if (!chat.isGroup) {throw new Error('Chat is not a group');}- const participantIds = participants.map(p => (p.includes('@') ? p : `${p}@c.us`));+ const participantIds = participants.map(toParticipantWid);const raw = await (chat as unknown as GroupChat).addParticipants(participantIds);// whatsapp-web.js reports a batch-level refusal (no admin rights, empty group) by RESOLVING a// plain reason string (GroupChat.js:106-107,128-130) instead of throwing — surface it as a@@ -249,7 +250,7 @@ export class WwebjsGroups {if (!chat.isGroup) {throw new Error('Chat is not a group');}- const participantIds = participants.map(p => (p.includes('@') ? p : `${p}@c.us`));+ const participantIds = participants.map(toParticipantWid);const res = await (chat as unknown as GroupChat)[op](participantIds);if (res?.status !== 200) {throw new EngineRefusedError(`${op} refused for group ${groupId} (status ${res?.status ?? 'unknown'})`);