How to process a request to an external REST service and return the response to Google Assistant?

Fitz

I am using Java to create an app for Google Assistant that will call an external REST API and return certain responses based on trigger phrases. I currently can use the Default Welcome Intent to return simple text responses through the Actions on Google simulator. However, when I try to call an external REST API and send back a response, the simulator returns a message that says:

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

I am using the org.springframework.web.client.RestTemplate from Spring-framework to process the result from the REST Service (https://fitzroi-rest-api-0525.appspot.com/rest/Fitz) with the following call: greeting = restTemplate.getForObject("https://fitzroi-rest-api-0525.appspot.com/rest/{name}", Greeting.class, givenName); (This works well in a regular Spring project, but not within the Actions on Google Intent)

An example training phrase for my test app is "Tony is sending greetings." From here I am extracting "Tony" as a @sys.given-name entity in Dialogflow. This name is then passed to the REST serivice for processing. The REST service is an MVC app that is running in a separate Google Cloud project from the Google Assistant App.

Please let me know if this is a good approach to consume a REST service using Dialogflow fullfillment webhook.

Below is a sample code from my webhook that is trying to consume the REST service.

@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();
  }
Fitz

It seems that since actions-on-google requires thread-safe function calls, the RestTemplate from spring-framework does not work in an app-engine app. I was able to find a walk-around by using a sample code provided by the actions-on-google team on GitHub. This code requires that you parse the results from the URL from within the ActionsApp instead of using a library. For example:

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

I also had a serious issue (UnKonwnHostException) parsing the results from a remote URL. This documentation was helpful.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to process a request to an external REST service and return the response to Google Assistant?

From Dev

Abstraction of external service / API request and response

From Dev

Abstraction of external service / API request and response

From Dev

How to convert (JSON toXML) request and Response(XML to JSON) to call an external Soap Service

From Dev

How to convert (JSON toXML) request and Response(XML to JSON) to call an external Soap Service

From Dev

How do I return custom HTTP response (400 or 401) from axis 2 web service (REST)?

From Dev

While creating a REST API in node, How can I stream http response from a request made to an external website to the original api call?

From Dev

How to format json response in WCF rest service

From Dev

Is it bad practice for a REST endpoint to return different response fields based on the request?

From Dev

How to Return a 400 response with Spring Rest and PagingAndSortingRepository

From Dev

How to wait a request response and return the value?

From Dev

How to go to the url return by a restful service as response

From Dev

How to set cookie in request header for REST service

From Dev

Return JSON on unauthorized REST service request using Spring Security 'hasPermission()'

From Dev

How to combine request from external API and send it as a response in Flask

From Dev

Handling REST request and REST response

From Dev

Return request response in angularJs

From Dev

Response from dialogflow does not come to Google Assistant

From Dev

Sharepoint How to do get request to external web service

From Dev

Make request to external server from vaadin client and return JSON response to my sever

From Dev

REST service return not working

From Dev

How to capture a response token sent by a REST API after a request?

From Dev

Django Rest Framework : How to add a custom field to the response of the GET request?

From Dev

Django Rest Framework : How to add a custom field to the response of the GET request?

From Dev

How to change the response content type for a Dropwizard REST service?

From Dev

How to get JSON response from a teiid REST service

From Dev

How can we check if response for the request came from Service Worker

From Dev

How can we check if response for the request came from Service Worker

From Dev

How to create a WCF Service with no structure (XML request and response)

Related Related

  1. 1

    How to process a request to an external REST service and return the response to Google Assistant?

  2. 2

    Abstraction of external service / API request and response

  3. 3

    Abstraction of external service / API request and response

  4. 4

    How to convert (JSON toXML) request and Response(XML to JSON) to call an external Soap Service

  5. 5

    How to convert (JSON toXML) request and Response(XML to JSON) to call an external Soap Service

  6. 6

    How do I return custom HTTP response (400 or 401) from axis 2 web service (REST)?

  7. 7

    While creating a REST API in node, How can I stream http response from a request made to an external website to the original api call?

  8. 8

    How to format json response in WCF rest service

  9. 9

    Is it bad practice for a REST endpoint to return different response fields based on the request?

  10. 10

    How to Return a 400 response with Spring Rest and PagingAndSortingRepository

  11. 11

    How to wait a request response and return the value?

  12. 12

    How to go to the url return by a restful service as response

  13. 13

    How to set cookie in request header for REST service

  14. 14

    Return JSON on unauthorized REST service request using Spring Security 'hasPermission()'

  15. 15

    How to combine request from external API and send it as a response in Flask

  16. 16

    Handling REST request and REST response

  17. 17

    Return request response in angularJs

  18. 18

    Response from dialogflow does not come to Google Assistant

  19. 19

    Sharepoint How to do get request to external web service

  20. 20

    Make request to external server from vaadin client and return JSON response to my sever

  21. 21

    REST service return not working

  22. 22

    How to capture a response token sent by a REST API after a request?

  23. 23

    Django Rest Framework : How to add a custom field to the response of the GET request?

  24. 24

    Django Rest Framework : How to add a custom field to the response of the GET request?

  25. 25

    How to change the response content type for a Dropwizard REST service?

  26. 26

    How to get JSON response from a teiid REST service

  27. 27

    How can we check if response for the request came from Service Worker

  28. 28

    How can we check if response for the request came from Service Worker

  29. 29

    How to create a WCF Service with no structure (XML request and response)

HotTag

Archive