Created
June 3, 2022 02:15
-
-
Save Iucasmaia/de2f350a0ab0d5c0a381203070817c37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const { Boom } = require('@hapi/boom') | |
const { | |
default: makeWASocket, | |
AnyMessageContent, | |
delay, | |
DisconnectReason, | |
fetchLatestBaileysVersion, | |
useMultiFileAuthState | |
} = require('@adiwajshing/baileys') | |
const P = require('pino') | |
const MAIN_LOGGER = P({ timestamp: () => `,"time":"${new Date().toJSON()}"` }) | |
const logger = MAIN_LOGGER.child({ }) | |
logger.level = 'error' | |
const startSock = async() => { | |
global.callUsers = [] | |
const { state, saveCreds } = await useMultiFileAuthState('session') | |
const { version, isLatest } = await fetchLatestBaileysVersion() | |
console.log(`WhatsApp - v${version.join('.')} ${isLatest ? '- Last version' : ''}`) | |
const sock = makeWASocket({ | |
version, | |
logger, | |
printQRInTerminal: true, | |
auth: state, | |
}) | |
sock.ev.on('messages.upsert', async 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) | |
await sock.sendReadReceipt(msg.key.remoteJid, msg.key.participant, [msg.key.id]) | |
await sock.sendMessage(msg.key.remoteJid, {text: 'test!'}) | |
} | |
}) | |
sock.ev.on('connection.update', (update) => { | |
const { connection, lastDisconnect } = update | |
if(connection === 'close') { | |
if(new Boom(lastDisconnect.error)?.output?.statusCode !== DisconnectReason.loggedOut) { | |
startSock() | |
} else { | |
console.log('Connection closed.') | |
} | |
} | |
if (update.connection !== undefined) { | |
console.log(`[Connection] ${update.connection}.`) | |
} | |
}) | |
sock.ev.on('creds.update', saveCreds) | |
return sock | |
} | |
startSock() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment