0
votes

I've created a webhook in C# using Http request-response messages. Now I am trying to create a Java one. This is how I return my response: (Based on the sample response template)

        return s.createResponseBuilder(HttpStatus.OK)
            .body(new JSONObject().put("fulfillmentText", resultText)
                    .put("fulfillmentMessages",
                            new JSONArray().put(new JSONObject().put("simpleResponses",
                                    new JSONObject().put("simpleResponses",
                                            new JSONArray().put(new JSONObject().put("displayText", "display text")
                                                    .put("textToSpeech", "display text")))))
                    .put("payload",
                            new JSONObject().put("google", new JSONObject().put("expectUserResponse", Boolean.TRUE)))
                    .toString())
            .build();

(result text is the actual result displayed in google actions) and this is not working ( although the exact json is returned by my c# webhook and it works fine ) Is there any template for good HttpResponse template that it will love in Java?

EDIT

I've tried using the classes provided by the Java SDK:

        // create simple response
    GoogleCloudDialogflowV2IntentMessageSimpleResponse sr = new GoogleCloudDialogflowV2IntentMessageSimpleResponse();
    sr.setDisplayText("display text");
    sr.setTextToSpeech("text to speech");
    // create list of simple response
    List<GoogleCloudDialogflowV2IntentMessageSimpleResponse> sr_list = new ArrayList<>();
    sr_list.add(sr);
    // set simple_responses
    GoogleCloudDialogflowV2IntentMessageSimpleResponses sr1 = new GoogleCloudDialogflowV2IntentMessageSimpleResponses();
    sr1.setSimpleResponses(sr_list);
    // set intent msg
    GoogleCloudDialogflowV2IntentMessage intentmsg = new GoogleCloudDialogflowV2IntentMessage();
    intentmsg.setSimpleResponses(sr1);
    // set list of intent msgs
    List<GoogleCloudDialogflowV2IntentMessage> intent_list = new ArrayList<>();
    intent_list.add(intentmsg);
    // set up the response
    GoogleCloudDialogflowV2WebhookResponse response = new GoogleCloudDialogflowV2WebhookResponse();
    response.setFulfillmentMessages(intent_list);
    response.setFulfillmentText(resultText);
    Map<String,Object> my_map = new HashMap<String, Object>();
    my_map.put("expectUserResponse", Boolean.TRUE);
    response.setPayload(my_map);

And sent that response.toString as the body for the HTTP response, but this didn't make the dialogflow \ actions on google understand me more :/

EDIT 2.0

when running on dialogflow I get the repsonse: enter image description here However, invoking it using Postman with the same URL and parameters: this is the response dialogflow gets, for some reason he doesn't seem to understand it (aligned it in VS code)

EDIT 3.0

the declaration:

public HttpResponseMessage run(
    @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> s,
    final ExecutionContext c) {
1
Can you update your question to clarify what you mean by "not working"? Are you getting an error? Where is the error? If it appears in the simulator, can you post the contents of the "request", "reply", and "debug" tabs? Do you have any logs on your Java server that indicate what might be going on? - Prisoner
edited. If this helps you in any way please share your thoughts :) - CmpScience Learner
It helps a great deal. Can you update it again to clarify what type s is in your sample code? - Prisoner
edited once again, if it helps I'm also using V2 of dialogflow for simulating (or actions on google) - CmpScience Learner

1 Answers

0
votes

I'm not familiar enough with your runtime environment (Java on Azure? Using Microsoft's Azure package com.microsoft.azure.functions?) to untangle what, exactly, is going on, but it looks like your HttpResponseMessage.Builder instance isn't encoding the body correctly, or isn't being clear about the encoding when the response is being set.

Specifically, I suspect the charset is ending up as UTF-16 or something similar. I would make sure you're forcing it to UTF-8.