yayan 1 an în urmă
părinte
comite
a45c555a46
5 a modificat fișierele cu 161 adăugiri și 185 ștergeri
  1. 2 1
      .gitignore
  2. 2 49
      main.js
  3. 74 129
      package-lock.json
  4. 1 1
      package.json
  5. 82 5
      whatsapp-client.js

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
 node_modules
-.wwebjs_auth
+.wwebjs_auth
+.wwebjs_cache

+ 2 - 49
main.js

@@ -1,59 +1,12 @@
-const { WhasAppClient } = require('./whatsapp-client.js');
+const { WhasAppClient, map, duplicateMap } = require('./whatsapp-client.js');
 const { MessageMedia } = require('whatsapp-web.js');
 const express = require('express');
-// const qrcode = require('qrcode-terminal');
 const axios = require('axios');
 const https = require('https');
 const fs = 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) {
-        //     try {
-        //         await wa.client.destroy();
-        //         fs.rm(`.wwebjs_auth/session-${wa.clientId}`, { recursive: true, force: true }, (error) => {
-        //         });
-        //     } catch (error) {
-        //         console.log(error);
-        //     }
-        //     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);
-        await wa.client.destroy();
-        map.delete(wa.clientId);
-    });
+    new WhasAppClient(be, res);
 }
 fs.readdir('.wwebjs_auth', (err, files) => {
     if (files != null) {

+ 74 - 129
package-lock.json

@@ -13,7 +13,7 @@
         "express": "^4.19.2",
         "puppeteer": "^22.10.0",
         "qrcode-terminal": "^0.12.0",
-        "whatsapp-web.js": "^1.23.0"
+        "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#webpack-exodus"
       }
     },
     "node_modules/@babel/code-frame": {
@@ -537,7 +537,8 @@
     "node_modules/chownr": {
       "version": "1.1.4",
       "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
-      "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg=="
+      "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==",
+      "license": "ISC"
     },
     "node_modules/chromium-bidi": {
       "version": "0.5.19",
@@ -704,29 +705,11 @@
       "version": "3.1.5",
       "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz",
       "integrity": "sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==",
+      "license": "MIT",
       "dependencies": {
         "node-fetch": "2.6.7"
       }
     },
-    "node_modules/cross-fetch/node_modules/node-fetch": {
-      "version": "2.6.7",
-      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
-      "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
-      "dependencies": {
-        "whatwg-url": "^5.0.0"
-      },
-      "engines": {
-        "node": "4.x || >=6.0.0"
-      },
-      "peerDependencies": {
-        "encoding": "^0.1.0"
-      },
-      "peerDependenciesMeta": {
-        "encoding": {
-          "optional": true
-        }
-      }
-    },
     "node_modules/data-uri-to-buffer": {
       "version": "6.0.2",
       "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-6.0.2.tgz",
@@ -1114,18 +1097,6 @@
       "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
       "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A=="
     },
-    "node_modules/find-up": {
-      "version": "4.1.0",
-      "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz",
-      "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==",
-      "dependencies": {
-        "locate-path": "^5.0.0",
-        "path-exists": "^4.0.0"
-      },
-      "engines": {
-        "node": ">=8"
-      }
-    },
     "node_modules/fluent-ffmpeg": {
       "version": "2.1.2",
       "resolved": "https://registry.npmjs.org/fluent-ffmpeg/-/fluent-ffmpeg-2.1.2.tgz",
@@ -1588,11 +1559,6 @@
         "graceful-fs": "^4.1.6"
       }
     },
-    "node_modules/jsqr": {
-      "version": "1.4.0",
-      "resolved": "https://registry.npmjs.org/jsqr/-/jsqr-1.4.0.tgz",
-      "integrity": "sha512-dxLob7q65Xg2DvstYkRpkYtmKm2sPJ9oFhrhmudT1dZvNFFTlroai3AWSpLey/w5vMcLBXRgOJsbXpdN9HzU/A=="
-    },
     "node_modules/lazystream": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/lazystream/-/lazystream-1.0.1.tgz",
@@ -1646,17 +1612,6 @@
       "integrity": "sha512-3mk/Zag0+IJxeDrxSgaDPy4zZ3w05PRZeJNnlWhzFz5OkX49J4krc+A8X2d2M69vGMBEX0uyl8M+W+8gH+kBqQ==",
       "optional": true
     },
