|
@@ -1,25 +1,65 @@
|
|
|
const { WhasAppClient } = require('./whatsapp-client.js');
|
|
|
const { MessageMedia } = require('whatsapp-web.js');
|
|
|
const express = require('express');
|
|
|
+const qrcode = require('qrcode-terminal');
|
|
|
+const axios = require('axios');
|
|
|
const f = require('fs');
|
|
|
|
|
|
const map = new Map();
|
|
|
const duplicateMap = new Map();
|
|
|
|
|
|
+function createWa(be, res) {
|
|
|
+ const wa = new WhasAppClient(be);
|
|
|
+ wa.client.on('qr', async qr => {
|
|
|
+ console.log(`be:${wa.clientId}:qr:${qr}`);
|
|
|
+ if (res == null) {
|
|
|
+ wa.client.destroy();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!wa.isGenerateQr) {
|
|
|
+ if (res != null) res.json({ status: '00', be: wa.clientId, qr: qr });
|
|
|
+ }
|
|
|
+ wa.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: wa.clientId,
|
|
|
+ qrcode: qr
|
|
|
+ })
|
|
|
+ .then(function (response) {
|
|
|
+ console.log(`response: ${response.data}`);
|
|
|
+ })
|
|
|
+ .catch(function (error) {
|
|
|
+ console.log(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ wa.client.on('ready', () => {
|
|
|
+ console.log(`be:${wa.clientId}:Client is ready!`);
|
|
|
+ map.set(wa.clientId, wa.client);
|
|
|
+ if (!wa.isGenerateQr) {
|
|
|
+ if (res != null) res.json({ status: '00', be: wa.clientId });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ wa.client.on('disconnected', async (reason) => {
|
|
|
+ console.log(`be:${wa.clientId}:Client was logged out`, reason);
|
|
|
+ wa.client.destroy();
|
|
|
+ map.delete(wa.clientId);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
f.readdir('.wwebjs_auth', (err, files) => {
|
|
|
files.forEach(file => {
|
|
|
console.log(`Read session: ${file}`);
|
|
|
- const session = file.split('-')[1];
|
|
|
- const wa = new WhasAppClient(session, null);
|
|
|
- wa.client.on('ready', () => {
|
|
|
- console.log(`be:${wa.clientId}:Client is ready!`);
|
|
|
- map.set(wa.clientId, wa.client);
|
|
|
- });
|
|
|
+ const be = file.split('-')[1];
|
|
|
+ createWa(be);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
const app = express();
|
|
|
-app.use(express.json({limit: '50mb'}));
|
|
|
+app.use(express.json({ limit: '50mb' }));
|
|
|
app.get("/api/qr/:be", (req, res) => {
|
|
|
try {
|
|
|
console.log(`params:${req.params.be}`);
|
|
@@ -35,36 +75,7 @@ app.get("/api/qr/:be", (req, res) => {
|
|
|
return;
|
|
|
}
|
|
|
duplicateMap.set(be, be);
|
|
|
- const wa = new WhasAppClient(be, res);
|
|
|
- wa.client.on('qr', async qr => {
|
|
|
- console.log(`be:${wa.clientId}:qr:${qr}`);
|
|
|
- if (!wa.isGenerateQr) {
|
|
|
- res.json({ status: '00', be: wa.clientId, qr: qr });
|
|
|
- }
|
|
|
- wa.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: wa.clientId,
|
|
|
- qrcode: qr
|
|
|
- })
|
|
|
- .then(function (response) {
|
|
|
- console.log(response);
|
|
|
- })
|
|
|
- .catch(function (error) {
|
|
|
- console.log(error);
|
|
|
- });
|
|
|
- });
|
|
|
- wa.client.on('ready', () => {
|
|
|
- console.log(`be:${wa.clientId}:Client is ready!`);
|
|
|
- map.set(wa.clientId, wa.client);
|
|
|
- if (!wa.isGenerateQr) {
|
|
|
- res.json({ status: '00', be: wa.clientId });
|
|
|
- }
|
|
|
- });
|
|
|
+ createWa(be, res);
|
|
|
} catch (error) {
|
|
|
console.log(`error:${error}`);
|
|
|
}
|
|
@@ -86,7 +97,7 @@ app.post("/api/send/:be", async (req, res) => {
|
|
|
const data = req.body.message.data;
|
|
|
const caption = req.body.message.caption;
|
|
|
let messageMedia = new MessageMedia(media, data, fileName);
|
|
|
- await client.sendMessage(`${to}@c.us`, messageMedia, { caption: caption});
|
|
|
+ await client.sendMessage(`${to}@c.us`, messageMedia, { caption: caption });
|
|
|
res.json({ status: '00', be: be });
|
|
|
return;
|
|
|
}
|