Skip to content

Commit 19fde4e

Browse files
galz10averikitschgcf-owl-bot[bot]lesv
authored andcommitted
samples: added webhook sample (#313)
* samples: added webhook sample * fixed lint * added cloud function dependecy * moved cloud function dependecy * changed pom file * moved maven dependency * changed dependecy * added scope * added mocks * added dependency * Added plugin * Orginized code * modified pom file * removed httprequest from test * added to poms * Changed test to include functions * removed scope * change cloud function version * reverted version * changed cloud function version to 1.0.4 * added dependency to more pom files * added plugin * removed dependency from install-wo-bom * removed plugin * added dependency * added plugin * Added Dependency to samples pom file * lint fix * changed dependencies * Added Dependencies * added dependency * lint fix * lint fix * removed dependency * removed dependency * added snapshot dependency * samples: added webhook sample * fixed lint * added cloud function dependecy * moved cloud function dependecy * changed pom file * moved maven dependency * changed dependecy * added scope * added mocks * added dependency * Added plugin * Orginized code * modified pom file * removed httprequest from test * added to poms * Changed test to include functions * removed scope * change cloud function version * reverted version * changed cloud function version to 1.0.4 * added dependency to more pom files * added plugin * removed dependency from install-wo-bom * removed plugin * added dependency * added plugin * Added Dependency to samples pom file * lint fix * changed dependencies * Added Dependencies * added dependency * lint fix * lint fix * removed dependency * removed dependency * added snapshot dependency * Add debugging flags * debug * debug * Update build.sh * Update build.sh * Update build.sh * Update build.sh * Update build.sh * Update build.sh * Update build.sh * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Update pom.xml add the closing magic tag Co-authored-by: averikitsch <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Les Vogel <[email protected]>
1 parent 76ec3df commit 19fde4e

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

dialogflow-cx/snippets/pom.xml

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,34 @@
3030
<artifactId>google-cloud-dialogflow-cx</artifactId>
3131
<version>0.10.1</version>
3232
</dependency>
33-
33+
<dependency>
34+
<groupId>com.google.code.gson</groupId>
35+
<artifactId>gson</artifactId>
36+
<version>2.8.7</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.google.cloud.functions</groupId>
40+
<artifactId>functions-framework-api</artifactId>
41+
<version>1.0.4</version>
42+
</dependency>
3443
<dependency>
3544
<groupId>junit</groupId>
3645
<artifactId>junit</artifactId>
3746
<version>4.13.2</version>
3847
<scope>test</scope>
3948
</dependency>
49+
<dependency>
50+
<groupId>com.google.cloud.functions</groupId>
51+
<artifactId>function-maven-plugin</artifactId>
52+
<version>0.9.7</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.mockito</groupId>
57+
<artifactId>mockito-core</artifactId>
58+
<version>3.12.4</version>
59+
<scope>test</scope>
60+
</dependency>
4061
<dependency>
4162
<groupId>com.google.truth</groupId>
4263
<artifactId>truth</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
// [START dialogflow_webhook]
20+
21+
// TODO: add GSON dependency to Pom file
22+
// (https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5)
23+
// TODO: Uncomment the line bellow before running cloud function
24+
// package com.example;
25+
26+
import com.google.cloud.functions.HttpFunction;
27+
import com.google.cloud.functions.HttpRequest;
28+
import com.google.cloud.functions.HttpResponse;
29+
import com.google.gson.Gson;
30+
import com.google.gson.GsonBuilder;
31+
import com.google.gson.JsonObject;
32+
import com.google.gson.JsonParser;
33+
import java.io.BufferedWriter;
34+
35+
public class Example implements HttpFunction {
36+
37+
public void service(HttpRequest request, HttpResponse response) throws Exception {
38+
JsonParser parser = new JsonParser();
39+
Gson gson = new GsonBuilder().create();
40+
41+
JsonObject job = gson.fromJson(request.getReader(), JsonObject.class);
42+
String str = job.getAsJsonObject("fulfillmentInfo").getAsJsonPrimitive("tag").toString();
43+
JsonObject o = null;
44+
String a = '"' + "Default Welcome Intent" + '"';
45+
String b = '"' + "get-agent-name" + '"';
46+
String responseText = "";
47+
48+
if (str.equals(a)) {
49+
responseText = '"' + "Hello from a Java GCF Webhook" + '"';
50+
} else if (str.equals(b)) {
51+
responseText = '"' + "My name is Flowhook" + '"';
52+
} else {
53+
responseText = '"' + "Sorry I didn't get that" + '"';
54+
}
55+
56+
o =
57+
parser
58+
.parse(
59+
"{ \"fulfillment_response\": { \"messages\": [ { \"text\": { \"text\": ["
60+
+ responseText
61+
+ "] } } ] } }")
62+
.getAsJsonObject();
63+
BufferedWriter writer = response.getWriter();
64+
writer.write(o.toString());
65+
}
66+
}
67+
// [END dialogflow_webhook]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
37+
@Mock private HttpRequest request;
38+
@Mock private HttpResponse response;
39+
40+
private BufferedWriter writerOut;
41+
private StringWriter responseOut;
42+
private static final Gson gson = new Gson();
43+
44+
@Before
45+
public void beforeTest() throws IOException {
46+
MockitoAnnotations.initMocks(this);
47+
48+
// use an empty string as the default request content
49+
BufferedReader reader = new BufferedReader(new StringReader(""));
50+
when(request.getReader()).thenReturn(reader);
51+
52+
responseOut = new StringWriter();
53+
writerOut = new BufferedWriter(responseOut);
54+
when(response.getWriter()).thenReturn(writerOut);
55+
}
56+
57+
@Test
58+
public void helloHttp_bodyParamsPost() throws IOException, Exception {
59+
60+
String firstHalf = "{\fulfillmentInfo\": {\"tag\": \"Default Welcome Intent\",}";
61+
String secondHalf = ",\"text\": \"hi\",\"languageCode\": \"en\",}";
62+
63+
BufferedReader jsonReader = new BufferedReader(new StringReader(firstHalf + secondHalf));
64+
65+
when(request.getReader()).thenReturn(jsonReader);
66+
67+
new Example().service(request, response);
68+
writerOut.flush();
69+
70+
assertThat(responseOut.toString()).contains("Hello from a Java GCF Webhook");
71+
}
72+
}

0 commit comments

Comments
 (0)