-    "node_modules/locate-path": {
-      "version": "5.0.0",
-      "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz",
-      "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==",
-      "dependencies": {
-        "p-locate": "^4.1.0"
-      },
-      "engines": {
-        "node": ">=8"
-      }
-    },
     "node_modules/lodash.defaults": {
       "version": "4.2.0",
       "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",
@@ -1788,7 +1743,8 @@
     "node_modules/mkdirp-classic": {
       "version": "0.5.3",
       "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz",
-      "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A=="
+      "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
+      "license": "MIT"
     },
     "node_modules/ms": {
       "version": "2.1.2",
@@ -1812,10 +1768,31 @@
         "node": ">= 0.4.0"
       }
     },
+    "node_modules/node-fetch": {
+      "version": "2.6.7",
+      "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
+      "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
+      "license": "MIT",
+      "dependencies": {
+        "whatwg-url": "^5.0.0"
+      },
+      "engines": {
+        "node": "4.x || >=6.0.0"
+      },
+      "peerDependencies": {
+        "encoding": "^0.1.0"
+      },
+      "peerDependenciesMeta": {
+        "encoding": {
+          "optional": true
+        }
+      }
+    },
     "node_modules/node-webpmux": {
-      "version": "3.2.0",
-      "resolved": "https://registry.npmjs.org/node-webpmux/-/node-webpmux-3.2.0.tgz",
-      "integrity": "sha512-Rmc7MIS7zxDRPkfC9B15malsd1uStX2N6mYqHZNZeCJAVRDjmnxxTsyDh6POvWdNC7nmAYBW6CcHVY62DlnzRg=="
+      "version": "3.1.7",
+      "resolved": "https://registry.npmjs.org/node-webpmux/-/node-webpmux-3.1.7.tgz",
+      "integrity": "sha512-ySkL4lBCto86OyQ0blAGzylWSECcn5I0lM3bYEhe75T8Zxt/BFUMHa8ktUguR7zwXNdS/Hms31VfSsYKN1383g==",
+      "license": "ISC"
     },
     "node_modules/normalize-path": {
       "version": "3.0.0",
@@ -1853,39 +1830,6 @@
         "wrappy": "1"
       }
     },
-    "node_modules/p-limit": {
-      "version": "2.3.0",
-      "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",
-      "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==",
-      "dependencies": {
-        "p-try": "^2.0.0"
-      },
-      "engines": {
-        "node": ">=6"
-      },
-      "funding": {
-        "url": "https://github.com/sponsors/sindresorhus"
-      }
-    },
-    "node_modules/p-locate": {
-      "version": "4.1.0",
-      "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz",
-      "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==",
-      "dependencies": {
-        "p-limit": "^2.2.0"
-      },
-      "engines": {
-        "node": ">=8"
-      }
-    },
-    "node_modules/p-try": {
-      "version": "2.2.0",
-      "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz",
-      "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==",
-      "engines": {
-        "node": ">=6"
-      }
-    },
     "node_modules/pac-proxy-agent": {
       "version": "7.0.1",
       "resolved": "https://registry.npmjs.org/pac-proxy-agent/-/pac-proxy-agent-7.0.1.tgz",
@@ -1954,14 +1898,6 @@
         "node": ">= 0.8"
       }
     },
-    "node_modules/path-exists": {
-      "version": "4.0.0",
-      "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
-      "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==",
-      "engines": {
-        "node": ">=8"
-      }
-    },
     "node_modules/path-is-absolute": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
@@ -1985,17 +1921,6 @@
       "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz",
       "integrity": "sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ=="
     },
