whatsapp-client.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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) {
  6. this.clientId = be;
  7. this.isGenerateQr = false;
  8. this.client = new Client({
  9. puppeteer: {
  10. args: ['--no-sandbox'],
  11. },
  12. authStrategy: new LocalAuth({
  13. clientId: this.clientId
  14. }),
  15. webVersionCache: {
  16. type: "remote",
  17. remotePath:
  18. "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html",
  19. },
  20. });
  21. this.client.initialize();
  22. this.client.on('authenticated', async () => {
  23. console.log(`be:${this.clientId}:Authenticated`);
  24. });
  25. let that = this.client;
  26. this.client.on('message', async (msg) => {
  27. // console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
  28. // axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
  29. // role: 'user',
  30. // content: msg.body
  31. // }], {
  32. // headers: {
  33. // 'User-Agent': 'easySoftIndonesia',
  34. // 'Cookie': 'PHPSESSID=123;MOBILE=123'
  35. // }
  36. // })
  37. // .then(function (response) {
  38. // console.log(`response:${JSON.stringify(response.data)}`);
  39. // that.sendMessage(msg.from, response.data.content);
  40. // })
  41. // .catch(function (error) {
  42. // console.log(error);
  43. // });
  44. });
  45. this.client.on('disconnected', async (reason) => {
  46. console.log(`be:${this.clientId}:Client was logged out`, reason);
  47. });
  48. }
  49. }
  50. module.exports.WhasAppClient = WhasAppClient;