Skip to content

Commit

Permalink
samples: added webhook sample (#313)
Browse files Browse the repository at this point in the history
* 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 <akitsch@google.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Les Vogel <lesv@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 11, 2021
1 parent 4b25209 commit 2cf1322
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
23 changes: 22 additions & 1 deletion dialogflow-cx/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,34 @@
<artifactId>google-cloud-dialogflow-cx</artifactId>
<version>0.10.1</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.9.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
Expand Down
67 changes: 67 additions & 0 deletions dialogflow-cx/snippets/src/main/java/dialogflow/cx/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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;

// [START dialogflow_webhook]

// TODO: add GSON dependency to Pom file
// (https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5)
// TODO: Uncomment the line bellow before running cloud function
// 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("fulfillmentInfo").getAsJsonPrimitive("tag").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(
"{ \"fulfillment_response\": { \"messages\": [ { \"text\": { \"text\": ["
+ responseText
+ "] } } ] } }")
.getAsJsonObject();
BufferedWriter writer = response.getWriter();
writer.write(o.toString());
}
}
// [END dialogflow_webhook]
72 changes: 72 additions & 0 deletions dialogflow-cx/snippets/src/test/java/dialogflow/cx/ExampleIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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, Exception {

String firstHalf = "{\fulfillmentInfo\": {\"tag\": \"Default Welcome Intent\",}";
String secondHalf = ",\"text\": \"hi\",\"languageCode\": \"en\",}";

BufferedReader jsonReader = new BufferedReader(new StringReader(firstHalf + secondHalf));

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

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

assertThat(responseOut.toString()).contains("Hello from a Java GCF Webhook");
}
}

0 comments on commit 2cf1322

Please sign in to comment.