mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
1. the store works as a temporary store for connection data such as chats, messages & contacts 2. the store is primarily meant to illustrate the usage of the event emitter as a way to construct the state of the connection. This will likely be very inefficient to perform well at scale 3. the store is meant to be a quick way to have some visibility of data while testing 4. the store works for both legacy & MD connections
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
import P from "pino"
|
|
import { Boom } from "@hapi/boom"
|
|
import makeWASocket, { DisconnectReason, AnyMessageContent, delay, useSingleFileAuthState, makeInMemoryStore } from '../src'
|
|
|
|
// the store maintains the data of the WA connection in memory
|
|
// can be written out to a file & read from it
|
|
const store = makeInMemoryStore({ logger: P().child({ level: 'debug', stream: 'store' }) })
|
|
store.readFromFile('./baileys_store.json')
|
|
|
|
setInterval(() => {
|
|
store.writeToFile('./baileys_store.json')
|
|
}, 10_000)
|
|
|
|
const { state, saveState } = useSingleFileAuthState('./auth_info_multi.json')
|
|
|
|
// start a connection
|
|
const startSock = () => {
|
|
|
|
const sock = makeWASocket({
|
|
logger: P({ level: 'trace' }),
|
|
printQRInTerminal: true,
|
|
auth: state,
|
|
// implement to handle retries
|
|
getMessage: async key => {
|
|
return {
|
|
conversation: 'hello'
|
|
}
|
|
}
|
|
})
|
|
|
|
store.bind(sock.ev)
|
|
|
|
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)
|
|
|
|
await sock.sendMessage(jid, msg)
|
|
}
|
|
|
|
sock.ev.on('chats.set', item => console.log(`recv ${item.chats.length} chats (is latest: ${item.isLatest})`))
|
|
sock.ev.on('messages.set', item => console.log(`recv ${item.messages.length} messages (is latest: ${item.isLatest})`))
|
|
sock.ev.on('contacts.set', item => console.log(`recv ${item.contacts.length} contacts`))
|
|
|
|
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 sendMessageWTyping({ text: 'Hello there!' }, msg.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))
|
|
sock.ev.on('contacts.upsert', m => console.log(m))
|
|
|
|
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) {
|
|
startSock()
|
|
} else {
|
|
console.log('connection closed')
|
|
}
|
|
}
|
|
|
|
console.log('connection update', update)
|
|
})
|
|
// listen for when the auth credentials is updated
|
|
sock.ev.on('creds.update', saveState)
|
|
|
|
return sock
|
|
}
|
|
|
|
startSock() |