Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
// bot.js
// Simple WhatsApp bot using Baileys
const { default: makeWASocket, useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion } = require('@whiskeysockets/baileys') const qrcode = require('qrcode-terminal')
const fs = require('fs')
async function start() {
// simpan session di folder "session"
const { state, saveCreds } = await useMultiFileAuthState('session')
// dapatkan versi baileys (optional) untuk kompatibilitas
let { version, isLatest } = await fetchLatestBaileysVersion().catch(() => ({ version: [4, 0, 0], isLatest: false }))
const sock = makeWASocket({
auth: state,
printQRInTerminal: false, // kita handle QR sendiri
version
})
// print QR ke terminal ketika muncul⚠️ Koneksi tertutup:', reason)
sock.ev.on('connection.update', (update) => {
const { connection, qr, lastDisconnect } = update
if (qr) {
console.log('QR CODE TERSEDIA — scan dengan WhatsApp di device lain')
qrcode.generate(qr, { small: true })
}
if (connection === 'open') {
console.log('✅ Bot WhatsApp berhasil konek!')
}
if (connection === 'close') {
const reason = (lastDisconnect?.error)?.output?.statusCode || lastDisconnect?.error?.toString()
console.log('
// reconnect logic simple:
if (lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut) {
console.log('Mencoba reconnect dalam 5 detik...')
setTimeout(start, 5000)
} else {
console.log('Session ter-logout. Hapus folder session untuk login ulang.')
}
}
})
// simpan credentials saat ada update
sock.ev.on('creds.update', saveCreds)
// event pesan masuk
sock.ev.on('messages.upsert', async (m) => {
try {
const upsert = m
if (!upsert.messages) return
const msg = upsert.messages[0]
if (!msg.message || msg.key.fromMe) return
})
}
start().catch(err => console.error('Start failed:', err))