Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

TwiML™ Voice: <VirtualAgent> with Dialogflow CX


The <VirtualAgent> TwiML noun, used with Twilio's Google Dialogflow CX Connector Add-ons, allows you to connect callers to a Google Dialogflow agent. <VirtualAgent> nests inside the \<Connect> verb to direct a call to a Dialogflow agent.


Set up the integration between Twilio and Dialogflow CX

set-up-the-integration-between-twilio-and-dialogflow-cx page anchor

Before using <VirtualAgent>, you must complete the Dialogflow CX one-click integration.


<Connect> attributes

connect-attributes page anchor

<VirtualAgent> is a TwiML noun that is nested within the <Connect> verb.

<Connect>'s action and method attributes allow you to execute a new set of TwiML instructions for a call after the <Connect><VirtualAgent> session ends.

Attribute NameAllowed ValuesDefault Values
actionRelative or absolute URLCurrent TwiML document URL
methodGET, POSTPOST

action

action page anchor

The action attribute takes an absolute or relative URL as a value. After a <Connect><VirtualAgent> session ends, Twilio will send an HTTP request to this action URL and will expect a new set of TwiML instructions in the response. If you do not provide an action attribute, Twilio will request TwiML from the current TwiML document.

The following code sample is an example of using the action attribute with <Connect> and <VirtualAgent>.

Specify an action URL with <Connect>

specify-an-action-url-with-connect page anchor
Node.js
Python
C#
Java
PHP
Ruby

_12
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_12
_12
const response = new VoiceResponse();
_12
const connect = response.connect({
_12
action: 'https://myactionurl.com/twiml'
_12
});
_12
connect.virtualAgent({
_12
connectorName: 'project',
_12
statusCallback: 'https://mycallbackurl.com'
_12
});
_12
_12
console.log(response.toString());

Output

_10
<?xml version="1.0" encoding="UTF-8"?>
_10
<Response>
_10
<Connect action="https://myactionurl.com/twiml" >
_10
<VirtualAgent connectorName="project" statusCallback="https://mycallbackurl.com"/>
_10
</Connect>
_10
</Response>

