我正在从 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/