actions-on-google / actions-on-google-java

Java/Kotlin library for Actions on Google
Apache License 2.0
286 stars 39 forks source link

How to make a request to a REST service from within a Dialogflow intent #22

Closed fitsie007 closed 5 years ago

fitsie007 commented 5 years ago

I am trying to make a request to a REST API from within my intent, but it always throws an error:

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

I am using spring-framework RestTemplate to send the request as follows.

@ForIntent("process-greeting")
  public ActionResponse greetingProcessor(ActionRequest request) {
...
String givenName = (String) request.getParameter("given-name");
Greeting greeting = restTemplate.getForObject("https://fitzroi-rest-api-0525.appspot.com/rest/{name}", Greeting.class, givenName);
responseBuilder.add(greeting.getContent()); //this is a simple greeting like "Hello, Fitz"
}

Is this the right way to make a call to a REST service from within an Intent? Also, how do I log custom error messages for debugging?

taycaldwell commented 5 years ago

Hi @fitsie007,

Our dialog-quotes-java sample provides a great example on how to make requests to external APIs and URLs. You can use that as an example.

Regarding logging - You can log using SLF4J, providing any underlying implementation you wish, such as log4j. The sample mentioned above also provides an example of this that you can use as a reference.