For Dialogflow CX, this HTTP request's body will contain additional information about the result of the <Connect><VirtualAgent> session, along with Twilio's standard request parameters. A <Connect><VirtualAgent> session ends (and thereby triggers Twilio's HTTP request to the action URL) in the following ways:

  • The Dialogflow CX agent session ends successfully and the caller did not hang up
  • The conversation with the Dialogflow CX agent requires a live agent handoff
  • The caller hangs up
  • An error occurred during the VirtualAgent session

Twilio's request to the action URL

twilios-request-to-the-action-url page anchor

The body of Twilio's request to the action URL contains properties about the <Connect><VirtualAgent> session, along with Twilio's standard request parameters. You can use this information to configure the call behavior after a <Connect><VirtualAgent> session, or to log information for analytics purposes.

PropertyDescription
VirtualAgentProviderThe VirtualAgent provider e.g. DialogflowCX
VirtualAgentStatusThe reason a VirtualAgent session ended (see below for more information)
VirtualAgentProviderDataJSON string containing the provider specific action callback event data (see below for more information)
VirtualAgentErrorCodeA Twilio error code. See Twilio's Error and Warning Dictionary for more information. This property will only appear if the value of the VirtualAgentStatus property is failed.
VirtualAgentErrorA message describing the error that caused the VirtualAgent session to fail. This property will only appear if the value of the VirtualAgentStatus property is failed.
VirtualAgentProviderErrorA Dialogflow-provided error message. This property will only appear if the value of the VirtualAgentStatus property is failed and the VirtualAgentErrorCode is 32601.

VirtualAgentStatus

This property describes why the VirtualAgent session ended. Possible values are:

  • completed: VirtualAgent session was terminated by one of the following:
    • The Dialogflow CX agent indicated end of conversation (by matching the end of conversation intent)
    • The caller hung up
  • live-agent-handoff: VirtualAgent returned a live agent handoff response
  • failed: An error occurred during VirtualAgent processing

VirtualAgentProviderData

This property is a JSON string containing Dialgfow-specific data from the agent interaction:

  • ConversationId: Unique identifier for this conversation provided by Google
  • EndUserId: Unique identifier for the end user participant provided by Google
  • AgentHandoffParameters: Parameters included from the Dialogflow CX agent if the agent returned a live agent handoff response

Respond to Twilio's request to the action URL

respond-to-twilios-request-to-the-action-url page anchor

If you want a call to continue after a <Connect><VirtualAgent> session ends, your application should provide new TwiML instructions based on the <Connect><VirtualAgent> session.

This optional <Connect> attribute indicates the HTTP request method to be used when Twilio sends a request to the action URL. If you don't specify a method attribute in <Connect>, POST is used by default.

The allowed values are POST and GET.


<VirtualAgent> attributes

virtualagent-attributes page anchor

The table below lists all of the <VirtualAgent> attributes with their allowed values. The Dialogflow CX integration allows additional customization of the call flow using <Parameter> and <Configuration> TwiML nouns.

NameAllowed ValuesRequired?Default Value
connectorNameA stringYesempty
statusCallbackAn absolute URLNoempty
statusCallbackMethodGET, POSTNoempty

The connectorName is a string, configured in Dialogflow CX connector instance in the Unique Name field. You can review your Dialogflow CX connector instances in the Twilio Console(link takes you to an external page).

The statusCallback attribute is an absolute URL where Twilio will send HTTP requests. While the Dialogflow agent interaction is in progress, Twilio will send HTTP requests to this URL each time an intent matches in the Dialogflow agent's conversation.

The body of Twilio's requests to your statusCallback URL contains information about intents, transcripts (caller text vs. virtual agent text), sentiment analysis (if enabled), and custom parameters that were sent to Dialogflow to build custom logic.

The body of Twilio's request to your statusCallback URL contains the following properties:

ParameterDescriptionTypeRequired?
AccountSidTwilio Account SIDstringYes
CallSidTwilio Call SIDstringYes
TimestampTime of the event, conformant to UTC ISO 8601 TimestampstringYes
StatusCallbackEventThe event type that triggered the status callback request (e.g. intent)stringYes
VirtualAgentProviderThe VirtualAgent provider (e.g. DialogflowCX)stringYes
VirtualAgentProviderDataJSON string with data from Dialogflow CX. See more information below.JSON stringYes

VirtualAgentProviderData

virtualagentproviderdata page anchor

This parameter in the status callback is a JSON string containing the Dialogflow CX-specific intent event data. The JSON string can contain the following fields:

KeyDescriptionTypeRequired
ConversationIdUnique identifier for this conversation provided by GooglestringYes
EndUserIdUnique identifier for the end user participant provided by GooglestringYes
ReplyTextThe Dialogflow CX agent's response to the callerstringYes
LanguageCodeThe language that was triggered during intent detectionstringNo
ParametersJSON object with key-value pairs containing the collected session parametersobjectNo
ConfidenceThe intent detection confidence. Values range from 0.0 (completely uncertain) to 1.0 (completely certain)numberNo
ResolvedInputFinal text input matched against. This value may be different from the original input because of spelling correction or other processing.stringNo
IntentIdIntent ID provided by GooglestringNo
IntentDisplayNameThe human readable name of this intentstringNo
SentimentAnalysisScoreSentiment score between -1.0 (negative sentiment) and 1.0 (positive sentiment)numberNo
SentimentAnalysisMagnitudeA non-negative number in the [0, +inf) range, which represents the absolute magnitude of sentiment, regardless of score (positive or negative)numberNo

This is the HTTP method to use when requesting the statusCallback URL. Accepted values are GET or POST, and the default value is POST.


You can pass custom key-value pairs to your Dialogflow CX agent by using the nested <Parameter> TwiML noun within <VirtualAgent>. The <Parameter> noun allows you to send custom session parameters(link takes you to an external page) to your Dialogflow CX virtual agent, which can be referenced in your agent as $session.params.parameter-name.

<Parameter> has two attributes: name and value, both of which must be used every time you use <Parameter>.

For example, if you want to provide the end caller with a personalized agent greeting, you can supply a nested <Parameter/> element with the customer name to the Dialogflow CX agent as in the following code sample.

This key-value pair will be passed in as a session parameter(link takes you to an external page) which can be referenced under $session.params.cutomer_name at runtime in your Dialogflow CX agent.

Add custom parameters to a Dialogflow CX interaction

add-custom-parameters-to-a-dialogflow-cx-interaction page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
const response = new VoiceResponse();
_10
const connect = response.connect();
_10
const virtualagent = connect.virtualAgent({connectorName: 'uniqueName'});
_10
virtualagent.parameter({name: 'customer_name', value: 'Burton Guster'});
_10
_10
console.log(response.toString());

Output

_10
<Response>
_10
<Connect>
_10
<VirtualAgent connectorName="uniqueName">
_10
<Parameter name="customer_name" value="Burton Guster"/>
_10
</VirtualAgent>
_10
</Connect>
_10
</Response>


Dynamic connector configuration

dynamic-connector-configuration page anchor

TwiML noun allows you to override your underlying Dialogflow CX Connector's configuration or pass additional parameters that change the behavior of your virtual agent. The <Config> noun is nested inside of <VirtualAgent>. <Config> has two attributes: name and value, both of which must be used every time you use <Config>.

For each configuration option you want to override, you must include a new <Config> noun. The name attribute then corresponds to one of your Dialogflow CX Connector's configuration options, such as language, sentimentAnalysis, voiceName, and welcomeIntent.

Additionally, there are attributes that are not present in your Dialogflow CX Connector configuration such as voiceModel, speechModel, speechModelVariant, but you can still set these attributes using <Config> noun nested inside of <VirtualAgent>.

Below are the configuration settings supported within <Config> TwiML noun:

Configuration SettingDescriptionDefault
languageLanguage in which the caller will be interacting with the Dialogflow CX Virtual Agent, e.g. "es-es". When choosing a language, you must apply "STT" and "TTS" filters in the Dialogflow CX languages(link takes you to an external page)If not specified, this defaults to the setting in the Dialogflow CX connector configuration
sentimentAnalysisBoolean indicating whether to enable sentiment analysis or not. If you want Dialogflow to perform sentiment analysis on the intents, you must select language that supports "Sentiment" filter in addition to STT/TTS in the Dialogflow CX languages(link takes you to an external page)If not specified, this defaults to the setting in the Dialogflow CX connector configuration
voiceNameThe TTS voice(link takes you to an external page) to use for the Dialogflow CX Virtual Agent, e.g. "en-US-Standard-C"If not specified, this defaults to the setting in the Dialogflow CX connector configuration
welcomeIntentThe name of the event to trigger when connecting to the virtual agent. You would set this to the built-in system event(link takes you to an external page) "WELCOME" to trigger the Default Welcome Intent for your agent. However, Dialogflow CX allows you to configure custom events(link takes you to an external page) on your Dialogflow CX agent to programmatically denote where the flow should start.empty
voiceModelThe TTS custom voice model(link takes you to an external page) to use. If specified in addition to voiceName, this attribute will take precedence. This feature allows you to train a custom voice model using your own studio-quality audio recordings to create a unique voice.empty
speechModelThe STT model(link takes you to an external page) used in the processing of human speech. If not specified, the "default" speech-to-text transcription model is used.This option is not exposed in the Dialogflow CX Connector, but uses the "default" speechModel under the hood
speechModelVariantThe speech model variant used in speech-to-text. If an enhanced model variant is specified and an enhanced version of the specified model for the language does not exist, this omits an error. See the Enhanced Models(link takes you to an external page) guide for more information.This option is not exposed in the Dialogflow CX Connector and not explicitly set by Twilio. Dialogflow defaults to "USE_BEST_AVAILABLE"

For example, if you want to customize the TTS voice and language for the virtual agent interaction, you can supply the respective configuration settings inside the <Config/> attributes as follows:

Override Dialogflow CX Connector Configuration

override-dialogflow-cx-connector-configuration page anchor
Node.js
Python
C#
Java
PHP
Ruby

_10
const VoiceResponse = require('twilio').twiml.VoiceResponse;
_10
_10
const response = new VoiceResponse();
_10
const connect = response.connect();
_10
const virtualagent = connect.virtualAgent({connectorName: 'uniqueName'});
_10
virtualagent.config({name: 'language', value: 'en-us'});
_10
virtualagent.config({name: 'voiceName', value: 'en-US-Wavenet-C'});
_10
_10
console.log(response.toString());

Output

_10
<Response>
_10
<Connect>
_10
<VirtualAgent connectorName="uniqueName">
_10
<Config name="language" value="en-us"/>
_10
<Config name="voiceName" value="en-US-Wavenet-C"/>
_10
</VirtualAgent>
_10
</Connect>
_10
</Response>


Rate this page: