const { Client, LocalAuth } = require('whatsapp-web.js'); const qrcode = require('qrcode-terminal'); const axios = require('axios') class WhasAppClient { constructor(be, res) { this.clientId = be; this.res = res; this.isGenerateQr = false; this.client = new Client({ puppeteer: { args: ['--no-sandbox'], }, authStrategy: new LocalAuth({ clientId: this.clientId }), webVersionCache: { type: "remote", remotePath: "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html", }, }); this.client.initialize(); this.client.on('authenticated', async () => { console.log(`be:${this.clientId}:Authenticated`); }); this.client.on('qr', async qr => { console.log(`be:${this.clientId}:qr:${qr}`); if (!this.isGenerateQr) { this.res.json({ status: '00', be: this.clientId, qr: qr }); } this.isGenerateQr = true; // generate qr qrcode.generate(qr, { small: true }); // kirim ke abi untuk update qr axios.post('https://nexilis.io/dashboardv2/logics/insert_whatsapp_qrcode', { be: this.clientId, qrcode: qr }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); }); let that = this.client; this.client.on('message', async (msg) => { console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`); try { axios.post('https://nexilis.io/chatgpt/gptnexilis', [{ role: 'user', content: msg.body }], { headers: { 'User-Agent': 'easySoftIndonesia', 'Cookie': 'PHPSESSID=123;MOBILE=123' } }) .then(function (response) { console.log(`response:${JSON.stringify(response.data)}`); that.sendMessage(msg.from, response.data.content); }) .catch(function (error) { console.log(error); }); } catch (error) { console.log(`error:${error}`); } }); this.client.on('disconnected', async (reason) => { console.log(`be:${this.clientId}:Client was logged out`, reason); }); } } module.exports.WhasAppClient = WhasAppClient;