WhatsApp-JS: Bringing Automation and Fun to WhatsApp

WhatsApp-JS: Bringing Automation and Fun to WhatsApp
ibnu gunawan prayogo

Ibnu Gunawan P

Sat, 26 2021, 1:17:00 am

Table Of Content

# Preparation

Before we dive into coding, make sure you have a laptop with Node.js installed. If you don't already have Node.js, you can download and install it from the official website at nodejs.org. And, don't forget to grab a cup of coffee ☕ for that extra coding energy. 😁

# Coding Time

Once you've prepared your laptop with Node.js, the first step is to create a new project with the following command:

mkdir [project name] && cd [project name] && npm init --y

Creating a New Project

Next, install the whatsapp-web.js package using the following command:

npm i whatsapp-web.js or yarn add whatsapp-web.js

After installation, create an index.js file in your project and add the following script:

const { Client } = require('whatsapp-web.js');
const client = new Client();

// Entering WhatsApp-JS using the QR code sent by whatsapp-web.js
client.on('qr', (qr) => {
    console.log('QR Code', qr);
});

// Ready to use WhatsApp-web.js
client.on('ready', () => {
    console.log('It's ready!');
});

// When the client disconnects from WhatsApp-web
client.on('disconnected', (reason) => {
    console.log('WhatsApp bot disconnected', reason);
});

client.initialize();

The whatsapp-web.js package uses Selenium to access web.whatsapp.com in the background, which means you need to log in using your WhatsApp account. Run the code with the following command:

node index.js

QR Code for WhatsApp-JS

The QR code is displayed as encrypted text. To turn it into a scannable QR code, you can install the qrcode-terminal package with this command:

npm i qrcode-terminal or yarn add qrcode-terminal

After installing the qrcode-terminal package, modify the code in the index.js file to look like this:

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

// Entering WhatsApp-JS using the QR code sent by whatsapp-web.js
client.on('qr', (qr) => {
    qrcode.generate(qr, { small: true });
});

// Ready to use WhatsApp-web.js
client.on('ready', () => {
    console.log('It's ready!');

    const number = "+628XXXXXXXX"; // Replace with the phone number to send the message to
    const text = "Bot is ready!"; // The message to send
    const chatId = number.substring(1) + "@c.us";

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

// When a message is received by the bot
client.on('message', async (message) => {
    // Check if the received message is "halo" (hello) and reply with "Haii!!"
    if (message.body === 'halo') {
        message.reply('Haii!!');
    }
});

// When the client disconnects from WhatsApp-web
client.on('disconnected', (reason) => {
    console.log('WhatsApp bot disconnected', reason);
});

client.initialize();

Sending and Receiving Messages

Sending and Receiving Messages

Sending and Receiving Messages

# Conclusion

Wasn't that easy? For more examples and reference, you can check out this link.

That's all I have for you. Hope you find it useful. Thanks! 😁