python - 对话流 python SDK : How do I parse the DetectIntentResponse

标签 python dialogflow-es

我正在从 dialogflow V1 迁移到 V2。 使用 dialogflow python SDK,我收到一个 DetectIntentResponse 结构对象,其中应该包含我需要的信息。

经过一段时间尝试查找文档并尝试检查此对象后,我需要您的帮助。这个对象远远超出了我的能力范围......

对于文档,这就是我获取响应对象的方式:

import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)

如何解析响应?

例如我通过使用 response.query_result.parameters 获得了一些参数结构,但是我如何获得这个列表?

也许我可以将响应转换为 json(这会让事情变得很简单)?

我需要字典、列表、字符串……:)

最佳答案

您可以在 google.protobuf 项目中使用 MessageToJson。 (谷歌的 Protocol Buffer )

#import the function
from google.protobuf.json_format import MessageToJson

#...after getting the response = session_client.detect_intent(...)
json_response = MessageToJson(response)

您可以通过MessageToJson(response.query_result)等将DetectIntentResponse中包含的各种类型如QueryResult等转换为特定的响应.

我的回答是针对 DialogFlow 的,但是我从 this 得到了提示在 SO 处回答,它回答了与 python 相关的一个非常普遍的问题。

请注意,我使用 google.protobuf.json_format.MessageToJson 是因为 DialogFlow API V2 返回由 Google 定义的类对象。我不能保证这会与其他 Chatbot API 一起使用(可能我也需要探索它)。

关于python - 对话流 python SDK : How do I parse the DetectIntentResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51504525/

相关文章:

push-notification - 为什么我尝试在 Google 上的 Actions 上推送通知时收到 404 'App [my-project-id] was not found. The app preview may have expired.'?

python - 禁用标准。和 Python 沙箱实现中的文件 I/O

python - 是否可以在不定义函数的情况下实现这一目标?

python - 使用边界框列表从图像中裁剪多个边界框

node.js - Webhook 调用失败。错误 : 500 Internal Server Error | Dialogflow | Firebase

dialogflow-es - ChatGPT 与 Dialogflow 或 Rasa 等其他聊天机器人框架有什么区别?

python - 如何在 pandas 数据框中使用 groupby 查找描述性统计数据

python - 与 mpi4py 中生成的进程相互通信?

javascript - DialogFlow,完成操作后保持助手打开

java - 使用 Actions On Google 从 Webhook 请求获取连接池