// index.js import { makeWASocket, useMultiFileAuthState, DisconnectReason } from 'baileys'; import { handleMessage } from './bot/autoreply.js'; async function connectToWhatsApp() { const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys'); const sock = makeWASocket({ auth: state, printQRInTerminal: false }); // Connection updates sock.ev.on('connection.update', async (update) => { const { connection, lastDisconnect } = update; if (connection === 'close') { const shouldReconnect = lastDisconnect?.error?.output?.statusCode !== DisconnectReason.loggedOut; if (shouldReconnect) connectToWhatsApp(); } else if (connection === 'open') { console.log('WhatsApp connected!'); } }); // Request pairing code if not registered setTimeout(async () => { if (!state.creds?.registered) { try { const number = '916234859346'; // Your WhatsApp number console.log(`🔄 Requesting pairing code for ${number}`); const code = await sock.requestPairingCode(number); console.log(`👍 Pairing Code: ${code}`); console.log('1. Go to WhatsApp on your phone: Settings > Linked Devices > Add Device'); console.log('2. Enter the code above into the app to pair.'); } catch (error) { console.error('Error requesting pairing code:', error); } } }, 5000); // Handle incoming messages sock.ev.on('messages.upsert', (event) => handleMessage(event, sock)); sock.ev.on('creds.update', saveCreds); return sock; } connectToWhatsApp() .then(() => console.log('Bot is running...')) .catch(console.error);