Home / Website Bots / How to use Dialogflow Messenger user-id
Website Bots

How to use Dialogflow Messenger user-id

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/**" }
Code language: JSON / JSON with Comments (json)

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.

<— End of article —>

This website contains affiliate links. See the disclosure page for more details. 
Check out the Learn Dialogflow ES and Learn Dialogflow CX courses if you would like to learn Dialogflow in depth.
"The magic key I needed as a non-programmer"

The custom payload generator was the magic key I needed (as a non-programmer) to build a good demo with rich responses in DialogFlow Messenger. I've only used it for 30 minutes and am thrilled. I've spent hours trying to figure out some of the intricacies of DialogFlow on my own. Over and over, I kept coming back to Aravind's tutorials available on-line. I trust the other functionalities I learn to use in the app will save me additional time and heartburn.

- Kathleen R
Cofounder, gathrHealth

Similar Posts