From 1840826e2b85b217287bd72d708497d18fbddaea Mon Sep 17 00:00:00 2001 From: sahalMoidu Date: Mon, 17 Feb 2020 18:40:07 +0530 Subject: [PATCH] feat : Check if the given number is a whatsapp number (#54) * feat : Check if the given number is a whatsapp number - Avoids sending message to non whatsapp number - Gets the id of the user from whatsapp server rather than generating on client --- package-lock.json | 2 +- src/Client.js | 16 ++++++++++------ src/util/Injected.js | 6 ++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6cf001d..a1225e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "whatsapp-web.js", - "version": "0.3.2-post", + "version": "1.0.2-post", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/Client.js b/src/Client.js index e139ae1..9223ae6 100644 --- a/src/Client.js +++ b/src/Client.js @@ -277,12 +277,16 @@ class Client extends EventEmitter { let msg; if (!chat) { // The chat is not available in the previously chatted list - //todo : Check if the number is a whatsapp enabled. Whatsapp web sends query exists via ws. - chat = window.Store.Chat.models[0]; //get the topmost chat object and assign the new chatId to it - let originalChatObjId = chat.id; - chat.id = typeof originalChatObjId === 'string' ? chatId : new window.Store.UserConstructor(chatId, { intentionallyUsePrivateConstructor: true }); - msg = await window.WWebJS.sendMessage(chat, message, options); - chat.id = originalChatObjId; //replace the chat with its original id + let newChatId = await window.WWebJS.getNumberId(chatId); + if (newChatId) { + //get the topmost chat object and assign the new chatId to it . + //This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does + let chat = window.Store.Chat.models[0]; + let originalChatObjId = chat.id; + chat.id = newChatId; + msg = await window.WWebJS.sendMessage(chat, message, options); + chat.id = originalChatObjId; //replace the chat with its original id + } } else msg = await window.WWebJS.sendMessage(chat, message, options); diff --git a/src/util/Injected.js b/src/util/Injected.js index 07e303c..75ec08e 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -25,7 +25,13 @@ 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.sendMessage = async (chat, content, options = {}) => { let attOptions = {}; if (options.attachment) {