Check the status of a WhatsApp account using whatsapp-web.js

To check if a phone is registered on WhatsApp, you will need to create a WhatsApp Web session using the whatsapp-web.js library. Once you have created a session, you can use the isRegisteredUser method of the WWebJS object to check if the phone number is registered on WhatsApp.

Here is an example code snippet that demonstrates how to check if a phone number is registered on WhatsApp using the whatsapp-web.js library:

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

const client = new Client();

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

client.on('ready', () => {
    console.log('Client is ready!');

    // Check if phone number is registered on WhatsApp
    const phoneNumber = '62123456789'; // Replace with phone number to check
    client.isRegisteredUser(phoneNumber + '@c.us').then((isRegistered) => {
        if (isRegistered) {
            console.log(phoneNumber + ' is registered on WhatsApp!');
        } else {
            console.log(phoneNumber + ' is not registered on WhatsApp.');
        }
    });
});

client.initialize();

In this example code, we first create a new Client object using the whatsapp-web.js library. We then listen for the qr event, which is emitted when a QR code is required to authenticate the session. Once the QR code is generated, the user must scan the code using their phone's WhatsApp app to authenticate the session.

After the session is authenticated, the ready event is emitted. In this event handler, we use the isRegisteredUser method to check if the phone number specified in the phoneNumber variable is registered on WhatsApp. We append @c.us to the phone number to indicate that we are checking for a user account and not a group account.

Finally, we log the result of the check to the console.

Note that this is just a basic example, and you may need to modify the code to fit your specific use case. Additionally, you should ensure that you are not violating any terms of service or user privacy policies when using the whatsapp-web.js library.

Komentar

Postingan populer dari blog ini

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

Node.js Telegram Bot API send an image with text

Add these security headers to your website