From 2cd560c4309be4bb41828d1479c3f728465d0c39 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Fri, 17 Apr 2020 18:34:36 +0530 Subject: [PATCH] support for custom queries + presence update handler --- README.md | 5 ++++- WhatsAppWeb.Recv.js | 7 +++++++ WhatsAppWeb.Send.js | 4 ++++ example/.DS_Store | Bin 6148 -> 6148 bytes example/example.js | 5 +++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8cb5948..78d4e8a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/WhatsAppWeb.Recv.js b/WhatsAppWeb.Recv.js index 7f9eed5..89a5322 100644 --- a/WhatsAppWeb.Recv.js +++ b/WhatsAppWeb.Recv.js @@ -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] } diff --git a/WhatsAppWeb.Send.js b/WhatsAppWeb.Send.js index bddba87..1134071 100644 --- a/WhatsAppWeb.Send.js +++ b/WhatsAppWeb.Send.js @@ -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 diff --git a/example/.DS_Store b/example/.DS_Store index b340688583367ddb830870602e4f83f42beddbcb..87af765292eaa0821674fbe05c9f262b45790db1 100644 GIT binary patch delta 56 zcmZoMXfc=&$z8yZ!;s3L$B@NPJn^IYWCIZvelDOu1w$f3E(2I{;u+P6{U#f~39xTw I=lIJH0J|9wlmGw# delta 54 zcmZoMXfc=&$(_lN#-PWL%TT~zGMSN4eX@ZF%f!Pboa_uG45 { 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 */ }