In today’s digital age, businesses are constantly seeking ways to improve their customer engagement and experience. Integrating communication tools with conversational platforms is one way to achieve this. Twilio, a cloud communications platform, and Dialogflow CX, a conversational AI platform, have joined forces to offer a seamless integration for businesses looking to build powerful conversational experiences. With this integration, businesses can leverage Twilio’s messaging and voice capabilities, combined with Dialogflow CX’s natural language understanding and intent recognition, to create sophisticated conversational experiences that can cater to a wide range of customer needs.

Pre-requisites

  • To get started with our project on Dialogflow CX, you’ll need to create an account with billing enabled and set up an agent. 
  • Additionally, you’ll need a Twilio account with a purchased mobile number and available credit for making calls.For testing purposes, we recommend using textnow.com, though you’re free to use any other service or your own mobile phone to create a call.
  • Lastly, make sure you have Ngrok installed on your system.

Steps

Step 1. Grant Twilio Permissions to access your Dialogflow CX agent
  • Before you can initiate the one-click integration process, there is a one-time manual step that you need to complete. This step involves granting permission to Twilio’s production service account to run your Dialogflow agent. Once you’ve completed this step, you’ll be able to initiate the one-click integration with ease.
  1. Log in to your Google Cloud console
  2. Select the Google Project in which you configured your Dialogflow CX Agent
  3. Navigate to IAM & Admin > IAM from the left navigation
  4. Click +Grant Access near the top of the page
  5. Under”New Principals”, add virtualagent-ccai-prod@dialogflow-prod-env.iam.gserviceaccount.com as a member with the following roles:
    • Dialogflow API Reader
    • Dialogflow API Client
  • Click Save
Step 2. Connect Twilio to your Dialogflow CX Agent with the one-click integration

Great job on completing the prerequisite steps! Now you’re ready to use the one-click integration process to connect your Twilio Dialogflow CX Connector to the Dialogflow CX agent of your choice. This should be a seamless process and should allow you to quickly start using the integration.

  1. Log in to your Google Dialogflow CX cloud console.
  2. On the left-hand side navigation bar, click on the “Manage” option.
  3. From the drop-down menu, select “Integrations”.
  4. Look for the “One-click Telephony” section, and then select “Twilio” from the options available.
  1. give your integration a new name. This can be anything you like, but make sure it’s something descriptive so you can easily identify it later on.
  2. Select the Environment and Language for your Dialogflow CX agent. This is important because it will determine how your agent will interact with users.
  3. Once you’ve entered all the necessary information, click on the “Add” button. This will start the integration process and launch the Twilio Console page, where you can configure your Twilio account and connect it to your Dialogflow CX agent.
Step 3.  Configuration steps in the Twilio Console

Once you’ve followed the steps in the Google Cloud Console, you’ll be directed to the Twilio Console to complete the Dialogflow integration. You should see a page titled “Dialogflow CX one-click integration”, which includes the Dialogflow CX Connector with pre-populated values.

On this page, you can configure your integration settings, such as the phone number you want to use for telephony interactions and other options. It’s important to review these settings carefully to ensure that your integration is set up correctly and working as expected. Once you’ve made any necessary changes, you can save your settings and start using your Twilio Dialogflow CX Connector right away.

To set up your Twilio Dialogflow CX Connector, you’ll need to configure the following settings:

  1. Connector Friendly Name: This is a unique name that you’ll use to identify your Connector in Twilio Studio or using TwiML. Let’s say you name it “test_demo”.
  2. Status Callback URL (optional): This is the URL where you want Twilio to send requests on call status, intents triggered, sentiment scores, and more. For this tutorial, you can link it to Twilio Functions.
  3. Welcome Intent Friendly Name: This is the name of the Dialogflow CX agent welcome intent you want to trigger when connecting the caller to an agent. If you don’t have a custom welcome intent, you can use the system-generated default welcome intent called WELCOME.
  4. Bot Voice: This setting indicates the Text-To-Speech voice to use when synthesizing audio responses from the bot. You can check out Google’s documentation for a list of voices currently supported for Dialogflow.
  5. Sentiment Analysis: This setting determines whether or not Dialogflow will perform sentiment analysis on the end-user input. If it’s set to ON, sentiment scores will be relayed in the status callbacks. Note that sentiment analysis availability depends on the language support in Dialogflow CX.

Once you’ve configured your Connector with all the details, click on “Next”. You’ll then see a “Review Information” screen prompting you to review your Connector configuration. If something is incorrect, you can click on “Previous” to update the Connector settings. If everything looks good, click on “Confirm Configuration Details”.

After configuring your Twilio Dialogflow CX Connector, you’ll see a modal pop-up asking for your consent to connect your Connector to your Dialogflow CX agent. If you don’t want to proceed with the setup, you can click on the X at the top right corner of the modal pop-up or close your browser tab.

If you want to continue, click on “Agree and connect to Google” to finish the integration setup.

Congratulations! Your Dialogflow CX integration with Twilio is now complete. You can verify that the Connector instance is completed by checking under the Dialogflow CX Connectors section in the Twilio Console.

Once you’ve completed the integration between Dialogflow CX and Twilio, Twilio automatically creates a new Studio Flow with a Connect Virtual Agent Widget in it.

To set the flow, follow these steps:

  • Open the Twilio Console.
  • Click on “Develop” and select “Studio” from the dropdown menu.
  • Click on “Flows” and select the name of the flow that you want to use.
  • Set the flow as desired, using the Connect Virtual Agent Widget that was automatically created for you as part of the integration.

Below is the screenshot of connecting an agent with Twilio.

To connect an outgoing call, you can use the “Make Outgoing Call” widget from the Widget library in Twilio Studio. You can then connect it with the Rest API as shown in the image below:

This will allow you to make outgoing calls using Twilio and connect them with your Dialogflow CX agent for natural language processing and intelligent responses.

Rest API:

Install the twilio library in your system. For installation in the Ubuntu system use this command.
pip install twilio

Script:
from twilio.rest import Client

# Your Account Sid and Auth Token from twilio.com/console
account_sid = <YOUR ACCOUNT SID>
auth_token = <YOUR ACCOUNT AUTH TOKEN>

# Create a Twilio client object
client = Client(account_sid, auth_token)


execution = client.studio \
                  .v2 \
                  .flows(<FLOW ID>) \
                  .executions \
                  .create(to= <TO NUMBER>, from_= <TWILIO NUMBER>)

print(execution.sid)

Now, the connection of Twilio of Dialogflow is done using the integration we setup.

You can test it by creating calls from your mobile to the number purchased from twilio. You will hear voice from Dialogflow agent.

Feel free to comment your doubts/questions. We would be glad to help you. If you are looking for Chatbot Development or Natural Language Processing services then do contact us or send your requirement at letstalk@pragnakalp.com. We would be happy to offer our expert services.

Categories: chatbot Dialogflow Dialogflow CX Dialogflow Tutorial NLP Python

2 Replies to “Integrating Twilio with Dialogflow CX to Enable Voice Calls”

  1. I did this end to end configuration, this works only for inbound calls. For the out bound calls, call gets connects but is dropped after a short delay. can you help?

    1. Hi Ali,

      You are able to connect via inbound call, indicating that the connection between the virtual agent and Twilio flow has been successfully established.

      Please verify the connection between the “Make Outgoing Call” and “Connect Virtual Agent” widgets.

      Feel free to reach out if you need any more assistance or have additional questions.

      Thank You

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*