whatsapp-client.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const { Client, LocalAuth } = require('whatsapp-web.js');
  2. const axios = require('axios');
  3. const qrcode = require('qrcode-terminal');
  4. const fs = require('fs');
  5. const map = new Map();
  6. const duplicateMap = new Map();
  7. class WhasAppClient {
  8. constructor(be, res) {
  9. this.clientId = be;
  10. this.isGenerateQr = false;
  11. this.client = new Client({
  12. authStrategy: new LocalAuth({
  13. clientId: this.clientId
  14. }),
  15. restartOnAuthFail: true,
  16. puppeteer: {
  17. headless: true,
  18. args: ['--no-sandbox'],
  19. },
  20. });
  21. this.retryQr = 0;
  22. this.client.initialize().catch(error => {
  23. console.log(`error:${error}`);
  24. });
  25. this.client.on('authenticated', async () => {
  26. console.log(`be:${this.clientId}:Authenticated`);
  27. });
  28. this.client.on('qr', async qr => {
  29. console.log(`be:${this.clientId}:qr:${qr}`);
  30. this.retryQr = this.retryQr + 1;
  31. if (this.retryQr == 5) {
  32. duplicateMap.delete(this.clientId);
  33. console.log(`destroy: ${this.clientId}`);
  34. await this.client.destroy();
  35. fs.rm(`.wwebjs_auth/session-${this.clientId}`, { recursive: true, force: true }, (error) => { });
  36. // kirim ke abi untuk update status
  37. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  38. be: this.clientId,
  39. status: 0
  40. })
  41. .then(function (response) {
  42. console.log(`response: ${response.data}`);
  43. })
  44. .catch(function (error) {
  45. console.log(error);
  46. });
  47. return;
  48. }
  49. if (!this.isGenerateQr) {
  50. if (res != null) res.json({ status: '00', be: this.clientId, qr: qr });
  51. }
  52. this.isGenerateQr = true;
  53. // generate qr terminal
  54. qrcode.generate(qr, { small: true });
  55. // kirim ke abi untuk update qr
  56. axios.post('https://nexilis.io/dashboardv2/logics/insert_whatsapp_qrcode', {
  57. be: this.clientId,
  58. qrcode: qr
  59. })
  60. .then(function (response) {
  61. console.log(`response: ${response.data}`);
  62. })
  63. .catch(function (error) {
  64. console.log(error);
  65. });
  66. });
  67. let that = this.client;
  68. this.client.on('message', async (msg) => {
  69. console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
  70. if (msg.body.startsWith('!bot ')) {
  71. let message = msg.body.slice(5, msg.body.length);
  72. let chat = await msg.getChat();
  73. chat.sendSeen();
  74. if (msg.to.indexOf("8119607282") === -1) { // nexilis.io official number
  75. return;
  76. }
  77. console.log(`message:${message}`);
  78. var url = 'https://nexilis.io/chatgpt/gptnexilis';
  79. var content = [{
  80. role: 'user',
  81. content: message
  82. }];
  83. if (message.includes("@bsb")) {
  84. url = 'https://nexilis.io/ibotbank_wsgi/predict'
  85. content = {prompt: message}
  86. }
  87. axios.post(url, content, {
  88. headers: {
  89. 'User-Agent': 'easySoftIndonesia',
  90. 'Cookie': 'PHPSESSID=123;MOBILE=123'
  91. }
  92. })
  93. .then(function (response) {
  94. console.log(`response:${JSON.stringify(response.data)}`);
  95. that.sendMessage(msg.from, response.data.content);
  96. })
  97. .catch(function (error) {
  98. console.log(error);
  99. });
  100. }
  101. });
  102. this.client.on('ready', () => {
  103. console.log(`be:${this.clientId}:Client is ready!`);
  104. map.set(this.clientId, this.client);
  105. if (!this.isGenerateQr) {
  106. if (res != null) res.json({ status: '00', be: this.clientId });
  107. }
  108. // kirim ke abi untuk update status
  109. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  110. be: this.clientId,
  111. status: 1
  112. })
  113. .then(function (response) {
  114. console.log(`response: ${response.data}`);
  115. })
  116. .catch(function (error) {
  117. console.log(error);
  118. });
  119. });
  120. this.client.on('disconnected', async (reason) => {
  121. console.log(`be:${this.clientId}:Client was logged out`, reason);
  122. map.delete(this.clientId);
  123. duplicateMap.delete(this.clientId);
  124. // kirim ke abi untuk update status
  125. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  126. be: this.clientId,
  127. status: 0
  128. })
  129. .then(function (response) {
  130. console.log(`response: ${response.data}`);
  131. })
  132. .catch(function (error) {
  133. console.log(error);
  134. });
  135. });
  136. }
  137. }
  138. module.exports.WhasAppClient = WhasAppClient;
  139. module.exports.map = map;
  140. module.exports.duplicateMap = duplicateMap;