Dialogflow Messenger How To
The user-id is one of the attributes you can use in Dialogflow Messenger HTML customization.
In this tutorial, I will explain how it works.
Suppose you want to associate a logged in user for your web app with a user ID, so that you can (for example) get details about their account.
First, add the user-id into the HTML script for the Dialogflow Messenger embed code.
Here is an example:
Of course, normally, you would fetch this user-id from the server based on the logged-in user, and add it as one of the attributes of the df-messenger tag. For example, if you are using Python Flask, you might have something like user-id = {{ user_id }} in a Jinja template, and you will pass the server side user_id to your render_template method.
Now, when your Dialogflow Messenger bot calls the webhook, you will find that the JSON sent to the webhook will look like this (I just saved the entire request JSON object to a file on my webhook server, and copied and pasted the JSON and redacted some IDs):
{
"responseId": "**",
"queryResult": {
"queryText": "call webhook",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentText": "Webhook response from console",
"fulfillmentMessages": [
{
"text": {
"text": [
"Webhook response from console"
]
}
}
],
"outputContexts": [
{
"name": "projects/**/agent/sessions/**/contexts/__system_counters__",
"parameters": {
"no-input": 0.0,
"no-match": 0.0
}
}
],
"intent": {
"name": "projects/**/agent/intents/**",
"displayName": "call.webhook"
},
"intentDetectionConfidence": 1.0,
"languageCode": "en"
},
"originalDetectIntentRequest": {
"payload": {
"userId": "testuser"
}
},
"session": "projects/**/agent/sessions/**"
}
As you can see, the JSON sent to the webhook contains an object called originalDetectIntentRequest, which has a payload object which contains the userId (line 35).
You can then obtain the user’s ID by accessing the relevant field in the JSON.
An interesting side note: the user ID does not show up in the History inside Dialogflow itself. It is obviously sent to the webhook, but there is no way to know by just inspecting the interaction log inside the Dialogflow console.