Automated reply with Whatsapp Web JS + Express JS

Ady Bagus
5 min readJul 19, 2023

--

So we will continue our last project about automated replies with WhatsApp Web JS. On this occasion, we will put Express JS into it to make it executable via URL too.

for you guys who haven't seen my last story, you can check it out here 👇👇👇

Just came out with the idea it will be fun if we can access our WhatsApp bot via URL. it just reminds me of broadcasters informing parents about their children at school. we are gonna try to make that now :D

before that, we need to get express JS first. to install express JS to our current project, we can use this command.

npm install express

after the installation is complete, then here we go again!! :D

Basic Express

As you can see, we will combine the basic example with our current WhatsApp JS app to make it work together. in this code, you can see the request method is GET. so when you access it from the URL it will send you back this response

so, the idea is when we execute the URL it will send a message to the designed numbers that we want to send a WhatsApp notification. back to the WhatsApp App. so we need to know how WhatsApp read the number

Getting Number Pattern

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

As you can see, we are using the message.body when getting a message from others. We can get the sender number from the message variable

message variable

From this response, we can conclude that the sender's number is from message.from variable, to make sure you can rerun and console.log the variable. it comes with localization format + @c.us behind the number, so on the sample target number, we can make sample data with the same format so the WhatsApp JS can recognize the number.

message.from output

move to the next step, according to the WhatsApp Web JS documentation. we can use these scripts to send messages to target numbers

client.on('message', message => {
if(message.body === '!ping') {
client.sendMessage(message.from, 'pong');
}
});

Now the WhatsApp pattern is already found, now we need to make the target data set. let's say it is the parents number to notify them about their children's status at school. you can make JSON files or make variables that hold parents numbers. In my case, I like using JSON files more than variables (use what you think is best for your needs).

Parents Data Set

For example, I'm making a new file parents.json, and putting the parent numbers like this.

number list

With this number, we can use it to send the parents WhatsApp notifications. back to express again :)

Send Message Routes

To make it more specific, we need to edit the route URL to avoid hitting the wrong URL when executing the task. For example, I'm naming my route like this

app.get('/send-whatsapp-notification', (req, res) => {
res.send('Done')
})

under this route, we need to read our data parents for sending them notifications. usually, I use fs module to read JSON files. To install fs you can use this command and import fs to your codes

npm install fs
import fs

After installation, read parents JSON file to get their number as target sent.

app.get('/send-whatsapp-notification', (req, res) => {
var parents = fs.readFileSync('parents.json');
parents = JSON.parse(parents);

res.send(parents)
})
out put from the send message route

with this setup, we are ready to send messages to parents via WhatsApp notifications.

Merge Route with WhatsApp

In this part, we will loop the array data that we already made on the parents JSON file. our data set number is a pure number, we need to make it the same with WhatsApp Web JS format so the app won't get any errors when sending the messages. according to the previous step, the WhatsApp sender number is 6282345678900@c.us You can make a new function (if you insert other formatting depending on your location) or just simply add the designed format to the number variable.

app.get('/send-whatsapp-notification', (req, res) => {
var parents = fs.readFileSync('parents.json');
parents = JSON.parse(parents);

for (var i = 0; i < parents.length; i++) {
var numbers = parents[i].number + '@c.us';
console.log(numbers)
}

res.send(parents)
});
formated number

After formatting the numbers, now it's time to merge the WhatsApp scripts into the route to make it accessible via URL. we simply put the formated number to WhatsApp and send a message to target. here are the full scripts

Output

When you access it via /send-whatsapp-notification it will automatically send to all the numbers in parents.json. you can also simply make a new key (designed text for the bot to read your task) and execute the send message, as always depending on your needs.

Remember, this is for education only. if you use it for spamming numbers, you will get banned for sure.

Thanks for reading my story :D

--

--

Ady Bagus

Software Quality Assurance | Backend Developer | PHP Developer