Skip to content

Commit

Permalink
samples: add webhook sample (#730)
Browse files Browse the repository at this point in the history
* samples:add webhook sample

* Revised Code

* Changed class name to match cloud function

* Revised code

* Changed Test

* Fixed test

* Test Fix

* changed json " to '

* added bracket

* lint fix

* 🦉 Updates from OwlBot

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Shabirmean committed Nov 15, 2022
1 parent c5a7160 commit 434e149
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
48 changes: 48 additions & 0 deletions dialogflow/snippets/src/main/dialogflow/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example;

import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedWriter;

public class Example implements HttpFunction {

public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonParser parser = new JsonParser();
Gson gson = new GsonBuilder().create();

JsonObject job = gson.fromJson(request.getReader(), JsonObject.class);
String str =
job.getAsJsonObject("queryResult")
.getAsJsonObject("intent")
.getAsJsonPrimitive("displayName")
.toString();
JsonObject o = null;
String a = '"' + "Default Welcome Intent" + '"';
String b = '"' + "get-agent-name" + '"';
String responseText = "";

if (str.equals(a)) {
responseText = '"' + "Hello from a Java GCF Webhook" + '"';
} else if (str.equals(b)) {
responseText = '"' + "My name is Flowhook" + '"';
} else {
responseText = '"' + "Sorry I didn't get that" + '"';
}

o =
parser
.parse(
"{\"fulfillmentMessages\": [ { \"text\": { \"text\": [ "
+ responseText
+ " ] } } ] }")
.getAsJsonObject();

BufferedWriter writer = response.getWriter();
writer.write(o.toString());
}
}
70 changes: 70 additions & 0 deletions dialogflow/snippets/src/test/dialogflow/ExampleIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dialogflow.cx;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;

import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class ExampleIT {
@Mock private HttpRequest request;
@Mock private HttpResponse response;

private BufferedWriter writerOut;
private StringWriter responseOut;
private static final Gson gson = new Gson();

@Before
public void beforeTest() throws IOException {
MockitoAnnotations.initMocks(this);

// use an empty string as the default request content
BufferedReader reader = new BufferedReader(new StringReader(""));
when(request.getReader()).thenReturn(reader);

responseOut = new StringWriter();
writerOut = new BufferedWriter(responseOut);
when(response.getWriter()).thenReturn(writerOut);
}

@Test
public void helloHttp_bodyParamsPost() throws IOException {
BufferedReader jsonReader =
new BufferedReader(
new StringReader(
"{'queryResult': { 'intent': { 'name': 'projects', 'displayName': 'Default Welcome Intent' } } })"));

when(request.getReader()).thenReturn(jsonReader);

new Webhook().service(request, response);
writerOut.flush();

assertThat(responseOut.toString()).contains("Hello from a Java GCF Webhook");
}
}
1 change: 0 additions & 1 deletion dialogflow/snippets/src/test/dialogflow/SetAgentIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.Test;

public class SetAgentIT {

/*
* We cannot test setAgent because Dialogflow ES can only have one agent
* and if we create a agent it will delete the exisitng testing agent and
Expand Down

0 comments on commit 434e149

Please sign in to comment.