From 6629c08c70ce3bd219d7fbc46b7e546245bf23f8 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Fri, 3 Apr 2020 14:13:55 +0530 Subject: [PATCH] added querying for numbers --- .DS_Store | Bin 8196 -> 8196 bytes .gitignore | 1 + README.md | 14 +++++++- WhatsAppWeb.Recv.js | 81 ++++++++++++++++++++++++++++---------------- WhatsAppWeb.Send.js | 11 ++++++ WhatsAppWeb.js | 2 ++ 6 files changed, 78 insertions(+), 31 deletions(-) diff --git a/.DS_Store b/.DS_Store index a90326e12f711dc69365c37a9774aead44fddf9b..96e93ef97ed296b60e601bd5adb6b8385e3a97ca 100644 GIT binary patch delta 122 zcmZp1XmOa}&nU7nU^hRb$Yve^dsc03h7yKUhGHNY&rrZn2Bh^EvKWdzbMljua`KZH c7#IW?7?^~C^p4FAq7#`mvrD*;s;iF)0MqOnH~;_u delta 40 pcmZp1XmOa}&nUbxU^hRb@MazXd)Cd3qHRnQ8_YJdOSr%|#sCFj47LCO diff --git a/.gitignore b/.gitignore index 20444fa..bf1eb46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules auth_info.json +test_pvt.js diff --git a/README.md b/README.md index 4e16e3e..f378a78 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Baileys is super easy to use: 1. Install from npm using ``` npm install github:adiwajshing/Baileys ``` -2. Then import using +2. Then import in your code using ``` javascript const WhatsAppWeb = require('Baileys') ``` @@ -64,6 +64,18 @@ Baileys is super easy to use: client.login( authJSON ) ``` This will use the credentials to connect & log back in. No need to call ``` connect() ``` after calling this function +10. If you want to query whether a number is registered on WhatsApp, use: + ``` javascript + client.isOnWhatsApp ("[countrycode][some10digitnumber]@s.whatsapp.net", (exists, id) => { + if (exists) { + console.log(id + " is on WhatsApp") + } else { + console.log(id + " is not on WhatsApp :(") + } + }) + ``` + Of course, replace ``` [countrycode][some10digitnumber] ``` with an actual country code & number + Do check out test.js to see example usage of all these functions. diff --git a/WhatsAppWeb.Recv.js b/WhatsAppWeb.Recv.js index d58dd9b..6558fe1 100644 --- a/WhatsAppWeb.Recv.js +++ b/WhatsAppWeb.Recv.js @@ -27,6 +27,7 @@ module.exports = function(WhatsAppWeb) { let json if (data[0] === "[" || data[0] === "{") { // if the first character is a "[", then the data must just be plain JSON array or object json = JSON.parse( data ) // simply parse the JSON + console.log("JSON: " + data) } else if (this.status === Status.connected) { /* If the data recieved was not a JSON, then it must be an encrypted message. @@ -114,7 +115,7 @@ module.exports = function(WhatsAppWeb) { const id = json[0][2].key.remoteJid // get the ID whose chats we just processed this.clearUnreadMessages(id) // forward to the handler any any unread messages } - break + return case "response": // if it is the list of all the people the WhatsApp account has chats with if (json[1].type === "chat") { @@ -131,42 +132,62 @@ module.exports = function(WhatsAppWeb) { }) } - break + return default: break } - // if the recieved JSON wasn't an array, then we must have recieved a status for a request we made - // this would include creating new sessions & logging in - switch (json.status) { - case 200: // all good and we can procede to generate a QR code for new connection, or can now login given present auth info - - if (this.status === Status.creatingNewConnection){ // if we're trying to start a connection - if (this.authInfo.encKey && this.authInfo.macKey) { // if we have the info to restore a closed session - this.status = Status.loggingIn - // create the login request - const data = ["admin", "login", this.authInfo.clientToken, this.authInfo.serverToken, this.authInfo.clientID, "takeover"] - this.sendJSON( data ) - } else { - this.generateKeysForAuth(json.ref) + /* + if the recieved JSON wasn't an array, then we must have recieved a status for a request we made + this would include creating new sessions, logging in & queries + */ + // if we're connected and we had a pending query + if (this.status === Status.connected) { + if (json.status && this.queryCallbacks.length > 0) { + for (var i in this.queryCallbacks) { + if (this.queryCallbacks[i].queryJSON[1] === "exist") { + this.queryCallbacks[i].callback(json.status == 200, this.queryCallbacks[i].queryJSON[2]) + this.queryCallbacks.splice(i, 1) + break } - } else { + } + } + } else { + // if we're trying to establish a new connection or are trying to log in + switch (json.status) { + case 200: // all good and we can procede to generate a QR code for new connection, or can now login given present auth info - } - break - case 401: // if the phone was unpaired - this.close() - return this.gotError([json.status, "unpaired from phone", message]) - case 429: // request to login was denied, don't know why it happens - this.close() - return this.gotError([ json.status, "request denied, try reconnecting", message ]) - case 304: // request to generate a new key for a QR code was denied - console.log("reuse previous ref") - return this.gotError([ json.status, "request for new key denied", message ]) - default: - break + if (this.status === Status.creatingNewConnection){ // if we're trying to start a connection + if (this.authInfo.encKey && this.authInfo.macKey) { // if we have the info to restore a closed session + this.status = Status.loggingIn + // create the login request + const data = ["admin", "login", this.authInfo.clientToken, this.authInfo.serverToken, this.authInfo.clientID, "takeover"] + this.sendJSON( data ) + } else { + this.generateKeysForAuth(json.ref) + } + } else if (this.queryCallbacks.length > 0) { + for (var i in this.queryCallbacks) { + if (this.queryCallbacks[i].queryJSON[1] == "query") { + this.queryCallbacks[i].callback( ) + } + } + } + + break + case 401: // if the phone was unpaired + this.close() + return this.gotError([json.status, "unpaired from phone", message]) + case 429: // request to login was denied, don't know why it happens + this.close() + return this.gotError([ json.status, "request denied, try reconnecting", message ]) + case 304: // request to generate a new key for a QR code was denied + console.log("reuse previous ref") + return this.gotError([ json.status, "request for new key denied", message ]) + default: + break + } } - } } // shoot off notifications to the handler that new unread message are available diff --git a/WhatsAppWeb.Send.js b/WhatsAppWeb.Send.js index b9b0b03..53739b7 100644 --- a/WhatsAppWeb.Send.js +++ b/WhatsAppWeb.Send.js @@ -18,6 +18,17 @@ module.exports = function(WhatsAppWeb) { this.chats[jid].user.count = 0 // reset read count } } + // check if given number is registered on WhatsApp + WhatsAppWeb.prototype.isOnWhatsApp = function (jid, callback) { + const json = [ + "query", + "exist", + jid + ] + this.sendJSON(json) // send + + this.queryCallbacks.push({queryJSON: json, callback: callback}) + } // tell someone about your presence -- online, typing, offline etc. WhatsAppWeb.prototype.updatePresence = function (jid, type) { const json = [ diff --git a/WhatsAppWeb.js b/WhatsAppWeb.js index f3e6a95..1f46c98 100644 --- a/WhatsAppWeb.js +++ b/WhatsAppWeb.js @@ -34,6 +34,8 @@ class WhatsAppWeb { this.autoReconnect = true // reconnect automatically after an unexpected disconnect this.lastSeen = null // updated by sending a keep alive request to the server, and the server responds with our updated last seen + this.queryCallbacks = [] + this.encoder = new BinaryCoding.Encoder() this.decoder = new BinaryCoding.Decoder()