123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- const { Client, LocalAuth } = require('whatsapp-web.js');
- const axios = require('axios');
- const qrcode = require('qrcode-terminal');
- const fs = require('fs');
- const map = new Map();
- const duplicateMap = new Map();
- class WhasAppClient {
- constructor(be, res) {
- this.clientId = be;
- this.isGenerateQr = false;
- this.client = new Client({
- authStrategy: new LocalAuth({
- clientId: this.clientId
- }),
- restartOnAuthFail: true,
- puppeteer: {
- headless: true,
- args: ['--no-sandbox'],
- },
- });
- this.retryQr = 0;
- this.client.initialize().catch(_ => _);
- this.client.on('authenticated', async () => {
- console.log(`be:${this.clientId}:Authenticated`);
- });
- this.client.on('qr', async qr => {
- console.log(`be:${this.clientId}:qr:${qr}`);
- this.retryQr = this.retryQr + 1;
- if (this.retryQr == 5) {
- duplicateMap.delete(this.clientId);
- console.log(`destroy: ${this.clientId}`);
- await this.client.destroy();
- fs.rm(`.wwebjs_auth/session-${this.clientId}`, { recursive: true, force: true }, (error) => { });
- // kirim ke abi untuk update status
- axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
- be: this.clientId,
- status: 0
- })
- .then(function (response) {
- console.log(`response: ${response.data}`);
- })
- .catch(function (error) {
- console.log(error);
- });
- return;
- }
- if (!this.isGenerateQr) {
- if (res != null) res.json({ status: '00', be: this.clientId, qr: qr });
- }
- this.isGenerateQr = true;
- // generate qr terminal
- 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: ${response.data}`);
- })
- .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)}`);
- if (msg.body.startsWith('!bot ')) {
- let message = msg.body.slice(5, msg.body.length);
- let chat = await msg.getChat();
- chat.sendSeen();
- console.log(`message:${message}`);
- axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
- role: 'user',
- content: message
- }], {
- 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);
- });
- }
- });
- this.client.on('ready', () => {
- console.log(`be:${this.clientId}:Client is ready!`);
- map.set(this.clientId, this.client);
- if (!this.isGenerateQr) {
- if (res != null) res.json({ status: '00', be: this.clientId });
- }
- // kirim ke abi untuk update status
- axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
- be: this.clientId,
- status: 1
- })
- .then(function (response) {
- console.log(`response: ${response.data}`);
- })
- .catch(function (error) {
- console.log(error);
- });
- });
- this.client.on('disconnected', async (reason) => {
- console.log(`be:${this.clientId}:Client was logged out`, reason);
- map.delete(this.clientId);
- // kirim ke abi untuk update status
- axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
- be: this.clientId,
- status: 0
- })
- .then(function (response) {
- console.log(`response: ${response.data}`);
- })
- .catch(function (error) {
- console.log(error);
- });
- });
- }
- }
- module.exports.WhasAppClient = WhasAppClient;
- module.exports.map = map;
- module.exports.duplicateMap = duplicateMap;
|