From 91cfbd1412fc0cf38de737c1218aacc921fd1774 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Sat, 9 May 2020 18:31:58 +0530 Subject: [PATCH] Added callback for delivery & read receipts --- .DS_Store | Bin 10244 -> 10244 bytes ConversationExtract.js | 24 +++++++++++++++++++----- README.md | 3 +++ WhatsAppWeb.Recv.js | 23 ++++++++++++++++++++++- WhatsAppWeb.Session.js | 2 +- WhatsAppWeb.js | 10 ++++++++-- example/example.js | 4 ++++ 7 files changed, 57 insertions(+), 9 deletions(-) diff --git a/.DS_Store b/.DS_Store index f5e2a23c443554aed41352813922b351d48a08ca..09f4d9e8b77043a737e089bb63fc3afa9db7056c 100644 GIT binary patch delta 162 zcmZn(XbG4Q$z8-y$q>(w%#hEJvaxU)`@{y`&FmZ;9FsQ-NN-LP$Y(R;0!o!KlrR*4 zNj;#_Vumu$oc!dZoctsP1_l8jwq{^p<`@{y`&FmZ;9Kw7Ii43I-B@7u1a21o~#3h)R i&QG=y(Aumekk7XHte6q!W_E>NESrytTww%q^%()_4j4}W diff --git a/ConversationExtract.js b/ConversationExtract.js index b02399c..27fb15d 100644 --- a/ConversationExtract.js +++ b/ConversationExtract.js @@ -5,7 +5,7 @@ const fs = require("fs") * Extract all your WhatsApp conversations & save them to a file * produceAnonData => should the Id of the chat be recorded * */ -function extractChats (authCreds, outputFile, produceAnonData) { +function extractChats (authCreds, outputFile, produceAnonData=false, offset=null) { let client = new WhatsAppWeb() // instantiate an instance if (authCreds) { // login if creds are present client.login(authCreds) @@ -14,13 +14,27 @@ function extractChats (authCreds, outputFile, produceAnonData) { } // internal extract function const extract = function () { - fs.writeFileSync(outputFile, "chat,input,output\n") // write header to file - let rows = 0 let chats = Object.keys(client.chats) + let encounteredOffset + if (offset) { + encounteredOffset = false + } else { + encounteredOffset = true + fs.writeFileSync(outputFile, "chat,input,output\n") // write header to file + } const extractChat = function (index) { const id = chats[index] + if (id.includes("g.us") || !encounteredOffset) { // skip groups + if (id === offset) { + encounteredOffset = true + } + if (index+1 < chats.length) { + return extractChat(index+1) + } + return + } console.log("extracting for " + id + "...") var curInput = "" @@ -92,5 +106,5 @@ function extractChats (authCreds, outputFile, produceAnonData) { console.log("got error: " + error) } } -let creds = JSON.parse(fs.readFileSync("auth_info.json")) -extractChats(creds, "output.csv", true) \ No newline at end of file +let creds = null//JSON.parse(fs.readFileSync("auth_info.json")) +extractChats(creds, "output.csv", true, "919820038582@s.whatsapp.net") \ No newline at end of file diff --git a/README.md b/README.md index 1f2a37a..f38ae38 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ Baileys is super easy to use: ``` javascript client.handlers.presenceUpdated = (id, presence) => { /* called when you recieve an update on someone's presence */ } ``` + ``` javascript + client.handlers.onMessageStatusChanged = (id, messageID, status) => { /* called when your message gets delivered or read */ } + ``` ``` javascript client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ } ``` diff --git a/WhatsAppWeb.Recv.js b/WhatsAppWeb.Recv.js index c877e3c..5cdd16c 100644 --- a/WhatsAppWeb.Recv.js +++ b/WhatsAppWeb.Recv.js @@ -84,7 +84,9 @@ module.exports = function(WhatsAppWeb) { this is when some action was taken on a chat or that we recieve a message. json[1] tells us more about the message, it can be null */ + //console.log (JSON.stringify (json)) if (!json[1]) { // if json[1] is null + json = json[2][0] // set json to the first element in json[2]; it contains the relevant part if (json[0] === "read") { // if one marked a chat as read or unread on the phone @@ -98,7 +100,26 @@ module.exports = function(WhatsAppWeb) { } } else if (json[1].add === "relay") { // if we just recieved a new message sent to us - this.onNewMessage( json[2][0][2] ) // handle this new message + json = json[2][0] + if (json[0] === "received") { + if (json[1].owner) { + let type + switch (json[1].type) { + case "read": + type = WhatsAppWeb.MessageStatus.read + break + case "message": + type = WhatsAppWeb.MessageStatus.received + break + default: + type = json[1].type + break + } + this.handlers.onMessageStatusChanged (json[1].jid, json[1].index, type) + } + } else if (json[1] === "message") { + this.onNewMessage( json[2][0][2] ) // handle this new message + } } else if (json[1].add === "before" || json[1].add === "last") { /* if we're recieving a full chat log diff --git a/WhatsAppWeb.Session.js b/WhatsAppWeb.Session.js index e2168a4..6f2deaf 100644 --- a/WhatsAppWeb.Session.js +++ b/WhatsAppWeb.Session.js @@ -175,7 +175,7 @@ module.exports = function (WhatsAppWeb) { WhatsAppWeb.prototype.disconnect = function () { if (this.status === Status.connected) { this.conn.send('goodbye,["admin","Conn","disconnect"]', null, () => { - this.conn.close() + this.close() if (this.handlers.onDisconnect) this.handlers.onDisconnect() }) diff --git a/WhatsAppWeb.js b/WhatsAppWeb.js index bbe3c33..1c11dde 100644 --- a/WhatsAppWeb.js +++ b/WhatsAppWeb.js @@ -21,7 +21,12 @@ class WhatsAppWeb { recording: "recording", // "recording..." paused: "paused" // I have no clue } - + // set of statuses visible to other people; see updatePresence() in WhatsAppWeb.Send + static MessageStatus = { + sent: "sent", + received: "received", + read: "read" + } // set of message types that are supported by the library static MessageType = { text: "conversation", @@ -50,7 +55,8 @@ class WhatsAppWeb { presenceUpdated: null, onDisconnect: null, onUnreadMessage: null, - gotContact: null + gotContact: null, + onMessageStatusChanged: null } this.callbacks = {} diff --git a/example/example.js b/example/example.js index 95834b3..bbfe5cf 100644 --- a/example/example.js +++ b/example/example.js @@ -23,6 +23,10 @@ client.handlers.onConnected = () => { client.handlers.presenceUpdated = (id, type) => { console.log("presence of " + id + " is " + type) } +// called when your message gets delivered or read +client.handlers.onMessageStatusChanged = (id, messageID, status) => { + console.log(id + " acknowledged message '" + messageID + "' status as " + status) +} // 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