mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: cleaner example file
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
import { readFileSync, writeFileSync } from "fs"
|
import { readFileSync, writeFileSync } from "fs"
|
||||||
import P from "pino"
|
import P from "pino"
|
||||||
import { Boom } from "@hapi/boom"
|
import { Boom } from "@hapi/boom"
|
||||||
import makeWASocket, { WASocket, AuthenticationState, DisconnectReason, AnyMessageContent, BufferJSON, initInMemoryKeyStore, delay } from '../src'
|
import makeWASocket, { AuthenticationState, DisconnectReason, AnyMessageContent, BufferJSON, initInMemoryKeyStore, delay } from '../src'
|
||||||
|
|
||||||
(async() => {
|
|
||||||
let sock: WASocket | undefined = undefined
|
|
||||||
// load authentication state from a file
|
// load authentication state from a file
|
||||||
const loadState = () => {
|
const loadState = () => {
|
||||||
let state: AuthenticationState | undefined = undefined
|
let state: AuthenticationState | undefined = undefined
|
||||||
@@ -23,9 +21,8 @@ import makeWASocket, { WASocket, AuthenticationState, DisconnectReason, AnyMessa
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
// save the authentication state to a file
|
// save the authentication state to a file
|
||||||
const saveState = (state?: any) => {
|
const saveState = (state: any | undefined) => {
|
||||||
console.log('saving pre-keys')
|
console.log('saving auth state')
|
||||||
state = state || sock?.authState
|
|
||||||
writeFileSync(
|
writeFileSync(
|
||||||
'./auth_info_multi.json',
|
'./auth_info_multi.json',
|
||||||
// BufferJSON replacer utility saves buffers nicely
|
// BufferJSON replacer utility saves buffers nicely
|
||||||
@@ -35,11 +32,24 @@ import makeWASocket, { WASocket, AuthenticationState, DisconnectReason, AnyMessa
|
|||||||
|
|
||||||
// start a connection
|
// start a connection
|
||||||
const startSock = () => {
|
const startSock = () => {
|
||||||
let sock = makeWASocket({
|
const sock = makeWASocket({
|
||||||
logger: P({ level: 'trace' }),
|
logger: P({ level: 'trace' }),
|
||||||
printQRInTerminal: true,
|
printQRInTerminal: true,
|
||||||
auth: loadState()
|
auth: loadState()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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('messages.upsert', async m => {
|
sock.ev.on('messages.upsert', async m => {
|
||||||
console.log(JSON.stringify(m, undefined, 2))
|
console.log(JSON.stringify(m, undefined, 2))
|
||||||
|
|
||||||
@@ -62,31 +72,19 @@ import makeWASocket, { WASocket, AuthenticationState, DisconnectReason, AnyMessa
|
|||||||
if(connection === 'close') {
|
if(connection === 'close') {
|
||||||
// reconnect if not logged out
|
// reconnect if not logged out
|
||||||
if((lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
if((lastDisconnect.error as Boom)?.output?.statusCode !== DisconnectReason.loggedOut) {
|
||||||
sock = startSock()
|
startSock()
|
||||||
} else {
|
} else {
|
||||||
console.log('connection closed')
|
console.log('connection closed')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('connection update', update)
|
console.log('connection update', update)
|
||||||
})
|
})
|
||||||
// listen for when the auth state is updated
|
// 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
|
// it is imperative you save this data, it affects the signing keys you need to have conversations
|
||||||
sock.ev.on('auth-state.update', () => saveState())
|
sock.ev.on('auth-state.update', () => saveState(sock.authState))
|
||||||
|
|
||||||
return sock
|
return sock
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendMessageWTyping = async(msg: AnyMessageContent, jid: string) => {
|
startSock()
|
||||||
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 = startSock()
|
|
||||||
})()
|
|
||||||
Reference in New Issue
Block a user