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:
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) {
sis in your sample code? - Prisoner