java - 如何处理对外部 REST 服务的请求并将响应返回给 Google Assistant?

标签 java rest spring-mvc actions-on-google google-assist-api

我正在使用 Java 为 Google 智能助理创建一个应用程序,该应用程序将调用外部 REST API 并根据触发短语返回某些响应。 我目前可以使用默认欢迎意图通过 Actions on Google 模拟器返回简单的文本响应。但是,当我尝试调用外部 REST API 并发回响应时,模拟器返回一条消息:

"MalformedResponse: Failed to parse Dialogflow response into AppResponse because of empty speech response."

我正在使用来自 Spring 框架的 org.springframework.web.client.RestTemplate 通过以下调用处理来自 REST 服务 ( https://fitzroi-rest-api-0525.appspot.com/rest/Fitz ) 的结果: greeting = restTemplate.getForObject("https://fitzroi-rest-api-0525.appspot.com/rest/{name}", Greeting.class, givenName);(这在常规 Spring 项目,但不在 Actions on Google Intent 中)

我的测试应用的训练短语示例是“Tony is sending greetings”。从这里我将“Tony”提取为 Dialogflow 中的 @sys.given-name 实体。然后将此名称传递给 REST 服务进行处理。 REST 服务是一个 MVC 应用程序,它在与 Google Assistant 应用程序不同的 Google Cloud 项目中运行。

请告诉我这是否是使用 Dialogflow fullfillment webhook 使用 REST 服务的好方法。

下面是我的 webhook 中尝试使用 REST 服务的示例代码。

@ForIntent("process-greeting")
  public ActionResponse greetingProcessor(ActionRequest request) {
    LOGGER.info("Trying to process greeting intent");
    ResponseBuilder responseBuilder = getResponseBuilder(request);

    String givenName = (String) request.getParameter("given-name");


    if (givenName != null && !givenName.isEmpty()) {
      RestTemplate restTemplate = new RestTemplate();
      Greeting greeting = null;
      greeting = restTemplate.getForObject("https://fitzroi-rest-api-0525.appspot.com/rest/{name}", Greeting.class, givenName);

//    LOGGER.info("Attempting to send back " + greeting.getContent() + " to Google Assistant");
      if (greeting == null)
        responseBuilder.add("The rest service did not return a  response.");
      else
        responseBuilder.add(greeting.getContent());
    }

    LOGGER.info("Welcome intent end.");
    return responseBuilder.build();
  }

最佳答案

似乎由于 actions-on-google 需要线程安全的函数调用,spring-framework 中的 RestTemplate 无法在一个 app-engine 应用程序。通过使用 actions-on-google 团队在 GitHub 上提供的示例代码,我能够找到解决方法。 .此代码要求您从 ActionsApp 中解析 URL 的结果,而不是使用库。例如:

URL restURL = new URL("http://yourdomain.com/your/rest/call");
URLConnection conn = restURL.openConnection();
InputStreamReader reader = new InputStreamReader(
               (InputStream) conn.getContent());
JsonParser parser = new JsonParser();
JsonElement root = parser.parse(reader);
MyObject = myParseFunction(root);

我在解析来自远程 URL 的结果时也遇到了严重的问题 (UnKonwnHostException)。这documentation很有帮助。

关于java - 如何处理对外部 REST 服务的请求并将响应返回给 Google Assistant?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56378344/

相关文章:

java - Java Spring 应用程序可以利用 swagger 吗?

java - Eclipse 中的 Android 相机/网络摄像头模拟器不断崩溃

java - 调整 JVM 堆大小后解释 jstat 输出

c# - 如何使用ServiceStack实现删除服务调用

Java拉力赛休息API : How to add new user to the Project

java - 找不到约束 'javax.validation.constraints.Size' 验证类型 'java.lang.Double' 的 validator

java - 如何将 jbutton 添加到 jpanel

java - out.absolute.dir 在 Linux 中是什么意思?

java - spring mvc+ajax 获取点击数据

java - @JsonDeserialize 注释不起作用