const { Client, LocalAuth } = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); const port = 3000; // Middleware untuk meng-parse JSON request body app.use(bodyParser.json()); // Inisialisasi client WhatsApp const client = new Client({ authStrategy: new LocalAuth(), }); // Menampilkan QR Code saat pertama kali login client.on('qr', (qr) => { qrcode.generate(qr, { small: true }); }); // Setelah terhubung client.on('ready', () => { console.log('WhatsApp Web is ready'); }); // Service untuk mengirim pesan app.post('/send-message', (req, res) => { const { phone, message } = req.body; if (!phone || !message) { return res.status(400).send('Phone number and message are required!'); } // Mengirim pesan client.sendMessage(`${phone}@c.us`, message) .then((response) => { res.status(200).send('Message sent successfully!'); }) .catch((error) => { console.error(error); res.status(500).send('Failed to send message'); }); }); // Jalankan server app.listen(port, () => { console.log(`WhatsApp API service running on http://localhost:${port}`); }); // Log in to WhatsApp Web client.initialize();