Search code examples
google-cloud-platformdialogflow-cx

DialogFlow CX Chatbot how to refer to user-id from df-messenger within the DialogFlow Console


I am using the following embedded df-messenger javascript in my page:

<df-messenger
    df-cx="true"
    location="australia-southeast1"
    chat-title="Cassie"
    agent-id="be11f0b8-f343-408e-98da-749ec04659bd"
    user-id="{{ current_user.name }}"
    language-code="en"
></df-messenger>

This is within a flask application, so the current_user.name correctly resolves to 'Mr Smith' when the page is running, but I'm not sure how to access that value within my DialogFlow intents etc. within the DialogFlow CX console. I understand that you are meant to refer to

queryParams.payload.userId

within your 'detect intent' but I'm not clear on what that means. How can I refer to / display / leverege this value when building my chatbot within the DialogFlow CX console?


Solution

  • You can use the Dialogflow API to perform detectIntent as per documentation.

    user-id - Optional

    Can be used to track a user across sessions. You can pass the value to Dialogflow through the queryParams.payload.userId field in a detect intent request, and Dialogflow provides this value to you through the WebhookRequest.payload.userId field.

    Following the documentation, you can send a HTTP request to endpoint projects.locations.agents.sessions.detectIntent just make sure to use the correct request body. Or use client libraries to do this.

    To implement what the documentation says via Python, your code will look like this:

    NOTE: I just used a random intent of mine to illustrate what the documentation says. Also you can implement this in other languages like Nodejs, and Java.

    from google.cloud import dialogflowcx_v3beta1 as dialogflow
    from google.cloud.dialogflowcx_v3beta1 import types
    
    import uuid
    
    project_id = "your-project"
    location = "us-central1" # my project is located here hence us-central1
    session_id = uuid.uuid4()
    agent_id = "999999-aaaa-aaaa" # to get your agent_id see https://stackoverflow.com/questions/65389964/where-can-i-find-the-dialogflow-cx-agent-id
    session_path = f"projects/{project_id}/locations/{location}/agents/{agent_id}/sessions/{session_id}"
    
    api_endpoint = f"{location}-dialogflow.googleapis.com"
    client_options = {"api_endpoint": api_endpoint}
    client = dialogflow.services.sessions.SessionsClient(client_options=client_options)
    
    event = "custom_event"
    event_input = types.EventInput(event=event)
    query_input = types.QueryInput(event=event_input,language_code="en-US")
    query_params = types.QueryParameters(payload={"userId": "Mr Smith"}) # this is should be similar with queryParams.payload.userId
    request = types.DetectIntentRequest(
            session=session_path,
            query_input=query_input,
            query_params=query_params,
            )
    response = client.detect_intent(request=request)
    
    print(response)
    

    Request as seen in the logs:

    enter image description here