This repository outlines how a Twilio Function
can be used to query a Google Dialogflow
bot to respond to customers engaging with the Web Chat channel. When a chat session is started, a Studio flow relays the customer message to the Twilio function, which then returns the NLP query response to Studio. The direct response from Dialogflow is delivered to the customer with a Send Message
block within the Studio flow.
If the Google Dialogflow bot detects the customer would like to speak to an agent, an Agent Escalation
intent is checked in the Studio flow and routes them to an available agent in Flex.
- Twilio Serverless Function - used to orchestrate the API requests to Google Dialogflow
- Twilio Studio Flows - custom studio flow to handle incoming messages and responses to the client side
- Twilio Flex - agent escalation use case
- Google Dialogflow - given a message (text), NLP processing determines the intent and response
- Google Cloud Platform - houses the service account credentials to perform authenticated API calls from the Twilio Function
To deploy this function, you will need:
- An active Twilio account with Flex provisioned. Refer to the Flex Quickstart to create one.
- Google Dialogflow Agent and access to the Google Cloud Platform
- npm version 5.0.0 or later installed (type
npm -v
in your terminal to check) - Node.js version 14 (type
node -v
in your terminal to check) - Twilio CLI along with the Serverless Plugin. Run the following commands to install them:
# Install the Twilio CLI npm install twilio-cli -g # Install the Serverless and Flex as Plugins twilio plugins:install @twilio-labs/plugin-serverless
Starting with the Google Dialogflow agent, we need to create a new agent and do some initial configuration of intents.
-
Navigate to https://dialogflow.cloud.google.com/
-
Login.
-
Click on the cog on the left, and then click on create a new agent.
-
Give it a name, set the default time zone and press create.
-
It should take a few seconds to create the agent. Next, let's build a couple intents:
-
We now have our agent configured with a couple intents so we are ready to move on. We will need a Google service account to access the agent via API. To set this up, navigate to the general agent options by clicking on the cog once again. Make sure you make a note of your project id, as you will need it later.
You've now created the agent on the Dialogflow console and clicked on the project ID to navigate to the GCP console.
To allow our Twilio Function to perform authenticated API requests to Dialogflow, we will need a service account. We can create these via the GCP console by following this procedure:
- Click on the Navigation Menu on the top left, and under "IAM and Admin", click on "Service Accounts":
- Click on the create service account on the top, fill in the form, and then click “Create and Continue”:
- Click on "Select a role", pick "Dialogflow Service Agent", then press "Continue", and finally press"Done" to complete this process.The account should look something like this:
- Finally, click on the three dots under "Actions" then "Manage Keys", click on "Add Key" then "Create New Key". Pick a JSON type key and press “Create”. Your browser will download a JSON file automatically. We will use this JSON file within our Twilio Function in the following section.
We now need to deploy the function that will facilitate the API calls to the Google Dialogflow Bot. The function is called from the Studio flow responsible for handling the Web Chat channel. After querying the bot with the customer input (message), a JSON payload containing the Dialogflow results are available to the Studio flow. For further information on the technical functionality, please review ./functions/gdf-bot.protected.js
.
-
While at the root directory, install package dependencies, and then rename
.env.example
. Note: as part of the package installation, the npm package@google-cloud/dialogflow
is used to send API requests to the Dialogflow bot.# Install required dependencies npm install # Rename example env file cp .env.example .env
-
Open
.env
with your text editor and set the environment variables mentioned in the file.ACCOUNT_SID=ACXXXXXXXXXXXXXXXXXX AUTH_TOKEN=aXXXXXXXXXXXXXXXX
-
Rename the credentials JSON downloaded from the Service Account creation to
gcp_sa.private.json
and add it into the/assets
directory. -
Deploy the Twilio function to your account using the Twilio CLI:
twilio serverless:deploy # Example Output Deploying functions & assets to the Twilio Runtime ⠇ Creating 1 Functions ✔ Serverless project successfully deployed # Deployment Details Domain: https://web-chat-dialogflow-xxxx-dev.twil.io Service: web-chat-dialogflow (ZSxxxx) # ..
Now we need to create a new Studio flow to handle the incoming messages for Web Chat (note: editing the existing Web Chat flow configured into Flex is also an option).
- Chat_Variables - sets the incoming message text to a variable named
customerMessage
- send_to_GDF - configured to execute the Twilio function deployed in the previous step, with a Function Parameter of
customerMessage
set to{{flow.variables.customerMessage}}
- Set_Response_Text - sets two more variables based off the response from Dialogflow:
fulfillmentText
:{{widgets.send_to_GDF.parsed.fulfillmentText}}
intent
:{{widgets.send_to_GDF.parsed.intent.displayName}}
- check_escalation - checks the
intent
flow variable if an agent escalation is needed - send_bot_message - delivers the bot response to the Web Chat channel
- Send_Final_Bot_Message - delivers final message from bot indicating the session will be handed off to an agent
- send_to_flex_1 - sends the chat session over to Flex to be picked up by an available agent
After setting up the Studio flow outlined above, it is time to configure Flex to route incoming Web Chat messages to this flow.
- Navigate to the Flex product page in the Twilio Console.
- Click on
Manage
>Messaging
. - Under the
WebChat
row entry, click theEdit
button and select the new Studio flow created in the previous step. - Flex is now configured to route incoming WebChat messages to the new Studio flow.
To test this solution:
- Launch Flex from the Twilio console.
- Ensure you are set to
Available
to enable the ability to receive incoming WebChat tasks as an agent. - Launch the demo chat widget from the admin screen in Flex.
- Enter a chat message to observe the responses from Google Dialogflow.
- Enter one of the training phrases for the
Agent Escalation
path, then view the incoming task in Flex.
- Guidance for this approach was sourced from this Twilio blog article: Add Natural Language Processing Capabilities to your Twilio Flex Contact Center IVR and Messaging Channels with Google Dialogflow ES
May 23, 2022
- Updated README with overview, setup instructions and screenshots.
This software is to be considered "sample code", a Type B Deliverable, and is delivered "as-is" to the user. Twilio bears no responsibility to support the use or implementation of this software.