whatsapp-client.js 5.6 KB

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