mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Added callback for delivery & read receipts
This commit is contained in:
@@ -5,7 +5,7 @@ const fs = require("fs")
|
|||||||
* Extract all your WhatsApp conversations & save them to a file
|
* Extract all your WhatsApp conversations & save them to a file
|
||||||
* produceAnonData => should the Id of the chat be recorded
|
* 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
|
let client = new WhatsAppWeb() // instantiate an instance
|
||||||
if (authCreds) { // login if creds are present
|
if (authCreds) { // login if creds are present
|
||||||
client.login(authCreds)
|
client.login(authCreds)
|
||||||
@@ -14,13 +14,27 @@ function extractChats (authCreds, outputFile, produceAnonData) {
|
|||||||
}
|
}
|
||||||
// internal extract function
|
// internal extract function
|
||||||
const extract = function () {
|
const extract = function () {
|
||||||
fs.writeFileSync(outputFile, "chat,input,output\n") // write header to file
|
|
||||||
|
|
||||||
let rows = 0
|
let rows = 0
|
||||||
let chats = Object.keys(client.chats)
|
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 extractChat = function (index) {
|
||||||
const id = chats[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 + "...")
|
console.log("extracting for " + id + "...")
|
||||||
|
|
||||||
var curInput = ""
|
var curInput = ""
|
||||||
@@ -92,5 +106,5 @@ function extractChats (authCreds, outputFile, produceAnonData) {
|
|||||||
console.log("got error: " + error)
|
console.log("got error: " + error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let creds = JSON.parse(fs.readFileSync("auth_info.json"))
|
let creds = null//JSON.parse(fs.readFileSync("auth_info.json"))
|
||||||
extractChats(creds, "output.csv", true)
|
extractChats(creds, "output.csv", true, "919820038582@s.whatsapp.net")
|
||||||
@@ -33,6 +33,9 @@ Baileys is super easy to use:
|
|||||||
``` javascript
|
``` javascript
|
||||||
client.handlers.presenceUpdated = (id, presence) => { /* called when you recieve an update on someone's presence */ }
|
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
|
``` javascript
|
||||||
client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ }
|
client.handlers.onDisconnect = () => { /* called when internet gets disconnected */ }
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -84,7 +84,9 @@ module.exports = function(WhatsAppWeb) {
|
|||||||
this is when some action was taken on a chat or that we recieve a message.
|
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
|
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
|
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
|
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
|
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
|
} 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") {
|
} else if (json[1].add === "before" || json[1].add === "last") {
|
||||||
/*
|
/*
|
||||||
if we're recieving a full chat log
|
if we're recieving a full chat log
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ module.exports = function (WhatsAppWeb) {
|
|||||||
WhatsAppWeb.prototype.disconnect = function () {
|
WhatsAppWeb.prototype.disconnect = function () {
|
||||||
if (this.status === Status.connected) {
|
if (this.status === Status.connected) {
|
||||||
this.conn.send('goodbye,["admin","Conn","disconnect"]', null, () => {
|
this.conn.send('goodbye,["admin","Conn","disconnect"]', null, () => {
|
||||||
this.conn.close()
|
this.close()
|
||||||
if (this.handlers.onDisconnect)
|
if (this.handlers.onDisconnect)
|
||||||
this.handlers.onDisconnect()
|
this.handlers.onDisconnect()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -21,7 +21,12 @@ class WhatsAppWeb {
|
|||||||
recording: "recording", // "recording..."
|
recording: "recording", // "recording..."
|
||||||
paused: "paused" // I have no clue
|
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
|
// set of message types that are supported by the library
|
||||||
static MessageType = {
|
static MessageType = {
|
||||||
text: "conversation",
|
text: "conversation",
|
||||||
@@ -50,7 +55,8 @@ class WhatsAppWeb {
|
|||||||
presenceUpdated: null,
|
presenceUpdated: null,
|
||||||
onDisconnect: null,
|
onDisconnect: null,
|
||||||
onUnreadMessage: null,
|
onUnreadMessage: null,
|
||||||
gotContact: null
|
gotContact: null,
|
||||||
|
onMessageStatusChanged: null
|
||||||
}
|
}
|
||||||
|
|
||||||
this.callbacks = {}
|
this.callbacks = {}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ client.handlers.onConnected = () => {
|
|||||||
client.handlers.presenceUpdated = (id, type) => {
|
client.handlers.presenceUpdated = (id, type) => {
|
||||||
console.log("presence of " + id + " is " + 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
|
// called when you have a pending unread message or recieve a new message
|
||||||
client.handlers.onUnreadMessage = (m) => {
|
client.handlers.onUnreadMessage = (m) => {
|
||||||
// console.log("recieved message: " + JSON.stringify(m)) // uncomment to see what the raw message looks like
|
// console.log("recieved message: " + JSON.stringify(m)) // uncomment to see what the raw message looks like
|
||||||
|
|||||||
Reference in New Issue
Block a user