WhatsApp Web login QR code in an HTML page using whatsapp-web.js

To display the WhatsApp Web login QR code in an HTML page using whatsapp-web.js in Windows, you can follow these steps:

1. Install the required dependencies:

npm install express qrcode whatsapp-web.js

2. Create a new file named api-browser.js and add the following code:

const express = require('express');
const qrcode = require('qrcode');
const { Client } = require('whatsapp-web.js');

const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

app.get('/qrcode', async (req, res) => {
  const client = new Client();

  client.on('qr', (qr) => {
    qrcode.toDataURL(qr, (err, url) => {
      if (err) {
        console.error('Error generating QR code:', err);
        res.sendStatus(500);
      } else {
        res.send(`QR Code`);
      }
    });
  });

  await client.initialize();
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

3. Create a new file named index.html in the same directory and add the following code:

4. Run the server:

node index.js

5. Open a browser and navigate to http://localhost:3000. You should see the WhatsApp Web login QR code displayed on the page.

This setup will start an Express server, generate the QR code using the qrcode library, and serve it to the HTML page. The whatsapp-web.js library is used to create a Client instance and initialize it to obtain the QR code for login.

Komentar

Postingan populer dari blog ini

Node.js Telegram Bot API send an image with text

Add these security headers to your website