无法正确解析对话流程满足。
当定义默认响应和Facebook集成响应时,我在flutter应用程序中有一个问题解析对话流响应。
Facebook集成响应是快速回复的。
如下所示,可以检查上述两个响应的JSON表示。
"messages": [
{
"type": "quick_reply",
"platform": "facebook",
"condition": "",
"title": "What can I help you with today?",
"replies": [
"Facebook option 1",
"Facebook option 2"
]
},
{
"type": "custom_payload",
"condition": "",
"payload": {
"replies": [
"option 1",
"option 2"
],
"text": "this is some text!"
}
}
],
在这个简单的幻影应用程序中,我连接到DialogFlow并发送映射到上述意图的查询。由于对检测()的响应是类型列表,因此我打印列表中每个消息的列表的长度以及键/值。
var request = Dialogflow.getRequestTextInput(query, "en-US");
var response = await df.detectIntent(request, sessionPath);
List<GoogleCloudDialogflowV2IntentMessage> payloads = response.queryResult.fulfillmentMessages;
debugPrint("Got response length: ${payloads.length}");
debugPrint(response.toString());
GoogleCloudDialogflowV2IntentMessage firstEntry = payloads[0];
Map<String, dynamic> payload0 = firstEntry.payload;
payload0.forEach((key, value) => debugPrint("Key: $key Value: $value"));
debugPrint("Printing second entry");
GoogleCloudDialogflowV2IntentMessage secondEntry = payloads[1];
Map<String, dynamic> payload1 = secondEntry.payload;
payload1.forEach((key, value) => debugPrint("Key: $key Value: $value"));
debugPrint("Printing third entry");
GoogleCloudDialogflowV2IntentMessage thirdEntry = payloads[2];
Map<String, dynamic> payload2 = thirdEntry.payload;
payload2.forEach((key, value) => debugPrint("Key: $key Value: $value"));
以下是每个方案的场景和观察值:
方案1:使用默认选项卡中的响应作为第一个响应的滑块按钮。 观察:
- 返回的响应计数是
- 列表=&gt的第一个元素的3个打印;自定义有效载荷的密钥/值是打印
- 列表=&gt的第二个元素;程序引发了一个未经治疗的异常(在null上称为“ foreach”方法)。这是有道理的,因为Facebook响应中没有有效载荷对象(请参阅JSON)。
方案2:使用默认选项卡中的响应作为第一个响应禁用的滑块按钮。 观察:
- 返回的响应计数是2。为什么?
- 然后,该程序抛出了一个未手持的异常(在null上称为“ foreach”方法)。
方案3:删除Facebook响应,因此意图的唯一响应是默认选项卡中的自定义有效负载。 观察:
- 响应计数为2。
- 打印响应列表的前两个元素返回重复项,即相同的键/值对。
- 尝试打印第三元素引发异常(rangeRor),这是有意义的,因为数组只有2个元素)
我的问题:
I'm having an issue parsing DialogFlow responses in a Flutter app when BOTH default response and Facebook integration responses are defined.
Setup: DEFAULT response contains a simple custom payload
Facebook integration response is set of quick replies.
The JSON representation of both the above responses can be examined when the Intent is downloaded, as below.
"messages": [
{
"type": "quick_reply",
"platform": "facebook",
"condition": "",
"title": "What can I help you with today?",
"replies": [
"Facebook option 1",
"Facebook option 2"
]
},
{
"type": "custom_payload",
"condition": "",
"payload": {
"replies": [
"option 1",
"option 2"
],
"text": "this is some text!"
}
}
],
In this simple Flutter app, I connect to DialogFlow and send a query that is mapped to the above intent. Since the response to detectIntent() is of type List, I print the length of the List and key/value of each Message in the List.
var request = Dialogflow.getRequestTextInput(query, "en-US");
var response = await df.detectIntent(request, sessionPath);
List<GoogleCloudDialogflowV2IntentMessage> payloads = response.queryResult.fulfillmentMessages;
debugPrint("Got response length: ${payloads.length}");
debugPrint(response.toString());
GoogleCloudDialogflowV2IntentMessage firstEntry = payloads[0];
Map<String, dynamic> payload0 = firstEntry.payload;
payload0.forEach((key, value) => debugPrint("Key: $key Value: $value"));
debugPrint("Printing second entry");
GoogleCloudDialogflowV2IntentMessage secondEntry = payloads[1];
Map<String, dynamic> payload1 = secondEntry.payload;
payload1.forEach((key, value) => debugPrint("Key: $key Value: $value"));
debugPrint("Printing third entry");
GoogleCloudDialogflowV2IntentMessage thirdEntry = payloads[2];
Map<String, dynamic> payload2 = thirdEntry.payload;
payload2.forEach((key, value) => debugPrint("Key: $key Value: $value"));
Here are the scenarios and my observations for each scenario:
Scenario 1: Slider button for using responses from Default tab as first responses is ENABLED.
Observations:
- Count of responses returned is 3
- Printing first element of list => key/value of custom payload is printed
- Printing second element of list => program throws an Unhandled exception(The method 'forEach' was called on null). This makes sense because there is no payload object in the Facebook response (see JSON).
Scenario 2: Slider button for using responses from Default tab as first responses is DISABLED.
Observations:
- Count of responses returned is 2. Why?
- The program then throws an Unhandled exception(The method 'forEach' was called on null).
Scenario 3: Delete the Facebook response, so the only response for the intent is the custom payload in Default tab.
Observations:
- Response count is 2.
- Printing first two elements of the response list returns duplicates, i.e. identical key/value pairs.
- Attempting to print the third element throws exception(RangeError), which makes sense since array only has 2 elements)
My questions:
Why are the lengths/counts different in each scenario? Is there a way to introspect the different response objects to figure out what each is composed of?
It appears the order of elements in the response differs, based on whether the slider button is Enabled/Disabled. Why?
If enabled (count=3): Element 0 has the custom payload, Element 1 has the Facebook quick reply payload, Element 2 has the custom payload (duplicate).
If disabled (count=2): Element 0 has the Facebook quick reply payload, Element 1 has the custom payload.The response list often contains duplicates. Scenario 3 above is one example. Another example is, say for Scenario 1, I query the title (instead of payload). Now all 3 elements are printed, with the first and third elements duplicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
需要明确的是,
长度
您是计数是Response.queryResult.fulfillmentMessages
中的对象。以下是原始打印
response.queryresult.fulfillmentMessages
用于参考子弹答案:将根据子弹的顺序回答您的 3个问题。
wendesp.queryresult.fulfillmentMessages
即可检查对象总数的差异。在上面的示例中, 3对象是ff:index [0]
将是有效载荷 默认响应的对象索引[1]
将是 quick_replies Facebook平台index [2]
将是有效载荷 Facebook平台内的对象。当您禁用“使用默认选项卡中的响应作为第一个响应。” slider按钮,这意味着从上面的示例中,有效载荷 默认响应将被省略,这就是为什么长度计数仅为2的原因。也就是说, Quick_replies Facebook平台和有效载荷对象 Facebook平台。。
这些元素不是重复的。基于您的示例屏幕截图,第一个
键值
是默认响应的有效载荷 时>是 Facebook平台的有效载荷。正如您在我上述示例原始输出上看到的那样,它具有指标“平台:Facebook” ,这意味着自定义有效载荷响应是Facebook平台的。
Just to be clear, the
length
that you are counting are the objects insideresponse.queryResult.fulfillmentMessages
.Below is the raw printed
response.queryResult.fulfillmentMessages
for reference of the bulleted answers:Will answer your 3 questions based on the order of the bullets.
response.queryResult.fulfillmentMessages
to examine the differences in the total number of objects. In the above example the 3 objects are the ff:index[0]
will be the payload object of the Default Responseindex[1]
will be the quick_replies inside facebook platformindex[2]
will be the payload object inside facebook platform.When you disable the "Use responses from the DEFAULT tab as the first responses." slider button, this means that, from the above example, payload object of the Default Response will be omitted that's why the count of length will be 2 only. Namely, the quick_replies inside facebook platform and the payload object inside facebook platform.
The elements are not duplicate. Based your sample screenshot, the first
key-value
are the payload of the Default Response while the secondkey-value
are the payload of the Facebook Platform.As you can see on my above sample raw output, it has an indicator "platform: FACEBOOK" which means the custom payload response is for facebook platform.