-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
c5a7160
commit 434e149
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters