Automated reply with Whatsapp Web JS

Ady Bagus
5 min readJul 19, 2023

--

Our society nowadays loves to make everything automated, and for the lazy one like me is a solid YES. for example automated house gates, automated lamps, automated scripts, and everything. most online seller, they use automated reply from eCommerce to reply as soon as the buyer text them but it's also limited only to greeting messages or product suggestions. mostly, the seller is not on the app or on standby on their PC monitoring their in-app chat. for faster replies, a lot of sellers put their phone numbers if they are away, and the buyer can chat with them directly about the products or any question they have in mind. same as the previous one, “they are away” which means they are not on the phone and take time to reply until they open the phone to reply to the buyer. As a buyer myself, I got annoyed too with this kind of situation.

A picture of myself

It's painful right, There is several chatting app that you can automate to reply the message that others send to you. for example Discord, Telegram, and WhatsApp. For double check, you can check their TOS to make sure you're not violating their rules and get banned. On this occasion, we will talk about WhatsApp Web JS.

Whatsapp Web JS

What is Whatsapp Web JS??

Simply WhatsApp web js is one of the libraries that we can use to automate replies or send messages to the others. to build this automated app, we will use Node JS. for you who haven't installed Node JS, you can get the installer here https://nodejs.org/en

As for me, I'm currently using Nodejs v18.14.0. you can get the older version here too https://nodejs.org/en/download/releases

Let’s Go

After you installed Node JS you can check if your installation is already done, you can check it first on your terminal with this script

PS E:\Developments\Applications\Node App\whatsappjs-learn> node -v
v18.14.0

the result will tell your Node JS version like this, and then you good to go

Node JS version

now, to start your first program. you need to type npm -init to set up your project details. you can leave them empty or you can set it to your preference. You will see this kind of result after filling in the inputs

PS E:\Developments\Applications\Node App\whatsappjs-learn> npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (whatsappjs-learn)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository: (https://github.com/itsmeeep/whatsappjs-learn.git)
keywords:
author:
license: (ISC)
About to write to E:\Developments\Applications\Node App\whatsappjs-learn\package.json:

{
"name": "whatsappjs-learn",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/itsmeeep/whatsappjs-learn.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/itsmeeep/whatsappjs-learn/issues"
},
"homepage": "https://github.com/itsmeeep/whatsappjs-learn#readme"
}


Is this OK? (yes)

Code Breakdown

The first step is creating index.js file. in this index.js file, we will put scripts to run WhatsApp Web Js. Here the full code

Before you run the code, you need to install the required modules to make it fun. you can see the requirement module on the top part of the codes.

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

in this part of the code, you need to install qrcode-terminal and whatsapp-web.js to make it work. To install a new module you can use command npm install <the module that you want to install>

The installation progress is more or less like this

PS E:\Developments\Applications\Node App\whatsappjs-learn> npm install qrcode-terminal
npm WARN whatsappjs-learn@1.0.0 No description

+ qrcode-terminal@0.12.0
added 1 package from 2 contributors and audited 1 package in 0.621s
found 0 vulnerabilities

PS E:\Developments\Applications\Node App\whatsappjs-learn> npm install whatsapp-web.js
npm WARN deprecated puppeteer@13.7.0: < 19.4.0 is no longer supported

> puppeteer@13.7.0 install E:\Developments\Applications\Node App\whatsappjs-learn\node_modules\puppeteer
> node install.js

Downloading Chromium r982053 - 180.5 Mb [====================] 100% 0.0s
Chromium (982053) downloaded to E:\Developments\Applications\Node App\whatsappjs-learn\node_modules\puppeteer\.local-chromium\win64-982053
npm WARN whatsappjs-learn@1.0.0 No description

+ whatsapp-web.js@1.21.0
added 110 packages from 110 contributors and audited 111 packages in 48.06s

8 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities

After the installation process is completed, you can run the app by typing command node index.js on your terminal. Then we moved to the next part of the codes

const whatsapp = new Client({
authStrategy: new LocalAuth()
});

In this part we declare the authentication process, there are several types of authentication. by default, WhatsApp Web JS does not save your login session. usually, I use LocalAuth() so I don't have to rescan the QR code every time I rerun the app With this LocalAuth() method you can store your session to your local files.

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

this code will generate a QR code on your terminal every time WhatsApp requests a QR code to log in. The QR code will be like this when the app starts

QR Code

the QR code will be regenerated if you leave them too long

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

whatsapp.on('message', async message => {
if(message.body === '!ping') {
message.reply('pong');
}
});

on this code part, you see whatsapp.on(‘ready’, () … this code will be executed when the app is ready to use. the app will let you know if WhatsApp App is already running on your local by sending a message to your terminal. whatsapp.on(‘message’, … this code will be executed every time your number receives a message. you can set any function that you want to execute with some registered commands so whenever the app reads the designed text it will read/execute the command you put in here, as example I'm taking command !ping then reply automatically to the sender.

example

The example above is the result when you send the designed command to the account that you logged in to WhatsApp Web JS.

On the next chance, I will talk about express js so stay tuned. thanks, guys :)

--

--

Ady Bagus

Software Quality Assurance | Backend Developer | PHP Developer