Twilio now supports a One-Click integration with Google's Dialogflow CX, which simplifies the process of connecting a Dialogflow agent with Twilio Programmable Voice.
This guide outlines how you can connect your Dialogflow Agent with your Twilio phone number. This partnership enables you to build and provide natural and rich conversational experiences over the phone. Twilio provides connectivity over PSTN or SIP with the ability to program call flows for your Dialogflow Agent.
Twilio Media Streams provides the ability to access the audio in real-time from your active phone calls. Media Streams powers the integration between Twilio and Dialogflow. The Dialogflow Agent receives the stream, processes the request for intent, and responds with audio. It can then optionally escalate back to your Twilio application so that you can customize subsequent actions in the call flow, such as forwarding the call to an agent or routing the call into a conference.
The following pictures depict a very high-level interaction between a Caller, Twilio and Google Dialogflow.
This guide provides step by step instructions to integrate Twilio with Google Dialogflow.
Make sure the following prerequisites are completed before continuing with the rest of the instructions in this guide:
Setup your Dialogflow Agent. You can build your own or use a pre-built Dialogflow Agent. For the purpose of this guide, make sure to select region US - USA (Global)
in the top left corner of Dialogflow.
Copy the Google Project ID mapped to your Dialogflow agent, you will need it later. You can find this in the settings page for your Dialogflow Agent
Download your Google Credentials with permissions to access Dialogflow API and name the file as google_creds.json, you will need it later. To find this:
Setup and configure gcloud command line tool to be able to deploy code in Google AppEngine.
Sign up and activate your Twilio account.
Copy Twilio Account Sid and Auth token from Twilio console page.
Setup Twilio CLI using the Quick Start.
We've provided a Node application that uses Twilio Media Streams to integrate with Dialogflow. The following instructions walk you through configuring and deploying the app into Google AppEngine.
Clone the Github repo
git clone https://github.com/twilio/media-streams
Change directories
cd media-streams/node/dialogflow-integration
Install the dependencies
npm install
Copy the google_creds.json file you downloaded earlier, into this folder. Configure the app environment: this command will prompts you for information that will be used by the app in runtime,
npx configure-env
The Account SID & Auth Token are available at https://www.twilio.com/console
The URL requested in the last step is used by the app to redirect the phone call when the end of interaction is received from Dialogflow. Twilio expects TwiML as a response to the URL provided. As an example, you could return TwiML <Dial> to dial a number and connect the call or <Enqueue> to Queue the call with TaskRouter and route the call to a Flex Agent. If no URL is provided, the call will disconnect. As a starting point you could create a Twilio Function with the following code and use the URL created by the function.
1exports.handler = function(context, event, callback) {2const twiml = new Twilio.twiml.VoiceResponse();3const dialogflow = JSON.parse(event.dialogflowJSON);4switch (dialogflow.intent.displayName) {5case 'speak-to-an-agent':6twiml.dial(process.env.AGENT_NUMBER);7break;8default:9console.error(`Intent: "${dialogflow.intent.displayName}" (${dialogflow.intent.name}) was not handled.`);10twiml.hangup();11}12callback(null, twiml);13};
Note: This code assumes that the intent that triggers the end of interaction as a result of the caller requesting to speak to the agent is named speak-to-an-agent
.
Deploy the App to Google App Engine with command
gcloud app deploy
Verify all the details and type Y and press Enter to initiate the deployment process. The deployment process typically takes between 2-5 minutes.
Once the deployment is completed successfully, you will see a success message. Copy the URL displayed, as it will be used when configuring Twilio number.
At this point your app is deployed in AppEngine and ready to accept traffic. You can validate to make sure your app is serving incoming traffic by running command:
gcloud app browse
In this section you will purchase a Twilio phone number and configure it with the URL you received in the previous section after deploying to App Engine. This will be completed using the Twilio CLI.
If you have already have a number that you'd like to use, you can skip both the Search and Purchase steps.
Search for a number for available numbers to choose from in the 650 area code that provide voice capabilities. Change the area code to match your needs.
twilio api:core:available-phone-numbers:local:list --area-code="650" --country-code=US --voice-enabled
Purchase a Twilio Phone Number of your liking. Keep track of the SID (it starts with a PN) that is displayed on creation, you'll use that in the next step.
twilio api:core:incoming-phone-numbers:create --phone-number="+16505551234"
Set up your phone number to point to your AppEngine instance. Replace the SID with your actual SID. Replace <YOUR APP ENGINE URL> with your AppEngine URL.
twilio api:core:incoming-phone-numbers:update --sid=PNXXXXXXXXXXXXXXXX --voice-url="https://<YOUR APP ENGINE URL>/twiml"
If you currently have an on-premise system for your IVR and contact center, you have a couple of options to integrate your on-premise system with Twilio to leverage Google Dialogflow.
You can follow the instructions described in this document and once you successfully purchased and configured a Twilio phone number, you can forward the calls from within your existing infrastructure to the configured Twilio phone number.
Alternatively, you can set up a SIP Domain using the instructions from this tutorial and route calls from your existing infrastructure to Twilio over SIP.
Billing for Twilio usage is additional to, and will occur separately from, Dialogflow CX usage billing from Twilio. (Certain other Google advanced features like analytics and metrics are also charged separately by Google). With Twilio you only pay for what you use. For example, if, based on the instructions in this guide, you purchased a Twilio US Local Inbound number, your Twilio and Dialogflow CX costs based on the current list price as of August 1, 2023, would be:
$1/month for Twilio Local Inbound Number + Inbound PSTN minutes + $0.085/min* to receive calls in Dialogflow
*Billing for Dialogflow is rounded up to the next minute, (e.g. 65 seconds = 2 minutes Dialogflow charges billed by Twilio)
To learn more about our pricing, visit https://www.twilio.com/en-us/voice/pricing/us.