whatsapp-client.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
  79. role: 'user',
  80. content: message
  81. }], {
  82. headers: {
  83. 'User-Agent': 'easySoftIndonesia',
  84. 'Cookie': 'PHPSESSID=123;MOBILE=123'
  85. }
  86. })
  87. .then(function (response) {
  88. console.log(`response:${JSON.stringify(response.data)}`);
  89. that.sendMessage(msg.from, response.data.content);
  90. })
  91. .catch(function (error) {
  92. console.log(error);
  93. });
  94. }
  95. });
  96. this.client.on('ready', () => {
  97. console.log(`be:${this.clientId}:Client is ready!`);
  98. map.set(this.clientId, this.client);
  99. if (!this.isGenerateQr) {
  100. if (res != null) res.json({ status: '00', be: this.clientId });
  101. }
  102. // kirim ke abi untuk update status
  103. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  104. be: this.clientId,
  105. status: 1
  106. })
  107. .then(function (response) {
  108. console.log(`response: ${response.data}`);
  109. })
  110. .catch(function (error) {
  111. console.log(error);
  112. });
  113. });
  114. this.client.on('disconnected', async (reason) => {
  115. console.log(`be:${this.clientId}:Client was logged out`, reason);
  116. map.delete(this.clientId);
  117. duplicateMap.delete(this.clientId);
  118. // kirim ke abi untuk update status
  119. axios.post('https://nexilis.io/dashboardv2/logics/update_whatsapp_login', {
  120. be: this.clientId,
  121. status: 0
  122. })
  123. .then(function (response) {
  124. console.log(`response: ${response.data}`);
  125. })
  126. .catch(function (error) {
  127. console.log(error);
  128. });
  129. });
  130. }
  131. }
  132. module.exports.WhasAppClient = WhasAppClient;
  133. module.exports.map = map;
  134. module.exports.duplicateMap = duplicateMap;