python - Python 中的 Dialogflow API 访问后续意图

标签 python api dialogflow-es

我正在按照以下教程来了解 Dialogflow Python API。

这是我的改编的样子:

import dialogflow_v2 as dialogflow
import json
from google.api_core.exceptions import InvalidArgument
from google.oauth2 import service_account

dialogflow_key = json.load(open(r'path_to_json_file.json'))
credentials = (service_account.Credentials.from_service_account_info(dialogflow_key))
session_client = dialogflow.SessionsClient(credentials=credentials)


DIALOGFLOW_LANGUAGE_CODE = 'en-US'
DIALOGFLOW_PROJECT_ID = 'some_project_id'
SESSION_ID = 'current-user-id'
session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)

text_to_be_analyzed =  "mobile data"
text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)
query_input = dialogflow.types.QueryInput(text=text_input)
try:
    response = session_client.detect_intent(session=session, query_input=query_input)
except InvalidArgument:
    raise

print("Query text:", response.query_result.query_text)
print("Detected intent:", response.query_result.intent.display_name)
print("Detected intent confidence:", response.query_result.intent_detection_confidence)
print("Fulfillment text:", response.query_result.fulfillment_text)

以下是程序输出的内容:

Query text: mobile data
Detected intent: support.problem
Detected intent confidence: 0.41999998688697815
Fulfillment text: Make sure mobile data is enabled and Wi-Fi is turned off.

现在,我的意图 support.problem 已跟进意图 support.problem-yes,客户回复 完成 并收到另一个响应让我们尝试另一个步骤

如何传递文本/查询来跟进意图以及如何在 Python 中获得响应?

最佳答案

response.query_result对象还应该包含 output_context字段,它应该是 Context 对象的数组。该数组应传入 query_parameters.context 您传递到detect_intent() .

您应该能够使用传递给 context 的查询参数字段(包括 session_client.detect_intent 的一个)创建一个字典。 .

关于python - Python 中的 Dialogflow API 访问后续意图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55694596/

相关文章:

Java : How to extract parameters from Google Dialogflow V2 response

python - to_sql pandas 方法改变了 sqlite 表的方案

python - 测试可转换字符的 wchar_t*

javascript - PhoneGap Ajax 请求在使用 AT&T 数据时返回 502(错误的网关),但不使用 WiFi

java - 为移动操作系统开发图像到文本转换应用程序

node.js - Actions-on-Google : TypeError: assistant. getContextArgument 不是函数

java - 将参数添加到 DialogFlow Java API 的 QueryInput 上的 EventInput

python - 如何从 python-social-auth 获取后端实例

python - 使用通配符的复杂和自定义 SQL 连接条件

Facebook取消授权回调未被调用