how i can run whatsapp-web.js in nestjs project?

Viewed 62

i working on whatsapp-web.js for sending message to client. I will have several routes that I want each of them to send a message to the user if upon request. i want to generate qrCode using qrcode-terminal npm, I followed all the steps mentioned in the whatsapp-web.js documentation correctly. I made a utils file and wrote these codes in it.

     const qrcode = require('qrcode-terminal');

const { Client, LocalAuth } = require('whatsapp-web.js');

export const client = new Client({
  restartOnAuthFail: true,
  qrTimeoutMs: 0,
  authTimeoutMs: 0,
  takeoverOnConflict: true,
  puppeteer: {
    headless: true,
    args: [
      '--no-sandbox',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage',
      '--disable-accelerated-2d-canvas',
      '--no-first-run',
      '--no-zygote',
      '--disable-gpu',
      '--unhandled-rejections=strict',
    ],
  },
  authStrategy: new LocalAuth(),
});

client.on('qr', qr => {
  qrcode.generate(qr, { small: true });
  console.log('QR RECEIVED', qr);
});

client.on('ready', () => {
  const number = '+9809172562637';

  // Your message.
  const text = 'radmehr';

  // Getting chatId from the number.
  // we have to delete "+" from the beginning and add "@c.us" at the end of the number.
  const chatId = number.substring(1) + '@c.us';

  // Sending message.
  client.sendMessage(chatId, text);
});

client.initialize();

Now I want to run this piece of code when running the project and show qrCode to me so I can scan it. Now I don't know where to call these functions correctly.please tell me how to set it up and how can I run this utils file ? Thank you in advance for any help.

0 Answers
Related