29 Apr 2018

Phone your Customers and Talk to them using a Chat Bot

Would you like to be able to phone your customers and let a chat bot talk to them? This is relatively easy to do and I will show you how.
There exist many reasons for communicating with your customers.


Giving information. Some examples are:

  • Courier delivery updates
  • Login issues
  • Banking events
  • Traffic alerts
  • Schedule changes

and requesting information (who, what, when, where, why). Some examples are:

  • Courier delivery confirmation
  • Satisfaction / Feedback survey
  • Checking attendance
  • Status requests
  • Location requests


Not all customers are able to or desire to use modern technologies to interact with your system, so a phone call gets around this problem. But the call must be life like and easy to use to be successful.

The main concerns to be considered, and the service I have used are as follows:

Concern Service
How to Initiate the call. Azure Function
Starting and controlling the phone call. Twilio
Converting the customer speech into text. Twilio
Converting the application text into speech. Twilio or Amazon Polly
Understanding the customer text DialogFlow
Determining what to say to the customer. DialogFlow
Persisting the data. Twilio Session during call
Integrating the services Azure function

These concerns can be satisfied by various different online services so you don’t need to develop them all yourself. I will use quite a simple implementation to show a working example which will ask our user for feedback on a visit to a theme park.

The application will flow like this:

Example application


Example Application


The logic in the conversation


DialogFlow is a free Google product which allows the intent of human speech / text to be understood. This makes it easier to write conversational bots. The bot I have written is very basic and barely scratches the surface of what DialogFlow can do. The capabilities of DialogFlow need to be understand well as it will influence how much logic you can set up inside it, and how much needs to be coded in your bot.

If you are new to DialogFlow then check out their tutorials:


The DialogFlow agent I have written asks the following 3 questions:

  1. Would you like to participate? A ‘No’ response ends the bot.
  2. Did you visit for a special event ?
  3. And finally get a rating of the experience.


Three Contexts are moved through during the conversation to control which Intents can be matched by the user input. For example if the user said “yes” intent 2.0 would only be matched if the current context was “SpecialEvent”.

Dialog flow contexts

The agent also has the following Entities which are set up to match the possible responses to their related questions.

Dialog flow Entities

You can restore the agent from a file in my GitHub repo and you can try it out here.

Start the conversation off by saying “startcall”.

DialogFlow Conversation

Talking to DialogFlow from a C# .net application


We are going to use Azure functions to pass on messages from Twilio to Dialogflow and then send a response back to Twilio. To allow our function to talk to DialogFlow we are going to add a nuget package ApiAiSDK. We are then able to call dialog flow as follows:

C# code

The response includes the speech to say to the user and any parameters which contain the information we have collected. There is also other information such as the context and intent which can be useful for testing.

The application solution comprises of following projects:

Project Description
FeedbackBotAzureFunctions Function StartCall makes a request to Twilio to make a phone call. Function MsgIn receives a request from Twilio. It passes the user text to the bot, then returns the bot response to Twilio. Any state required to be kept is stored in a cookie passed to and from Twilio.
FeedbackBot Handles the user conversation with DialogFlow
IntegrationTests Tests to run through a conversation and to check the intents and contexts are correct with both DialogFlow and the Azure function.
BotFrameworkAPI Allows an alternate route into the bot so that an interactive test can be performed using Microsoft’s Bot Framework Emulator

The azure functions will need to be published to Azure. You can learn more about that here

Setting up Twilio


Now that we have our application code we need to set up Twilio to talk to it. Twilio allow you to sign up for a free account.

Sign up for a Twilio Account here

  1. Create a project. Make a note of the Account SID and auth token
  2. Choose get started with twilio for “Programmable voice”
  3. Get a phonenumber. Make a note of it.
  4. Confirm programming language as C#.
  5. Now click on Numbers and select Manage Numbers.
  6. Now click on Tools and TwiML Apps and create a new app.
  7. The Voice Request URL can then be filled in with our MsgIn azure function URL.

We now need to go back to our function project and fill in the following in the StartCall azure function:

  • PhoneNumberToCall
  • TwilioAccountSid
  • TwilioAuthToken
  • TwilioProgVoiceApplicationSid
  • phoneNumberCallIsFrom

We also need to set the following in Bot.cs so that the bot talks to your DialogFlow
* DialogFlowAccessToken

The Audio


The Twilio voices are a little robotic. There are more lifelike voices available from 3rd parties. Here is a recorded call using Twilio voice: Twilio Voice Recording Note: you may need to download the mp3 to your pc if they won’t play in chrome.

One of which is Amazon Polly. You can listen to some of the voices here. Here is a recorded call using Polly Brian voice: Amazon Polly Voice Recording

Another text to speech service is available here from Microsoft.

Conclusions


It’s pretty straight forward to write a bot you can talk to once you understand how the various components work and can be put together. Using a 3rd party voice gives less robotic speech, but it does add a slight delay.

This blog post was inspired by this article.

blog comments powered by Disqus

Image attribution: "Modem" by Brad Montgomery is licensed under CC BY 2.0

About Me


My first computer was a Commodore VIC-20, I had great fun trying to code text adventures and side scrolling shoot ‘em ups in BASIC. This helped me lead the way as the first in my school to pass a computer exam.

Currently I work as a Senior Software Engineer in Bedford for a FTSE 100 Company. Coding daily in C#, JavaScript and SQL. Outside of work I work on whatever is interesting me at that time.