首页 > 解决方案 > 'QueryResult' 对象不可迭代 - 无法将 Dialogflow API 中的值返回到我的聊天机器人

问题描述

我无法返回response.query_result

我想回来result["action"]result["query_text"]["fulfillment_text"]

我试过response.query_result.fulfillment_text了,但它的显示

“太多值无法解压”

response_id: "39a7bc01-2432-4de2-b134-6d7c5bbee6ea-f308a5c4"
query_result {
  query_text: "you annoy me"
  action: "smalltalk.agent.annoying"
  parameters {
  }
  all_required_params_present: true
  fulfillment_text: "I\'ll try not to annoy you."
  fulfillment_messages {
    text {
      text: "I\'ll try not to annoy you."
    }
  }
  intent {
    name: "projects/my-project-1560026125515/agent/intents/c50c7c63-aa76-4781-ba7d-d9ba4c93df64"
    display_name: "smalltalk.agent.annoying"
  }
  intent_detection_confidence: 1.0
  language_code: "en"
}

标签: pythondialogflow-es

解决方案


您必须将响应转换为 JSON 格式。为此用途:

import google.protobuf  as pf

response =pf.json_format.MessageToJson(response)

现在您可以解析这个 JSON 响应。希望能帮助到你。


推荐阅读