DialogFlow CX Chatbot 如何在 DialogFlow 控制台中从 df-messenger 引用用户 ID

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

我在我的页面中使用了以下嵌入式 df-messenger javascript:

<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>

这是在一个 flask 应用程序中,因此当页面为 运行 时 current_user.name 正确解析为 'Mr Smith',但我不确定如何在我的应用程序中访问该值DialogFlow CX 控制台中的 DialogFlow 意图等。我明白你是指

queryParams.payload.userId

在您的 'detect intent' 中,但我不清楚这意味着什么。在 DialogFlow CX 控制台中构建我的聊天机器人时如何引用/显示/杠杆这个值?

您可以使用 Dialogflow API 根据 documentation 执行 detectIntent

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.

按照文档,您可以向端点 projects.locations.agents.sessions.detectIntent 发送 HTTP 请求,只需确保使用正确的请求正文。或者使用客户端库来执行此操作。

要实现文档通过 Python 所说的内容,您的代码将如下所示:

注意:我只是使用了我的随机意图来说明文档所说的内容。你也可以用其他语言实现它,比如 Nodejs 和 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 
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)

日志中显示的请求: