Web Development

Building a two-way interactive chatbot with Twilio: A step-by-step guide

3 min read

To build a chatbot that can communicate both ways we need to do two things: build the chatbot into the web app and modify setup configurations in Twilio. To do these, follow these steps:

  1. Create an index.js file in the root directory of the project.
  2. Install the express and body-parser libraries. These libraries will be used to make a web app:
npm install body-parser --save

npm install express --save
  1. Create a web app in index.js:
// Two-way SMS Bot
const express = require('express')
const bodyParser = require('body-parser')
const twilio = require('twilio')
const app = express()
app.set('port', (process.env.PORT || 5000))
Chapter 5
[ 185 ]
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am SMS bot.')
})
//Twilio webhook
app.post('/sms/', function (req, res) {
var botSays = 'You said: ' + req.body.Body;
var twiml = new twilio.TwimlResponse();
twiml.message(botSays);
res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(twiml.toString());
})

The preceding code creates a web app that looks for incoming messages from users and responds to them. The response is currently to repeat what the user has said.

  1. Push it onto the cloud:
git add .

git commit -m webapp

git push heroku master

Now we have a web app on the cloud at https://ms-notification-bot.herokuapp.com/sms/ that can be called when an incoming SMS message arrives. This app will generate an appropriate chatbot response to the incoming message.

  1. Go to the Twilio Programmable SMS Dashboard page at https://www.twilio.com/ console/sms/dashboard.
  2. Select Messaging Services on the menu and click Create new Messaging Service:

  1. Give it a name and select Chat Bot/Interactive 2-Way as the use case:

  1. This will take you to the Configure page with a newly-assigned service ID:
  2. Under Inbound Settings, specify the URL of the web app we have created in the REQUEST URL field (that is, https://sms-notification-bot.herokuapp.com/sms/):

Now all the inbound messages will be routed to this web app.

  1. Go back to the SMS console page at https://www.twilio com/console/sms/services. Here you will notice your new messaging service listed along with the inbound request URL:

  1. Click the service to attach a number to the service:

  1. You can either add a new number, in which case you need to buy one or choose the number you already have. We already have one sending notifications that can be reused. Click Add an Existing Number.
  1. Select the number by checking the box on the right and click Add Selected:

  1. Once added, it will be listed on the Numbers page as follows:

In Advanced settings, we can add multiple numbers for serving different geographic regions and have them respond as if the chatbot is responding over a local number.

  1. The final step is to try sending an SMS message to the number and receive a response. Send a message using any SMS app on your phone and observe the response:

Congratulations! You now have a two-way interactive chatbot.

This tutorial is an excerpt from the book, Hands-On Chatbots and Conversational UI Development written by  Srini Janarthanam. If you found our post useful, do check out this book to get real-world examples of voice-enabled UIs for personal and home assistance.

Read Next

How to build basic server side chatbot using Go

Build generative chatbot using recurrent neural networks (LSTM RNNs)

Top 4 chatbot development frameworks for developers

 

 

Gebin George

Share
Published by
Gebin George
Tags: Twilio

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago