Spaces:
Paused
Paused
| // src/connection.js | |
| const makeWASocket = require('@whiskeysockets/baileys').default; | |
| const { DisconnectReason, useMultiFileAuthState, fetchLatestBaileysVersion } = require('@whiskeysockets/baileys'); | |
| const logger = require('./logger'); | |
| const config = require('./config'); // Import config but botJid setting/clearing removed | |
| const whatsapp = require('./services/whatsappService'); // Import service | |
| const { processMessage } = require('./handlers/messageHandler'); // Import main handler | |
| const path = require('path'); // Path is still needed for useMultiFileAuthState path construction | |
| /** | |
| * Establishes the WhatsApp connection using Baileys, initializes services, | |
| * and sets up event listeners. Filters groups and channels. (Original Refactored Version) | |
| */ | |
| async function connectWhatsApp() { | |
| // Define auth folder path relative to src/ directory | |
| const authFolderPath = path.join(__dirname, '..', 'auth'); | |
| const { state, saveCreds } = await useMultiFileAuthState(authFolderPath); | |
| const { version } = await fetchLatestBaileysVersion(); | |
| logger.info(`Using Baileys version: ${version.join('.')}`); | |
| const sock = makeWASocket({ | |
| printQRInTerminal: true, | |
| auth: state, | |
| version: version, | |
| // getMessage: async key => { }, // Define if using message store features | |
| logger // Pass pino logger to Baileys | |
| }); | |
| // Initialize WhatsApp Service with sock instance | |
| whatsapp.initialize(sock); | |
| // Baileys Event Processing | |
| sock.ev.process(async (events) => { | |
| // ** Connection Logic ** | |
| if (events['connection.update']) { | |
| // (Connection logic remains the same as previous - without auto-delete/auto-restart) | |
| const { connection, lastDisconnect, qr } = events['connection.update']; | |
| const statusCode = (lastDisconnect?.error)?.output?.statusCode; | |
| if (connection === 'close') { | |
| config.botJid = null; // Clear bot JID on close | |
| const shouldReconnect = statusCode !== DisconnectReason.loggedOut; | |
| logger.warn({ err: lastDisconnect?.error, shouldReconnect }, `Connection closed. Status Code: ${statusCode}`); | |
| if (shouldReconnect) { | |
| logger.info("Attempting to reconnect in 5 seconds..."); | |
| setTimeout(connectWhatsApp, 5000); | |
| } else { | |
| logger.error('Connection closed: Logged Out or Invalid Session. Please delete "auth" folder manually and restart.'); | |
| process.exit(1); | |
| } | |
| } else if (connection === 'open') { | |
| config.botJid = sock.user?.id; | |
| logger.info({ botJid: config.botJid }, 'WhatsApp connection opened and service initialized.'); | |
| whatsapp.initialize(sock); // Re-initialize service if needed | |
| } | |
| if(qr) { | |
| logger.info('QR code received, scan please!'); | |
| } | |
| } | |
| // ** Credentials Update ** | |
| if (events['creds.update']) { | |
| await saveCreds(); | |
| } | |
| // ** Message Handling ** | |
| if (events['messages.upsert']) { | |
| const upsert = events['messages.upsert']; | |
| // logger.trace({ upsert }, 'Received messages.upsert event'); | |
| if (upsert.type === 'notify') { | |
| for (const msg of upsert.messages) { | |
| // --- Basic Message Filtering --- | |
| // Ignore messages without content, from self, or status broadcasts | |
| if (!msg.message || msg.key.fromMe || msg.key.remoteJid === 'status@broadcast') { | |
| // logger.trace({ msgId: msg.key.id }, 'Ignoring message (no content, fromMe, or status broadcast)'); | |
| continue; | |
| } | |
| // Ignore group messages | |
| if (msg.key.remoteJid.endsWith('@g.us')) { | |
| // logger.trace({ msgId: msg.key.id, group: msg.key.remoteJid }, 'Ignoring group message'); | |
| continue; | |
| } | |
| // *** ADDED: Ignore channel messages *** | |
| if (msg.key.remoteJid.endsWith('@newsletter')) { | |
| logger.trace({ msgId: msg.key.id, channel: msg.key.remoteJid }, 'Ignoring channel message'); | |
| continue; | |
| } | |
| // *** END ADDED FILTER *** | |
| // Delegate processing asynchronously for valid user messages | |
| setImmediate(() => { | |
| processMessage(msg).catch(err => { | |
| logger.error({ err, msgId: msg?.key?.id }, "Error caught in setImmediate for processMessage"); | |
| }); | |
| }); | |
| } // end for loop | |
| } // end if notify | |
| } // end messages.upsert | |
| }); // End sock.ev.process | |
| return sock; | |
| } // End connectWhatsApp | |
| module.exports = { connectWhatsApp }; | |