Added callback for delivery & read receipts

This commit is contained in:
Adhiraj
2020-05-09 18:31:58 +05:30
parent 266e695b3d
commit 91cfbd1412
7 changed files with 57 additions and 9 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -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)
let creds = null//JSON.parse(fs.readFileSync("auth_info.json"))
extractChats(creds, "output.csv", true, "919820038582@s.whatsapp.net")

View File

@@ -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 */ }
```

View File

@@ -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

View File

@@ -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()
})

View File

@@ -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 = {}

View File

@@ -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