mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: correctly add log level to examples
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import P from 'pino'
|
|
||||||
import { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeInMemoryStore, makeWALegacySocket, useSingleFileLegacyAuthState } from '../src'
|
import { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeInMemoryStore, makeWALegacySocket, useSingleFileLegacyAuthState } from '../src'
|
||||||
|
import MAIN_LOGGER from '../src/Utils/logger'
|
||||||
|
|
||||||
|
const logger = MAIN_LOGGER.child({ })
|
||||||
|
logger.level = 'debug'
|
||||||
// the store maintains the data of the WA connection in memory
|
// the store maintains the data of the WA connection in memory
|
||||||
// can be written out to a file & read from it
|
// can be written out to a file & read from it
|
||||||
const store = makeInMemoryStore({ logger: P().child({ level: 'debug', stream: 'store' }) })
|
const store = makeInMemoryStore({ logger })
|
||||||
store.readFromFile('./baileys_store.json')
|
store.readFromFile('./baileys_store.json')
|
||||||
// save every 10s
|
// save every 10s
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
@@ -21,7 +23,7 @@ const startSock = async() => {
|
|||||||
|
|
||||||
const sock = makeWALegacySocket({
|
const sock = makeWALegacySocket({
|
||||||
version,
|
version,
|
||||||
logger: P({ level: 'debug' }),
|
logger,
|
||||||
printQRInTerminal: true,
|
printQRInTerminal: true,
|
||||||
auth: state
|
auth: state
|
||||||
})
|
})
|
||||||
@@ -38,19 +40,19 @@ const startSock = async() => {
|
|||||||
|
|
||||||
await sock.sendMessage(jid, msg)
|
await sock.sendMessage(jid, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
sock.ev.on('messages.upsert', async m => {
|
sock.ev.on('messages.upsert', async m => {
|
||||||
if(m.type === 'append' || m.type === 'notify') {
|
if(m.type === 'append' || m.type === 'notify') {
|
||||||
console.log(JSON.stringify(m, undefined, 2))
|
console.log(JSON.stringify(m, undefined, 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
const msg = m.messages[0]
|
const msg = m.messages[0]
|
||||||
if(!msg.key.fromMe && m.type === 'notify') {
|
if(!msg.key.fromMe && m.type === 'notify') {
|
||||||
console.log('replying to', m.messages[0].key.remoteJid)
|
console.log('replying to', m.messages[0].key.remoteJid)
|
||||||
await sock!.chatRead(msg.key, 1)
|
await sock!.chatRead(msg.key, 1)
|
||||||
await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid)
|
await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid)
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
sock.ev.on('messages.update', m => console.log(JSON.stringify(m, undefined, 2)))
|
sock.ev.on('messages.update', m => console.log(JSON.stringify(m, undefined, 2)))
|
||||||
@@ -68,7 +70,7 @@ const startSock = async() => {
|
|||||||
console.log('connection closed')
|
console.log('connection closed')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('connection update', update)
|
console.log('connection update', update)
|
||||||
})
|
})
|
||||||
// listen for when the auth credentials is updated
|
// listen for when the auth credentials is updated
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { Boom } from '@hapi/boom'
|
import { Boom } from '@hapi/boom'
|
||||||
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeInMemoryStore, toNumber, useSingleFileAuthState } from '../src'
|
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeInMemoryStore, useSingleFileAuthState } from '../src'
|
||||||
import logger from '../src/Utils/logger'
|
import MAIN_LOGGER from '../src/Utils/logger'
|
||||||
|
|
||||||
|
const logger = MAIN_LOGGER.child({ })
|
||||||
|
logger.level = 'trace'
|
||||||
|
|
||||||
// the store maintains the data of the WA connection in memory
|
// the store maintains the data of the WA connection in memory
|
||||||
// can be written out to a file & read from it
|
// can be written out to a file & read from it
|
||||||
const store = makeInMemoryStore({ logger: logger.child({ level: 'debug', stream: 'store' }) })
|
const store = makeInMemoryStore({ logger })
|
||||||
store.readFromFile('./baileys_store_multi.json')
|
store.readFromFile('./baileys_store_multi.json')
|
||||||
// save every 10s
|
// save every 10s
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
@@ -21,7 +24,7 @@ const startSock = async() => {
|
|||||||
|
|
||||||
const sock = makeWASocket({
|
const sock = makeWASocket({
|
||||||
version,
|
version,
|
||||||
logger: logger.child({ level: 'trace' }),
|
logger,
|
||||||
printQRInTerminal: true,
|
printQRInTerminal: true,
|
||||||
auth: state,
|
auth: state,
|
||||||
// implement to handle retries
|
// implement to handle retries
|
||||||
@@ -45,21 +48,21 @@ const startSock = async() => {
|
|||||||
|
|
||||||
await sock.sendMessage(jid, msg)
|
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('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('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('contacts.set', item => console.log(`recv ${item.contacts.length} contacts`))
|
||||||
|
|
||||||
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))
|
||||||
|
|
||||||
const msg = m.messages[0]
|
const msg = m.messages[0]
|
||||||
if(!msg.key.fromMe && m.type === 'notify') {
|
if(!msg.key.fromMe && m.type === 'notify') {
|
||||||
console.log('replying to', m.messages[0].key.remoteJid)
|
console.log('replying to', m.messages[0].key.remoteJid)
|
||||||
await sock!.sendReadReceipt(msg.key.remoteJid, msg.key.participant, [msg.key.id])
|
await sock!.sendReadReceipt(msg.key.remoteJid, msg.key.participant, [msg.key.id])
|
||||||
await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid)
|
await sendMessageWTyping({ text: 'Hello there!' }, msg.key.remoteJid)
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
sock.ev.on('messages.update', m => console.log(m))
|
sock.ev.on('messages.update', m => console.log(m))
|
||||||
@@ -78,7 +81,7 @@ const startSock = async() => {
|
|||||||
console.log('Connection closed. You are logged out.')
|
console.log('Connection closed. You are logged out.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('connection update', update)
|
console.log('connection update', update)
|
||||||
})
|
})
|
||||||
// listen for when the auth credentials is updated
|
// listen for when the auth credentials is updated
|
||||||
|
|||||||
Reference in New Issue
Block a user