Menu

Expand
Rate this page:

Connect Twilio with your Dialogflow Agent

Twilio now supports a One-Click integration between Google's Dialogflow Essentials (ES) and Twilio, which simplifies the process of connecting a Dialogflow ES agent with Twilio Programmable Voice. Learn more here.

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.

How it works

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.

  1. Caller asks a question.
  2. Twilio streams the audio stream via Websocket to Node app running in Google AppEngine.
  3. The Node app streams the audio to Dialogflow API.
  4. The Node app receives the audio response stream from Dialogflow.
  5. The Node app streams the audio back to Twilio.
  6. Twilio plays audio back on the phone call and the caller hears the response. This process continues until the caller hangs up or the end of interaction is reached.

Integration Guide

This guide provides step by step instructions to integrate Twilio with Google Dialogflow.

Prerequisites

Please 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 ES.
  • 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:
    • Click on the Project ID in the Dialogflow Agent settings
    • This will open a new window on Google Cloud Platform. From the sidebar select "API" and then "Credential". You'll fine
    • In the page that open, you will find a service account called "Dialogflow Integrations". Click on it
    • In the page that opens, delete the existing key and click on "Add Key"
    • Select "JSON"
    • A new json file will be downaloded
  • 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.

Setting up and Deploying your App to Google AppEngine

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 earlyer, 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.

exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.VoiceResponse();
  const dialogflow = JSON.parse(event.dialogflowJSON);
  switch (dialogflow.intent.displayName) {
    case 'speak-to-an-agent':
      twiml.dial(process.env.AGENT_NUMBER);
      break;
    default:
      console.error(`Intent: "${dialogflow.intent.displayName}" (${dialogflow.intent.name}) was not handled.`);
      twiml.hangup();
  }
  callback(null, twiml);
};

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. Please 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

Getting Started with Twilio using the CLI

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"

Getting Started with Twilio with on-premises system

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 simply 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.

Pricing

Billing for Twilio usage is additional to and will occur separately from Dialogflow billing. 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 cost based on list price is:

$1/month for Twilio Local Inbound Number + $0.0085/min to receive calls +

$0.004/min for Media Stream minute

To learn more about our pricing, please visit https://www.twilio.com/voice/pricing/us.



Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more