如何使用 python 发送的 Dialogflow cx 中的事件开始对话

How to start a conversation using an event in Dialogflow cx sending by python

我正在尝试从 dialogflow cx python API 开始对话。我已经看到这个问题 使用 Node.js 解决了问题,但我无法在 python 中复制。 在我的代码中我有:

text_input = session.TextInput(text=msg)
query_input = session.QueryInput(text=text_input, language_code=language_code)
request = session.DetectIntentRequest(session=session_path, query_input=query_input)
response = session_client.detect_intent(request=request)

我想将 session.TextInput() 更改为 session.EventInput,例如 here,但它不适用于 dialogflow CX 和库 dialogflowcx_v3beta1

要将事件用作 query_input,您应该使用类型 EventInput() in your QueryInput()。请参阅下面的代码了解如何实现它。

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")
request = types.DetectIntentRequest(
        session=session_path,query_input=query_input
        )
response = client.detect_intent(request=request)

print(response.query_result.response_messages[0])

自定义事件:

使用 的输出: