首页 > 解决方案 > 如何向对话流发送响应

问题描述

我正在尝试使用 dialogflow v2 做一个 facebook 聊天机器人信使。我在 Dialogflow 中配置了 webhook url。我正在从 dialogflow 获取请求。但问题是我不清楚如何将响应发送到对话流。

下面是我的代码

post("/", (request, response) -> {
            String body = request.body();
            System.err.println(body);
            String reply = "Hi";
            JSONObject responseJSON = new JSONObject();
            JSONObject payload = new JSONObject();
            JSONObject google = new JSONObject();
            google.put("expectUserResponse", true);
            JSONObject richResponse = new JSONObject();
            JSONArray itemsArray = new JSONArray();
            JSONObject simpleResponseRoot = new JSONObject();
            JSONObject simpleResponse = new JSONObject();
            simpleResponse.put("textToSpeech", reply);
            simpleResponseRoot.put("simpleResponse", simpleResponse);
            itemsArray.put(simpleResponseRoot);
            richResponse.put("items", itemsArray);
            google.put("richResponse", richResponse);
            payload.put("google", google);
            responseJSON.put("payload", payload);
            System.err.println(responseJSON);
            return responseJSON;
        });

下面是我的json回复

{"payload":{"google":{"richResponse":{"items":[{"simpleResponse":{"textToSpeech":"Hi"}}]},"expectUserResponse":true}}}

但我没有收到对我的 Facebook 页面的回复。

任何人都可以共享代码或 json 格式来发送对话流。

任何帮助将不胜感激!!!

标签: javaapache-sparkdialogflow-es

解决方案


问题在于您使用的是 Actions on Google 的回复格式,其中包含的信息比其他集成使用的更多。您可能希望 JSON 更像

{
  "fulfillmentText": "Hi"
}

推荐阅读