无法正确解析对话流程满足。

发布于 2025-02-10 17:21:14 字数 3706 浏览 0 评论 0原文

当定义默认响应和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个元素)

“控制台输出

我的问题:

  • 为什么在每种情况下长度/计数都不同?有没有办法进行内省的不同响应对象来弄清每个响应对象?

  • 根据是否启用/禁用了滑块按钮,响应中元素的顺序有所不同。为什么? 如果启用(count = 3):元素0具有自定义有效负载,则元素1具有Facebook快速回复有效负载,元素2具有自定义有效负载(重复)。 如果禁用(count = 2):元素0具有Facebook快速回复有效负载,元素1具有自定义有效载荷。

  • 响应列表通常包含重复。方案3是一个例子。另一个例子是,对于方案1,我询问 title (而不是有效载荷)。现在打印了所有3个元素,其中第一个和第三个元素重复了。

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
Dialogflow console Response section

Facebook integration response is set of quick replies.

Facebook integration tab

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).

Console output - scenario 1

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).

console output - scenario 2

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)

console output - scenario 3

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.
    Console output - scenario 1a

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除→记忆 2025-02-17 17:21:14

需要明确的是,长度您是计数Response.queryResult.fulfillmentMessages中的对象。

以下是原始打印response.queryresult.fulfillmentMessages用于参考子弹答案:

Got response length:3
[payload {
  fields {
    key: "replies"
    value {
      list_value {
        values {
          string_value: "option 1"
        }
        values {
          string_value: "option 2"
        }
      }
    }
  }
  fields {
    key: "text"
    value {
      string_value: "this is some text!"
    }
  }
}
platform: FACEBOOK
, quick_replies {
  title: "What can I help you with today?"
  quick_replies: "Facebook option 1"
  quick_replies: "Facebook option 2"
}
platform: FACEBOOK
, payload {
  fields {
    key: "replies"
    value {
      list_value {
        values {
          string_value: "option 1"
        }
        values {
          string_value: "option 2"
        }
      }
    }
  }
  fields {
    key: "text"
    value {
      string_value: "this is some text!"
    }
  }
}
]

将根据子弹的顺序回答您的 3个问题

  • 您只需打印 raw 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 inside response.queryResult.fulfillmentMessages.

Below is the raw printed response.queryResult.fulfillmentMessages for reference of the bulleted answers:

Got response length:3
[payload {
  fields {
    key: "replies"
    value {
      list_value {
        values {
          string_value: "option 1"
        }
        values {
          string_value: "option 2"
        }
      }
    }
  }
  fields {
    key: "text"
    value {
      string_value: "this is some text!"
    }
  }
}
platform: FACEBOOK
, quick_replies {
  title: "What can I help you with today?"
  quick_replies: "Facebook option 1"
  quick_replies: "Facebook option 2"
}
platform: FACEBOOK
, payload {
  fields {
    key: "replies"
    value {
      list_value {
        values {
          string_value: "option 1"
        }
        values {
          string_value: "option 2"
        }
      }
    }
  }
  fields {
    key: "text"
    value {
      string_value: "this is some text!"
    }
  }
}
]

Will answer your 3 questions based on the order of the bullets.

  • You can just print the raw 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 Response

index[1] will be the quick_replies inside facebook platform

index[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 second key-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文