whatsapp-client.js 4.7 KB

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