Before you go, check out these stories!

Hackernoon logoCreating a WhatsApp bot Using NodeJs, and Twilio API by@worm4047

Creating a WhatsApp bot Using NodeJs, and Twilio API

Author profile picture

@worm4047Abhishek Chaudhary

This post is a prerequisite for an upcoming post. Here I’ll be creating a Whatsapp echo bot using Twilio and Nodejs. I’ll be making use of so as to save myself from the burden of going through the environmental setup.

Now, let’s get started 🏃‍

🔑 Accounts and Keys

First, Sign up for Twilio

Once you’re done verifying your phone number, select Products > Programmable SMS and then continue to name your project.
Feel free to skip steps for adding teammates — you won’t need that for now.

You must now take note of some authentication keys you’ll need for building the WhatsApp bot 👇

The final step — setup your WhatsApp Sandbox here — choose any number, and join your sandbox following instructions on the page. 👇

Aaaaaand you’re done with credential setup! Don’t worry, that was the toughest part of this tutorial 😛

🚀 Getting Started

So that we don’t spend too much time on setup, we’ll be using! you can use within your browser. Head over here, and click on new Repl 👇

Select express from the language dropdown and name your Repl appropriately (or not !!). After creating it you’ll be automatically redirected to the console.

Replace the code in index.js with the following code 👇

Now start the server.
You’ll get an error — Error: Cannot find module ‘twilio’
To resolve this you’ll need to add twilio module in from packages menu on the left menu bar. Search for package there and add it. Hit Restart. An interactive browser window will open up and show default route message.

Let’s go back to the WhatsApp Sandbox, and put in a webhook URL for incoming messages.

We’ll be using /incoming route to receive messages sent to the bot, so we will be redirecting them to the above-shown URL (Your’s will be different, obviously !!)

Add /incoming to the browser URL and save that into sandbox redirect URL.

After saving the configuration in the Twilio Sandbox, you’ll need to add your phone number to sandbox participants by sending the code to the mentioned number. 👇

After adding participants try sending any message from your number to the Sandbox number and observe the consoles log. 👇

🛠️Replying to incoming messages

To create an echo bot we’ll need to send back the incoming messages. For that purpose, we’ll need to use twiml.
Add the following code in /incoming route handler.

const twiml = new MessagingResponse();
res.writeHead(200, {‘Content-Type’: ‘text/xml’});

Index.js at this point will look like this. 👇

Hit Restart then and send the message again to the sandbox number. Now you’ll be able to receive the same message back from the bot.

⚡ What’s next?

There you have your WhatsApp echo bot. 
In case you get stuck you are free to use my . Just replace Account Sid and Authentication Token with your own.

Needless to say, it can be modified to create whatever you wish to do. Ranging from notification services to Wikipedia bots. 

One particular scenario I’ll be sharing in a few days so do stick around.


Join Hacker Noon

Create your free account to unlock your custom reading experience.