Home > Net >  How to send qr code string response in whatsapp-web.js?
How to send qr code string response in whatsapp-web.js?

Time:06-25

I'm trying to send the qr code of whatsapp-web.js to my frontend with the code bellow

const router = require("express").Router();

router.get("/", async (req: any, res: any) => {
  const qrcode = require("qrcode-terminal");

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

  const client = new Client({
    authStrategy: new LocalAuth(),
  });
  client.on("qr", (qr: any) => {
    // qrcode.generate(qr, { small: true });

    res.send(qr)
  });

  client.on("ready", (qr:any) => {
    console.log("Client is ready!");

    const number = [
      { phone: "number1", name: "name1" },
      { phone: "number2", name: "name2" },
    ];

    number.forEach((el) => {

      // Your message.
      const text = `teste ${el.name}`;

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

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

  client.initialize();

  res.status(200).json("ok");
});

module.exports = router;

but it's not sending. Maybe because there's a delay to generate this code. Can you guys help me

CodePudding user response:

You're registering a callback that sends the qr when the "qr" event is emitted. The res.status(200) is executed before the event is emitted (Even if it is emitted immediately the callback will be executed after the rest of the function since Node is single threaded).

I'm not familiar with the whatsapp package but if you know the qr event will only be emitted once or you only need the first emission you can wrap it in a promise.

Try

router.get('/', async (req, res) => {
    const client = new Client(...)
    let qr = await new Promise((resolve, reject) => {
        client.once('qr', (qr) => resolve(qr))
    })
    res.send(qr)
}

Notice, I used client.once so the callback is unregistered from the client's event after one event is emitted to prevent memory leaks.

It is a good practice to add a reject scenario to prevent cases the promise doesn't resolve

router.get('/', async (req, res) => {
    try {
        const client = new Client(...)
        let qr = await new Promise((resolve, reject) => {
            client.once('qr', (qr) => resolve(qr))
            setTimeout(() => {
                reject(new Error("QR event wasn't emitted in 15 seconds."))
            }, 15000)
        })
        res.send(qr)
    } catch (err) {
        res.send(err.message)
    }
}
  • Related