Build a WhatsApp bot in 10 minutes using Node.js and Twilio

Michael Ekpang
Sep 9, 2019 · 4 min read
Image for post
Image for post
Image credit: Unplash.com

WhatsApp helps over 1.5 billion users keep in touch with their friends and close ones, Businesses can leverage that to reach many users most naturally via chatting. In this tutorial, we are going to create a WhatsApp bot using Node.js and Twilio API.

The bot will make use of Google search API such that we can search for anything on Google directly on WhatsApp.

Let’s get started

The first step is to create our custom Google search engine.

The Custom Search JSON API requires the use of an API key, so let’s get our key. Click here

Image for post
Image for post

The next step is to setup your custom search engine Click here

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Switch the ‘search the entire web’ button to on, This will make your search more in-depth.

Image for post
Image for post

Setting up Twilio sandbox environment

The next step is to setup a sandbox environment on Twilio, follow the steps below.

Image for post
Image for post
Image for post
Image for post

The final step is to setup your WhatsApp Sandbox environment. Click here

Trust me this was the hardest part, now let’s get down to business.

Let's create a directory for our project.

On your terminal run mkdir whatsapp-bot or create a directory manually for the project.

Create a .env file inside your project directory and put the following:

Image for post
Image for post
.env file

Create your package.json with the following dependencies

create a .babelrc file and add this code.

Run npm install to install all your dependencies

Copy and paste this code inside server/server.js file

Here we are just creating an express server that runs on port 3000.

Your folder structure should look something like this

Image for post
Image for post

Run npm start to start the server, your app should start running on port 3000

Next let’s download ngrok. We need ngrok to make our local server publicly accessible on the internet.

You should see ngrok started with status online.

Image for post
Image for post
Image for post
Image for post

Note the api/v1/incoming that is how our route will be structured

Next, create a WhatsappBot.js file inside the controllers folder

Your folder structure should be like this

Image for post
Image for post

Next, let’s import the Google API and Twilio libraries into our WhatsappBot.js

Let’s load our environment variables and also initial our Twilio account by passing in our account id and auth token.

From the image below, we initialize the MessgingResponse object, we get the query the user is sending from req.body.Body, call the Google customsearch method and passing it our options parameter.

Then, get the first content from the search result and send back to the user.

See the complete code below

Setup the route

Here we import the WhatsAppbot controller and set our post route

Restart your server and try sending a message via WhatsApp to your endpoint.

If everything goes well you should get a response back

Image for post
Image for post

Congratulation you have just created your first WhatsApp bot, go ahead and explore.

The Andela Way

A pool of thoughts from the brilliant people at Andela

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store