finalize multi-device

This commit is contained in:
Adhiraj Singh
2021-09-15 13:40:02 +05:30
parent 9cba28e891
commit f267f27ada
82 changed files with 35228 additions and 10644 deletions

View File

@@ -1,97 +0,0 @@
const WhatsAppWeb = require("../WhatsAppWeb")
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=false, offset=null) {
let client = new WhatsAppWeb() // instantiate an instance
// internal extract function
const extract = function () {
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 = ""
var curOutput = ""
var lastMessage
return client.loadEntireConversation (id, m => {
var text
if (!m.message) { // if message not present, return
return
} else if (m.message.conversation) { // if its a plain text message
text = m.message.conversation
} else if (m.message.extendedTextMessage && m.message.extendedTextMessage.contextInfo) { // if its a reply to a previous message
const mText = m.message.extendedTextMessage.text
const quotedMessage = m.message.extendedTextMessage.contextInfo.quotedMessage
// if it's like a '.' and the quoted message has no text, then just forget it
if (mText.length <= 2 && !quotedMessage.conversation) {
return
}
// if somebody sent like a '.', then the text should be the quoted message
if (mText.length <= 2) {
text = quotedMessage.conversation
} else { // otherwise just use this text
text = mText
}
} else {
return
}
// if the person who sent the message has switched, flush the row
if (lastMessage && !m.key.fromMe && lastMessage.key.fromMe) {
let row = "" + (produceAnonData ? "" : id) + ",\"" + curInput + "\",\"" + curOutput + "\"\n"
fs.appendFileSync (outputFile, row)
rows += 1
curInput = ""
curOutput = ""
}
if (m.key.fromMe) {
curOutput += curOutput === "" ? text : ("\n"+text)
} else {
curInput += curInput === "" ? text : ("\n"+text)
}
lastMessage = m
}, 50, false) // load from the start, in chunks of 50
.then (() => console.log("finished extraction for " + id))
.then (() => {
if (index+1 < chats.length) {
return extractChat(index+1)
}
})
}
extractChat(0)
.then (() => {
console.log("extracted all; total " + rows + " rows")
client.logout ()
})
}
client.connect (authCreds)
.then (() => extract())
.catch (err => console.log("got error: " + error))
}
let creds = null//JSON.parse(fs.readFileSync("auth_info.json"))
extractChats(creds, "output.csv")

View File

@@ -1,13 +1,84 @@
import makeConnection from '../src'
import * as fs from 'fs'
import { readFileSync, writeFileSync } from "fs"
import P from "pino"
import { Boom } from "@hapi/boom"
import makeWASocket, { WASocket, AuthenticationState, DisconnectReason, AnyMessageContent, BufferJSON, initInMemoryKeyStore, delay } from '../src'
async function example() {
const conn = makeConnection({
credentials: './auth_info.json'
})
conn.ev.on('connection.update', state => {
console.log(state)
})
}
(async() => {
let sock: WASocket | undefined = undefined
// load authentication state from a file
const loadState = () => {
let state: AuthenticationState | undefined = undefined
try {
const value = JSON.parse(
readFileSync('./auth_info_multi.json', { encoding: 'utf-8' }),
BufferJSON.reviver
)
state = {
creds: value.creds,
// stores pre-keys, session & other keys in a JSON object
// we deserialize it here
keys: initInMemoryKeyStore(value.keys)
}
} catch{ }
return state
}
// save the authentication state to a file
const saveState = (state?: any) => {
console.log('saving pre-keys')
state = state || sock?.authState
writeFileSync(
'./auth_info_multi.json',
// BufferJSON replacer utility saves buffers nicely
JSON.stringify(state, BufferJSON.replacer, 2)
)
}
// start a connection
const startSock = () => {
const sock = makeWASocket({
logger: P({ level: 'trace' }),
auth: loadState()
})
sock.ev.on('messages.upsert', m => {
console.log(JSON.stringify(m, undefined, 2))
const msg = m.messages[0]
if(!msg.key.fromMe && m.type === 'notify') {
console.log('replying to', m.messages[0].key.remoteJid)
sendMessageWTyping({ text: 'Hello there!' }, m.messages[0].key.remoteJid!)
}
})
sock.ev.on('messages.update', m => console.log(m))
sock.ev.on('presence.update', m => console.log(m))
sock.ev.on('chats.update', m => console.log(m))
return sock
}
example().catch((err) => console.log(`encountered error`, err))
const sendMessageWTyping = async(msg: AnyMessageContent, jid: string) => {
await sock.presenceSubscribe(jid)
await delay(500)
await sock.sendPresenceUpdate('composing', jid)
await delay(2000)
await sock.sendPresenceUpdate('paused', jid)
}
sock = startSock()
sock.ev.on('connection.update', (update) => {
const { connection, lastDisconnect } = update
if(connection === 'close') {
// reconnect if not logged out
if((lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut) {
sock = startSock()
} else {
console.log('connection closed')
}
}
console.log('connection update', update)
})
// listen for when the auth state is updated
// it is imperative you save this data, it affects the signing keys you need to have conversations
sock.ev.on('auth-state.update', () => saveState())
})()