-    "node_modules/pkg-dir": {
-      "version": "4.2.0",
-      "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz",
-      "integrity": "sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==",
-      "dependencies": {
-        "find-up": "^4.0.0"
-      },
-      "engines": {
-        "node": ">=8"
-      }
-    },
     "node_modules/process-nextick-args": {
       "version": "2.0.1",
       "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
@@ -2204,6 +2129,8 @@
       "version": "3.0.2",
       "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
       "integrity": "sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==",
+      "deprecated": "Rimraf versions prior to v4 are no longer supported",
+      "license": "ISC",
       "dependencies": {
         "glob": "^7.1.3"
       },
@@ -2692,20 +2619,19 @@
       "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ=="
     },
     "node_modules/whatsapp-web.js": {
-      "version": "1.23.0",
-      "resolved": "https://registry.npmjs.org/whatsapp-web.js/-/whatsapp-web.js-1.23.0.tgz",
-      "integrity": "sha512-88UwvuLCaak4HmSCsp2/JY00p0qYFVQ0rPM6VPPGyjfSmRUsI/UKGjPA/p4FnC+Dygwx7g88K3iB7UdMXkQKhw==",
+      "version": "1.23.1-alpha.5",
+      "resolved": "git+ssh://git@github.com/pedroslopez/whatsapp-web.js.git#8034e8f5ad7411664eb22173fc68075d2820854e",
+      "license": "Apache-2.0",
       "dependencies": {
         "@pedroslopez/moduleraid": "^5.0.2",
-        "fluent-ffmpeg": "^2.1.2",
-        "jsqr": "^1.3.1",
+        "fluent-ffmpeg": "2.1.2",
         "mime": "^3.0.0",
-        "node-fetch": "^2.6.5",
-        "node-webpmux": "^3.1.0",
-        "puppeteer": "^13.0.0"
+        "node-fetch": "^2.6.9",
+        "node-webpmux": "3.1.7",
+        "puppeteer": "^18.2.1"
       },
       "engines": {
-        "node": ">=12.0.0"
+        "node": ">=18.0.0"
       },
       "optionalDependencies": {
         "archiver": "^5.3.1",
@@ -2717,6 +2643,7 @@
       "version": "6.0.2",
       "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz",
       "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==",
+      "license": "MIT",
       "dependencies": {
         "debug": "4"
       },
@@ -2725,14 +2652,16 @@
       }
     },
     "node_modules/whatsapp-web.js/node_modules/devtools-protocol": {
-      "version": "0.0.981744",
-      "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.981744.tgz",
-      "integrity": "sha512-0cuGS8+jhR67Fy7qG3i3Pc7Aw494sb9yG9QgpG97SFVWwolgYjlhJg7n+UaHxOQT30d1TYu/EYe9k01ivLErIg=="
+      "version": "0.0.1045489",
+      "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1045489.tgz",
+      "integrity": "sha512-D+PTmWulkuQW4D1NTiCRCFxF7pQPn0hgp4YyX4wAQ6xYXKOadSWPR3ENGDQ47MW/Ewc9v2rpC/UEEGahgBYpSQ==",
+      "license": "BSD-3-Clause"
     },
     "node_modules/whatsapp-web.js/node_modules/https-proxy-agent": {
       "version": "5.0.1",
       "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz",
       "integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==",
+      "license": "MIT",
       "dependencies": {
         "agent-base": "6",
         "debug": "4"
@@ -2762,33 +2691,48 @@
       }
     },
     "node_modules/whatsapp-web.js/node_modules/puppeteer": {
-      "version": "13.7.0",
-      "resolved": "https://registry.npmjs.org/puppeteer/-/puppeteer-13.7.0.tgz",
-      "integrity": "sha512-U1uufzBjz3+PkpCxFrWzh4OrMIdIb2ztzCu0YEPfRHjHswcSwHZswnK+WdsOQJsRV8WeTg3jLhJR4D867+fjsA==",
-      "deprecated": "< 21.9.0 is no longer supported",
+      "version": "18.2.1",
+      "resolved": "https://registry.npmjs.org/puppeteer/-/puppeteer-18.2.1.tgz",
+      "integrity": "sha512-7+UhmYa7wxPh2oMRwA++k8UGVDxh3YdWFB52r9C3tM81T6BU7cuusUSxImz0GEYSOYUKk/YzIhkQ6+vc0gHbxQ==",
+      "deprecated": "< 22.6.4 is no longer supported",
       "hasInstallScript": true,
+      "license": "Apache-2.0",
+      "dependencies": {
+        "https-proxy-agent": "5.0.1",
+        "progress": "2.0.3",
+        "proxy-from-env": "1.1.0",
+        "puppeteer-core": "18.2.1"
+      },
+      "engines": {
+        "node": ">=14.1.0"
+      }
+    },
+    "node_modules/whatsapp-web.js/node_modules/puppeteer-core": {
+      "version": "18.2.1",
+      "resolved": "https://registry.npmjs.org/puppeteer-core/-/puppeteer-core-18.2.1.tgz",
+      "integrity": "sha512-MRtTAZfQTluz3U2oU/X2VqVWPcR1+94nbA2V6ZrSZRVEwLqZ8eclZ551qGFQD/vD2PYqHJwWOW/fpC721uznVw==",
+      "license": "Apache-2.0",
       "dependencies": {
         "cross-fetch": "3.1.5",
         "debug": "4.3.4",
-        "devtools-protocol": "0.0.981744",
+        "devtools-protocol": "0.0.1045489",
         "extract-zip": "2.0.1",
         "https-proxy-agent": "5.0.1",
-        "pkg-dir": "4.2.0",
-        "progress": "2.0.3",
         "proxy-from-env": "1.1.0",
         "rimraf": "3.0.2",
         "tar-fs": "2.1.1",
         "unbzip2-stream": "1.4.3",
-        "ws": "8.5.0"
+        "ws": "8.9.0"
       },
       "engines": {
-        "node": ">=10.18.1"
+        "node": ">=14.1.0"
       }
     },
     "node_modules/whatsapp-web.js/node_modules/tar-fs": {
       "version": "2.1.1",
       "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.1.tgz",
       "integrity": "sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng==",
+      "license": "MIT",
       "dependencies": {
         "chownr": "^1.1.1",
         "mkdirp-classic": "^0.5.2",
@@ -2797,9 +2741,10 @@
       }
     },
     "node_modules/whatsapp-web.js/node_modules/ws": {
-      "version": "8.5.0",
-      "resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz",
-      "integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==",
+      "version": "8.9.0",
+      "resolved": "https://registry.npmjs.org/ws/-/ws-8.9.0.tgz",
+      "integrity": "sha512-Ja7nszREasGaYUYCI2k4lCKIRTt+y7XuqVoHR44YpI49TtryyqbqvDMn5eqfW7e6HzTukDRIsXqzVHScqRcafg==",
+      "license": "MIT",
       "engines": {
         "node": ">=10.0.0"
       },

+ 1 - 1
package.json

@@ -14,6 +14,6 @@
     "express": "^4.19.2",
     "puppeteer": "^22.10.0",
     "qrcode-terminal": "^0.12.0",
-    "whatsapp-web.js": "^1.23.0"
+    "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#webpack-exodus"
   }
 }

+ 82 - 5
whatsapp-client.js

@@ -1,18 +1,27 @@
 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) {
+    constructor(be, res) {
         this.clientId = be;
         this.isGenerateQr = false;
         this.client = new Client({
-            puppeteer: {
-                args: ['--no-sandbox'],
-            },
             authStrategy: new LocalAuth({
                 clientId: this.clientId
             }),
+            restartOnAuthFail: true,
+            puppeteer: {
+                headless: true,
+                args: ['--no-sandbox'],
+            },
         });
+        this.client.pupBrowser
+        this.retryQr = 0;
 
         this.client.initialize().catch(_ => _);
 
@@ -20,6 +29,49 @@ class WhasAppClient {
             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)}`);
@@ -46,7 +98,32 @@ class WhasAppClient {
                     });
             }
         });
+        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);
+            await this.client.destroy();
+            map.delete(this.clientId);
+        });
     }
 }
 
-module.exports.WhasAppClient = WhasAppClient;
+module.exports.WhasAppClient = WhasAppClient;
+module.exports.map = map;
+module.exports.duplicateMap = duplicateMap;