mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
added querying for numbers
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
node_modules
|
||||
auth_info.json
|
||||
test_pvt.js
|
||||
|
||||
14
README.md
14
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.
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user