whatsapp-client.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(_ => _);
  23. this.client.on('authenticated', async () => {
  24. console.log(`be:${this.clientId}:Authenticated`);
  25. });
  26. this.client.on('qr', async qr => {
  27. console.log(`be:${this.clientId}:qr:${qr}`);
  28. this.retryQr = this.retryQr + 1;
  29. if (this.retryQr == 5) {
  30. duplicateMap.delete(this.clientId);
  31. console.log(`destroy: ${this.clientId}`);
  32. await this.client.destroy();
  33. fs.rm(`.wwebjs_auth/session-${this.clientId}`, { recursive: true, force: true }, (error) => { });
  34. // kirim ke abi untuk update status
  35. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  36. be: this.clientId,
  37. status: 0
  38. })
  39. .then(function (response) {
  40. console.log(`response: ${response.data}`);
  41. })
  42. .catch(function (error) {
  43. console.log(error);
  44. });
  45. return;
  46. }
  47. if (!this.isGenerateQr) {
  48. if (res != null) res.json({ status: '00', be: this.clientId, qr: qr });
  49. }
  50. this.isGenerateQr = true;
  51. // generate qr terminal
  52. qrcode.generate(qr, { small: true });
  53. // kirim ke abi untuk update qr
  54. axios.post('https://nexilis.io/dashboardv2/logics/insert_whatsapp_qrcode', {
  55. be: this.clientId,
  56. qrcode: qr
  57. })
  58. .then(function (response) {
  59. console.log(`response: ${response.data}`);
  60. })
  61. .catch(function (error) {
  62. console.log(error);
  63. });
  64. });
  65. let that = this.client;
  66. this.client.on('message', async (msg) => {
  67. // console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
  68. if (msg.body.startsWith('!bot ')) {
  69. let message = msg.body.slice(5, msg.body.length);
  70. let chat = await msg.getChat();
  71. chat.sendSeen();
  72. console.log(`message:${message}`);
  73. axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
  74. role: 'user',
  75. content: message
  76. }], {
  77. headers: {
  78. 'User-Agent': 'easySoftIndonesia',
  79. 'Cookie': 'PHPSESSID=123;MOBILE=123'
  80. }
  81. })
  82. .then(function (response) {
  83. console.log(`response:${JSON.stringify(response.data)}`);
  84. that.sendMessage(msg.from, response.data.content);
  85. })
  86. .catch(function (error) {
  87. console.log(error);
  88. });
  89. }
  90. });
  91. this.client.on('ready', () => {
  92. console.log(`be:${this.clientId}:Client is ready!`);
  93. map.set(this.clientId, this.client);
  94. if (!this.isGenerateQr) {
  95. if (res != null) res.json({ status: '00', be: this.clientId });
  96. }
  97. // kirim ke abi untuk update status
  98. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  99. be: this.clientId,
  100. status: 1
  101. })
  102. .then(function (response) {
  103. console.log(`response: ${response.data}`);
  104. })
  105. .catch(function (error) {
  106. console.log(error);
  107. });
  108. });
  109. this.client.on('disconnected', async (reason) => {
  110. console.log(`be:${this.clientId}:Client was logged out`, reason);
  111. map.delete(this.clientId);
  112. duplicateMap.delete(this.clientId);
  113. // kirim ke abi untuk update status
  114. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  115. be: this.clientId,
  116. status: 0
  117. })
  118. .then(function (response) {
  119. console.log(`response: ${response.data}`);
  120. })
  121. .catch(function (error) {
  122. console.log(error);
  123. });
  124. });
  125. }
  126. }
  127. module.exports.WhasAppClient = WhasAppClient;
  128. module.exports.map = map;
  129. module.exports.duplicateMap = duplicateMap;