whatsapp-client.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const { Client, LocalAuth } = require('whatsapp-web.js');
  2. const qrcode = require('qrcode-terminal');
  3. const axios = require('axios')
  4. class WhasAppClient {
  5. constructor(be, res) {
  6. this.clientId = be;
  7. this.res = res;
  8. this.isGenerateQr = false;
  9. this.client = new Client({
  10. puppeteer: {
  11. args: ['--no-sandbox'],
  12. },
  13. authStrategy: new LocalAuth({
  14. clientId: this.clientId
  15. }),
  16. webVersionCache: {
  17. type: "remote",
  18. remotePath:
  19. "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html",
  20. },
  21. });
  22. this.client.initialize();
  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. if (!this.isGenerateQr) {
  29. this.res.json({ status: '00', be: this.clientId, qr: qr });
  30. }
  31. this.isGenerateQr = true;
  32. // generate qr
  33. qrcode.generate(qr, { small: true });
  34. // kirim ke abi untuk update qr
  35. axios.post('https://nexilis.io/dashboardv2/logics/insert_whatsapp_qrcode', {
  36. be: this.clientId,
  37. qrcode: qr
  38. })
  39. .then(function (response) {
  40. console.log(response);
  41. })
  42. .catch(function (error) {
  43. console.log(error);
  44. });
  45. });
  46. let that = this.client;
  47. this.client.on('message', async (msg) => {
  48. console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
  49. try {
  50. axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
  51. role: 'user',
  52. content: msg.body
  53. }], {
  54. headers: {
  55. 'User-Agent': 'easySoftIndonesia',
  56. 'Cookie': 'PHPSESSID=123;MOBILE=123'
  57. }
  58. })
  59. .then(function (response) {
  60. console.log(`response:${JSON.stringify(response.data)}`);
  61. that.sendMessage(msg.from, response.data.content);
  62. })
  63. .catch(function (error) {
  64. console.log(error);
  65. });
  66. } catch (error) {
  67. console.log(`error:${error}`);
  68. }
  69. });
  70. this.client.on('disconnected', async (reason) => {
  71. console.log(`be:${this.clientId}:Client was logged out`, reason);
  72. });
  73. }
  74. }
  75. module.exports.WhasAppClient = WhasAppClient;