How to Build a Voice-controlled Virtual Assistant (IVR) in Node.js Using Express.js and Plivo

How to Build a Voice-controlled Virtual Assistant (IVR) in Node.js Using Express.js and Plivo

A virtual assistant can help your business if you have clients who call your phone number. Interactive voice response (IVR) helps you to automate call reception by routing callers to the most appropriate department or the agent most qualified to meet their needs. Among its many advantages, IVR can provide increased operational efficiency, a stronger brand image, and better customer insights.

A voice-controlled virtual assistant is one step ahead of the legacy Touch-Tone/DTMF controlled one because of the flexibility it allows end users. They can just speak into their phone’s microphone to provide input to control the call.

Building a voice-controlled virtual assistant using Plivo’s automatic speech recognition (ASR) feature in Node.js using Express.js is simple. This guide shows you how to set up a voice-controlled IVR phone tree to a Plivo number and manage the call flow when the call reaches the Plivo voice platform. To see how to do this, we’ll build an Express.js application to receive an incoming call and use the GetInput XML element to capture speech input and implement a simple IVR phone system.

Prerequisites

Before you get started, you’ll need:

  • A Plivo account — sign up for one for free if you don’t have one already.
  • A voice-enabled Plivo phone number if you want to receive incoming calls. To search for and buy a number, go to Phone Numbers > Buy Numbers on the Plivo console.
  • Expressjs and Plivo npm packages — run npm i -S plivo express body-parser to install them.
  • ngrok — a utility that exposes your local development server to the internet over secure tunnels.

How it works

Receive Speech Inputs

Create an Express.js application to create a voice-controlled virtual assistant

Once you’ve installed Express.js and the Plivo Node.js SDK, create a simple Express.js application to handle incoming calls on a Plivo number. To handle an incoming call, you need to return an XML document from the URL configured as the Answer URL in the application assigned to the Plivo number. The Node.js SDK can manage the XML document generation, and you can use the GetInput XML element to capture speech inputs and implement a simple IVR phone system. Use this code:

        
var plivo = require('plivo');

var express = require('express');

var app = express();

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

//  Welcome message - firstbranch

var WelcomeMessage = "Welcome to the demo app, Say Sales to talk to our Sales representative. Say Support to talk to our Support representative"

// This is the message that Plivo reads when the caller does nothing at all

var NoInput = "Sorry, I didn't catch that. Please hangup and try again later."

// This is the message that Plivo reads when the caller inputs a wrong digit.

var WrongInput = "Sorry, it's a wrong input."

app.all('/response/ivr/', function (request, response) {

 if (request.method == "GET") {

  var r = new plivo.Response();

  const get_input = r.addGetInput(

   {

    'action': 'https://'+request.hostname+'/multilevelivr/firstbranch/',

    'method': 'POST',

    'interimSpeechResultsCallback': 'https://'+request.hostname+'/multilevelivr/firstbranch/',

    'interimSpeechResultsCallbackMethod': 'POST',

    'inputType': 'speech',

    'redirect': 'true',

   });

  get_input.addSpeak(WelcomeMessage);

  r.addSpeak(NoInput);

  console.log(r.toXML());

  response.set({ 'Content-Type': 'text/xml' });

  response.end(r.toXML());

 }

});

app.all('/multilevelivr/firstbranch/', function (request, response) {

 var from_number = request.query.From;

 var speech = request.query.Speech;

 console.log("Speech Input is:", speech)

 var r = new plivo.Response();

 var params = {

  'action': 'https://'+request.hostname+'/multilevelivr/action/',

  'method': 'POST',

  'redirect': 'false',

  'callerId': from_number

 };

 var dial = r.addDial(params);

 if (speech == "sales") {

  dial.addNumber("14156667777");

  console.log(r.toXML());

 }

 else if (speech == "support") {

  dial.addNumber("14156667778");

  console.log(r.toXML());

 }

 else {

  r.addSpeak(WrongInput);

 }

 response.set({ 'Content-Type': 'text/xml' });

 response.end(r.toXML());

});

app.listen(app.get('port'), function () {

 console.log('Node app is running on port', app.get('port'));

});


        

Test the code locally

Save this code in any file (name the file something like detect_speech.js). To run this file on the server, go to the folder where this file resides and use the following command:

$ node detect_speech.js

And you should see your basic server app in action on http://localhost:3000/response/ivr/

Expose the local server to the internet using ngrok

Once you see the application working locally, the next step is to connect the application to the internet to return the XML document to process the incoming call. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive calls (5000 in this case, as our local Express.js application is running there):

$ ./ngrok http 5000

Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

Ngrok CLI

Test the link by opening the ngrok URL(https://02a9fe62aabd.ngrok.io/detect_speech) in a browser or HTTPie to check the XML response from the ngrok URL.

XML document with GetDigits XML element

Connect the Express.js application to a Plivo number

The final step is to configure the application as a Plivo voice application and assign it to a Plivo number on which you want to activate the voice-controlled virtual assistant.

Go to the Plivo console and navigate to Voice > Applications > XML, then click on the Add New Application button in the upper right.

Provide a friendly name for the application — we used “App-Virtual-Assistant” — and configured the ngrok URL https://02a9fe62aabd.ngrok.io/detect_speech as the Answer URL. Select the HTTP verb as POST, then click Create Application.

Create Plivo App for voice-controlled IVR Laravel app

Now go to Phone Numbers > Your Numbers and click on the number to which you want to assign the application. From the Plivo Application drop-down, choose the voice application you just created. Finally, click Update Number.

Assign Virtual-Assistant Plivo App

Test the application

Make a phone call to the Plivo number you selected. You should see that the VirtualAssistant Express.js application automatically routes the call to the Sales and Support departments based on the speech inputs received on the call.

And that’s how simple it is to set up a voice-controlled virtual assistant on a Plivo number and handle it using XML documents using Plivo’s Node.js SDK and an Express.js application. You can implement other use cases on the Plivo Voice platform, such as phone system IVR, call forwarding, and number masking, as your business requires.

Haven’t tried Plivo yet? Getting started is easy and only takes five minutes. Sign up today.

Get Volume Pricing

Thousands of businesses in more than 220 countries trust Plivo’s cloud communications platform

The best communications platform forthe world’s leading entertainment service

Frequently asked questions

No items found.
footer bg

Subscribe to Our Newsletter

Get monthly product and feature updates, the latest industry news, and more!

Thank you icon
Thank you!
Thank you for subscribing
Oops! Something went wrong while submitting the form.