This guide provides various samples for implementing webhooks as well as webhook troubleshooting recommendations.
Set a session parameter
The following samples show how to set a session parameter.
Go
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
See the Webhooks quick start.Java
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Return a fulfillment response
The following samples show how to return a fulfillment response.
Go
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
See the Webhooks quick start.Java
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Set form parameters as required
The following samples show how to flag a parameter as required.
Java
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Validate a form parameter
The following samples show how to validate a form parameter.
Java
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Log session ID
The following sample shows how to log the session ID
from a webhook request.
Python
To authenticate to Dialogflow, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Troubleshooting
When troubleshooting a webhook,
it is helpful to log the session ID from your webhook.
The sessionInfo.session
field of the
WebhookRequest
contains the session ID.
The session ID should be unique for each conversation,
and it can help you compare agent logs against webhook logs
for requests using the same session ID.
The Log session ID section above
shows how to log the session ID from a webhook.
In addition, if you are hosting your webhook on
Cloud Functions
or a similar Google Cloud serverless option,
you can use the trace
field from
log entries
as a log filter.
A single execution of a function results in multiple log entries
with the same trace value.
The example below uses both the session ID and the trace value to associate a particular Conversational Agents (Dialogflow CX) agent error log with the corresponding Cloud Functions webhook log entries. The example uses Cloud Logging Filters for an agent that has enabled Cloud Logging.
1. Filter Conversational Agents (Dialogflow CX) logs for a particular agent's error logs
Use the following Cloud logging filter to filter your Conversational Agents (Dialogflow CX) logs for a particular agent's error logs:
labels.location_id="global"
labels.agent_id="AGENT_ID"
severity=ERROR
A webhook log error entry looks like the following:
{
"insertId": "-zi368ye1ie3r",
"jsonPayload": {
"message": "Webhook error: State: URL_UNREACHABLE, Reason: UNREACHABLE_5xx, HTTP status code: 500",
"code": 14
},
"resource": {
"type": "global",
"labels": {
"project_id": "PROJECT_ID"
}
},
"timestamp": "2022-10-05T19:44:53.098938Z",
"severity": "ERROR",
"labels": {
"session_id": "ec8781-f28-7e6-791-d7e992e8f",
"location_id": "global",
"agent_id": "807346f0-f501-42b5-9642-af59d5e98165"
},
"logName": "projects/PROJECT_ID/logs/dialogflow-runtime.googleapis.com%2Frequests",
"receiveTimestamp": "2022-10-05T19:44:53.222552658Z"
}
Note the labels.session_id
field,
which contains the session ID.
You will use the session ID in the next step.
2. Filter Cloud Function logs for the session ID
Use the following Cloud logging filter to filter your Cloud Function logs for the session ID:
resource.type = "cloud_function"
resource.labels.function_name = "CLOUD_FUNCTION_NAME"
resource.labels.region = "CLOUD_FUNCTION_REGION"
textPayload="Debug Node: session ID = SESSION_ID"
The resulting logs correspond to webhook logs made during the provided session. For example:
{
"textPayload": "Debug Node: session ID = ec8781-f28-7e6-791-d7e992e8f",
"insertId": "632bdb2b000b7c6c77d0cc62",
"resource": {
"type": "cloud_function",
"labels": {
"project_id": "PROJECT_ID",
"function_name": "webhook",
"region": "us-central1"
}
},
"timestamp": "2022-10-05T19:44:53.098938Z",
"severity": "INFO",
"labels": {
"execution_id": "ozt5bnz50eeo",
"instance_id": "00c61b117c1f116421aa5503bc80f9aa636b9ef117bb2722f3d54414085e62be6e55af0aa0250a63534262b31a3d3a14af8c940203f1915db8b25b"
},
"logName": "projects/PROJECT_ID/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/PROJECT_ID/traces/e41eefc1fac48665b442bfa400cc2f5e",
"receiveTimestamp": "2022-10-05T19:44:53.222552658Z"
}
Note the trace
field which is used in the next step.
3. Filter Cloud Function logs for a particular trace
Use the following Cloud Logging filter to filter Cloud Function logs for a particular trace:
resource.type = "cloud_function"
resource.labels.function_name = "CLOUD_FUNCTION_NAME"
resource.labels.region = "CLOUD_FUNCTION_REGION"
trace="projects/PROJECT_ID/traces/TRACE_ID"
where TRACE_ID
is the last segment of trace. For example the TRACE_ID
for projects/PROJECT_ID/traces/e41eefc1fac48665b442bfa400cc2f5e
is
e41eefc1fac48665b442bfa400cc2f5e
.
The resulting logs correspond to all webhook logs generated during the execution of the webhook request associated with the session ID from step 1 and with the trace from step 2.