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.
Our simple example application will ask our user for feedback on a visit to a theme park. And the application will flow like this:
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:
- https://dialogflow.com/docs/getting-started/building-your-first-agent
- https://developers.google.com/actions/dialogflow/first-app
The DialogFlow agent I have written asks the following 3 questions:
- Would you like to participate? A 'No' response ends the bot.
- Did you visit for a special event ?
- 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".
The agent also has the following Entities which are set up to match the possible responses to their related questions.
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".
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:
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
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
- Create a project. Make a note of the Account SID and auth token
- Choose get started with twilio for "Programmable voice"
- Get a phonenumber. Make a note of it.
- Confirm programming language as C#.
- Now click on Numbers and select Manage Numbers.
- Now click on Tools and TwiML Apps and create a new app.
- 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 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.
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.