support for custom queries + presence update handler

This commit is contained in:
Adhiraj
2020-04-17 18:34:36 +05:30
parent 9ea4b0b381
commit 2cd560c430
5 changed files with 20 additions and 1 deletions

View File

@@ -27,6 +27,9 @@ Baileys is super easy to use:
``` javascript
client.handlers.onError = (error) => { /* called when there was an error */ }
```
``` javascript
client.handlers.presenceUpdated = (id, presence) => { /* called when you recieve an update on someone's presence */ }
```
``` javascript
client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ }
```
@@ -133,7 +136,7 @@ Baileys is super easy to use:
}
})
```
Of course, replace ``` [countrycode][some10digitnumber] ``` with an actual country code & number. ``` isOnWhatsApp ``` returns a promise.
Of course, replace ``` [countrycode][some10digitnumber] ``` with an actual country code & number.
Do check out & run [example.js](example/example.js) to see example usage of all these functions.

View File

@@ -140,6 +140,11 @@ module.exports = function(WhatsAppWeb) {
}
return
case "Presence":
if (this.handlers.presenceUpdated) {
this.handlers.presenceUpdated(json[1].id, json[1].type)
}
return
default:
break
}
@@ -155,6 +160,8 @@ module.exports = function(WhatsAppWeb) {
q.callback(json.status == 200, q.queryJSON[2])
} else if (q.queryJSON[1] === "mediaConn") {
q.callback(json.media_conn)
} else {
q.callback(json)
}
delete this.queryCallbacks[messageTag]
}

View File

@@ -37,6 +37,10 @@ module.exports = function(WhatsAppWeb) {
]
this.sendBinary(json, [10, 128])
}
// check the presence status of a given jid
WhatsAppWeb.prototype.requestPresenceUpdate = function (jid) {
this.query(["action","presence","subscribe",jid])
}
// send a text message to someone, optionally you can provide a quoted message & the timestamp for the message
WhatsAppWeb.prototype.sendTextMessage = function (id, txt, quoted=null, timestamp=null) {
let message

BIN
example/.DS_Store vendored

Binary file not shown.

View File

@@ -19,6 +19,10 @@ client.handlers.onConnected = () => {
Despite the convenience, be careful with this file
*/
}
// called when someone's presence is updated
client.handlers.presenceUpdated = (id, type) => {
console.log("presence of " + id + " is " + type)
}
// called when you have a pending unread message or recieve a new message
client.handlers.onUnreadMessage = (m) => {
// console.log("recieved message: " + JSON.stringify(m)) // uncomment to see what the raw message looks like
@@ -58,6 +62,7 @@ client.handlers.onUnreadMessage = (m) => {
}
}, 4*1000) // send after 4 seconds
}
// called if an error occurs
client.handlers.onError = (err) => console.log(err)
client.handlers.onDisconnect = () => { /* internet got disconnected, save chats here or whatever; will reconnect automatically */ }