Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
feat: Get registered ID for a given phone number (pedroslopez#483)
Browse files Browse the repository at this point in the history
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 <pedroslopez@me.com>
  • Loading branch information
lucasrosa90 and pedroslopez committed Dec 29, 2020
1 parent 805bfd7 commit f9e0164
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ declare namespace WAWebJS {
/** Check if a given ID is registered in whatsapp */
isRegisteredUser(contactId: string): Promise<boolean>

/** Get the registered WhatsApp ID for a number. Returns null if the number is not registered on WhatsApp. */
getNumberId(number: string): Promise<ContactId?>

/**
* Mutes the Chat until a specified date
* @param chatId ID of the chat that will be muted
Expand Down
20 changes: 20 additions & 0 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object|null>}
*/
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
Expand Down
3 changes: 3 additions & 0 deletions src/util/Injected.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ exports.ExposeStore = (moduleRaidStr) => {

exports.LoadUtils = () => {
window.WWebJS = {};

window.WWebJS.getNumberId = async (id) => {

let result = await window.Store.Wap.queryExist(id);
if (result.jid === undefined)
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) {
Expand All @@ -50,6 +52,7 @@ exports.LoadUtils = () => {
return false;

};

window.WWebJS.sendMessage = async (chat, content, options = {}) => {
let attOptions = {};
if (options.attachment) {
Expand Down

0 comments on commit f9e0164

Please sign in to comment.