Skip to content

Commit 434e149

Browse files
galz10gcf-owl-bot[bot]
authored andcommitted
samples: add webhook sample (#730)
* 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>
1 parent c5a7160 commit 434e149

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.example;
2+
3+
import com.google.cloud.functions.HttpFunction;
4+
import com.google.cloud.functions.HttpRequest;
5+
import com.google.cloud.functions.HttpResponse;
6+
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonParser;
10+
import java.io.BufferedWriter;
11+
12+
public class Example implements HttpFunction {
13+
14+
public void service(HttpRequest request, HttpResponse response) throws Exception {
15+
JsonParser parser = new JsonParser();
16+
Gson gson = new GsonBuilder().create();
17+
18+
JsonObject job = gson.fromJson(request.getReader(), JsonObject.class);
19+
String str =
20+
job.getAsJsonObject("queryResult")
21+
.getAsJsonObject("intent")
22+
.getAsJsonPrimitive("displayName")
23+
.toString();
24+
JsonObject o = null;
25+
String a = '"' + "Default Welcome Intent" + '"';
26+
String b = '"' + "get-agent-name" + '"';
27+
String responseText = "";
28+
29+
if (str.equals(a)) {
30+
responseText = '"' + "Hello from a Java GCF Webhook" + '"';
31+
} else if (str.equals(b)) {
32+
responseText = '"' + "My name is Flowhook" + '"';
33+
} else {
34+
responseText = '"' + "Sorry I didn't get that" + '"';
35+
}
36+
37+
o =
38+
parser
39+
.parse(
40+
"{\"fulfillmentMessages\": [ { \"text\": { \"text\": [ "
41+
+ responseText
42+
+ " ] } } ] }")
43+
.getAsJsonObject();
44+
45+
BufferedWriter writer = response.getWriter();
46+
writer.write(o.toString());
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.when;
21+
22+
import com.google.cloud.functions.HttpRequest;
23+
import com.google.cloud.functions.HttpResponse;
24+
import com.google.gson.Gson;
25+
import java.io.BufferedReader;
26+
import java.io.BufferedWriter;
27+
import java.io.IOException;
28+
import java.io.StringReader;
29+
import java.io.StringWriter;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.mockito.Mock;
33+
import org.mockito.MockitoAnnotations;
34+
35+
public class ExampleIT {
36+
@Mock private HttpRequest request;
37+
@Mock private HttpResponse response;
38+
39+
private BufferedWriter writerOut;
40+
private StringWriter responseOut;
41+
private static final Gson gson = new Gson();
42+
43+
@Before
44+
public void beforeTest() throws IOException {
45+
MockitoAnnotations.initMocks(this);
46+
47+
// use an empty string as the default request content
48+
BufferedReader reader = new BufferedReader(new StringReader(""));
49+
when(request.getReader()).thenReturn(reader);
50+
51+
responseOut = new StringWriter();
52+
writerOut = new BufferedWriter(responseOut);
53+
when(response.getWriter()).thenReturn(writerOut);
54+
}
55+
56+
@Test
57+
public void helloHttp_bodyParamsPost() throws IOException {
58+
BufferedReader jsonReader =
59+
new BufferedReader(
60+
new StringReader(
61+
"{'queryResult': { 'intent': { 'name': 'projects', 'displayName': 'Default Welcome Intent' } } })"));
62+
63+
when(request.getReader()).thenReturn(jsonReader);
64+
65+
new Webhook().service(request, response);
66+
writerOut.flush();
67+
68+
assertThat(responseOut.toString()).contains("Hello from a Java GCF Webhook");
69+
}
70+
}

dialogflow/snippets/src/test/dialogflow/SetAgentIT.java

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.junit.Test;
2121

2222
public class SetAgentIT {
23-
2423
/*
2524
* We cannot test setAgent because Dialogflow ES can only have one agent
2625
* and if we create a agent it will delete the exisitng testing agent and

0 commit comments

Comments
 (0)