Skip to content

Commit

Permalink
fix: Fixed WPP.contact.queryExists for WhatsApp >= 2.2244.5 (fix #758)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Nov 19, 2022
1 parent f8080c3 commit b5e8b3c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/contact/functions/queryExists.ts
Expand Up @@ -38,6 +38,8 @@ export interface QueryExistsResult {

const cache = new Map<string, QueryExistsResult | null>();

let useInternationalMode: boolean | null = null;

/**
* Check if the number exists and what is correct ID
*
Expand All @@ -62,7 +64,30 @@ export async function queryExists(
return cache.get(id)!;
}

const result = await sendQueryExists(wid).catch(() => null);
/**
* @whatsapp >= 2.2244.5
* Since 2.2244.5 there are a problem with queryExists function,
* that not prepend the `+` sign
*/
if (useInternationalMode === null) {
const source = sendQueryExists.toString();

useInternationalMode = !/`\+\$\{\w+\.toString\(\)\}`/.test(source);
}

let result: QueryExistsResult | null = null;

if (useInternationalMode) {
const internationalWid = assertWid(contactId);

internationalWid.toString = () => `+${internationalWid._serialized}`;

result = await sendQueryExists(internationalWid).catch(() => null);
}

if (!result) {
result = await sendQueryExists(wid).catch(() => null);
}

cache.set(id, result);

Expand Down

0 comments on commit b5e8b3c

Please sign in to comment.