From 576768e3900613bd39971b26bf559407da07cfcf Mon Sep 17 00:00:00 2001 From: Lucas Rosa Date: Thu, 17 Dec 2020 23:16:31 -0300 Subject: [PATCH] feat: Get registered ID for a given phone number (#483) Exposes internal getNumberById function for easy usage. This can help with dealing with brazilian numbers with the extra digit, always returning the correct ID. This will probably eventually replace the current isRegisteredUser function. Co-authored-by: Pedro Lopez --- index.d.ts | 3 +++ src/Client.js | 20 ++++++++++++++++++++ src/util/Injected.js | 3 +++ 3 files changed, 26 insertions(+) diff --git a/index.d.ts b/index.d.ts index 8353c5a..bfa5e32 100644 --- a/index.d.ts +++ b/index.d.ts @@ -77,6 +77,9 @@ declare namespace WAWebJS { /** Check if a given ID is registered in whatsapp */ isRegisteredUser(contactId: string): Promise + /** Get the registered WhatsApp ID for a number. Returns null if the number is not registered on WhatsApp. */ + getNumberId(number: string): Promise + /** * Mutes the Chat until a specified date * @param chatId ID of the chat that will be muted diff --git a/src/Client.js b/src/Client.js index 0aa30be..8560094 100644 --- a/src/Client.js +++ b/src/Client.js @@ -712,6 +712,26 @@ class Client extends EventEmitter { }, id); } + /** + * Get the registered WhatsApp ID for a number. + * Will return null if the number is not registered on WhatsApp. + * @param {string} number Number or ID ("@c.us" will be automatically appended if not specified) + * @returns {Promise} + */ + async getNumberId(number) { + if(!number.endsWith('@c.us')) { + number += '@c.us'; + } + + try { + return await this.pupPage.evaluate(async numberId => { + return window.WWebJS.getNumberId(numberId); + }, number); + } catch(_) { + return null; + } + } + /** * Create a new group * @param {string} name group title diff --git a/src/util/Injected.js b/src/util/Injected.js index 9460c03..0e15e90 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -34,6 +34,7 @@ exports.ExposeStore = (moduleRaidStr) => { exports.LoadUtils = () => { window.WWebJS = {}; + window.WWebJS.getNumberId = async (id) => { let result = await window.Store.Wap.queryExist(id); @@ -41,6 +42,7 @@ exports.LoadUtils = () => { throw 'The number provided is not a registered whatsapp user'; return result.jid; }; + window.WWebJS.sendSeen = async (chatId) => { let chat = window.Store.Chat.get(chatId); if (chat !== undefined) { @@ -50,6 +52,7 @@ exports.LoadUtils = () => { return false; }; + window.WWebJS.sendMessage = async (chat, content, options = {}) => { let attOptions = {}; if (options.attachment) {