whatsapp-client.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { Client, LocalAuth } = require('whatsapp-web.js');
  2. const axios = require('axios');
  3. class WhasAppClient {
  4. constructor(be) {
  5. this.clientId = be;
  6. this.isGenerateQr = false;
  7. this.client = new Client({
  8. puppeteer: {
  9. args: ['--no-sandbox'],
  10. },
  11. authStrategy: new LocalAuth({
  12. clientId: this.clientId
  13. }),
  14. webVersionCache: {
  15. type: "remote",
  16. remotePath:
  17. "https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/2.2412.54.html",
  18. },
  19. });
  20. this.client.initialize().catch(_ => _);
  21. this.client.on('authenticated', async () => {
  22. console.log(`be:${this.clientId}:Authenticated`);
  23. });
  24. let that = this.client;
  25. this.client.on('message', async (msg) => {
  26. console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
  27. if (msg.body.startsWith('!bot ')) {
  28. let message = msg.body.slice(5, msg.body.length);
  29. let chat = await msg.getChat();
  30. chat.sendSeen();
  31. console.log(`message:${message}`);
  32. axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
  33. role: 'user',
  34. content: message
  35. }], {
  36. headers: {
  37. 'User-Agent': 'easySoftIndonesia',
  38. 'Cookie': 'PHPSESSID=123;MOBILE=123'
  39. }
  40. })
  41. .then(function (response) {
  42. console.log(`response:${JSON.stringify(response.data)}`);
  43. that.sendMessage(msg.from, response.data.content);
  44. })
  45. .catch(function (error) {
  46. console.log(error);
  47. });
  48. }
  49. });
  50. }
  51. }
  52. module.exports.WhasAppClient = WhasAppClient;