whatsapp-client.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. });
  15. this.client.initialize().catch(_ => _);
  16. this.client.on('authenticated', async () => {
  17. console.log(`be:${this.clientId}:Authenticated`);
  18. });
  19. let that = this.client;
  20. this.client.on('message', async (msg) => {
  21. console.log(`be:${this.clientId}:message:${JSON.stringify(msg)}`);
  22. if (msg.body.startsWith('!bot ')) {
  23. let message = msg.body.slice(5, msg.body.length);
  24. let chat = await msg.getChat();
  25. chat.sendSeen();
  26. console.log(`message:${message}`);
  27. axios.post('https://nexilis.io/chatgpt/gptnexilis', [{
  28. role: 'user',
  29. content: message
  30. }], {
  31. headers: {
  32. 'User-Agent': 'easySoftIndonesia',
  33. 'Cookie': 'PHPSESSID=123;MOBILE=123'
  34. }
  35. })
  36. .then(function (response) {
  37. console.log(`response:${JSON.stringify(response.data)}`);
  38. that.sendMessage(msg.from, response.data.content);
  39. })
  40. .catch(function (error) {
  41. console.log(error);
  42. });
  43. }
  44. });
  45. }
  46. }
  47. module.exports.WhasAppClient = WhasAppClient;