Skip to content

Commit

Permalink
docs(samples): add webhook samples (#479)
Browse files Browse the repository at this point in the history
* docs:add configure webhook to set form parameter as optional or required sample and test

Change-Id: I75bed489bd8abefca60263acb266501810c0e856

* docs: fix checkstyle

Change-Id: I18be2a5c1d46ac9838cf4a15ab87dcf331ee26ee

* fix checkstyle

Change-Id: Ia85899f678ef1382718c2443a682770417340af2

* fix checkstyle

Change-Id: I4dd56107a3197027f287980fa87a15e962154783

* fix multiline string

Change-Id: I8dcdb0dcaad486a2fb69e0737df99bda49b28839

* fix checkstyle violations

Change-Id: I5f4521b3e50660f30f9f73da3d939da213084d28

* fix checkstyle violations

Change-Id: I39dedfba1b62be5cb4580a42dde270146f3b6f97

* remove unused variable

Change-Id: Ic54ae0d1f091079661f3fa67097cdcd2e888dc90

* fix checkstyle violations

Change-Id: Ifd303b7395844240dca0b29cde9b791112fcabe5

* 🦉 Updates from OwlBot post-processor

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

* fix checkstyle violations

Change-Id: I59e45da9f9e57b327a527f1405816ac1275fe1c8

* fix checkstyle violations

Change-Id: I70ff4118cc8b3111a3ed55a2fae32258d3f6819c

* 🦉 Updates from OwlBot post-processor

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

* remove unnecessary samples

Change-Id: I395dd5abc9a48733126ab0ecdc2a974fbf9eb471

* add webhook configure session parameter sample and test

Change-Id: Ia599ab7f4d432477d9952b4e12c709845f1ad97b

* add webhook validate form parameter sample and test

Change-Id: I8cdf93fd9e21161757d198b1338c5ab77bb3a236

* fix checkstyle violations

Change-Id: I90f1f626588fd59c116d119348054c914945cd1d

* fix checkstyle violations

Change-Id: I5ff686ab2ffc94b358bdf87b8dfc3d00f5e45657

* 🦉 Updates from OwlBot post-processor

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

* update to use gson

Change-Id: I6f645ff225e42a3485773af1b63e7b3378f95bb2

* update to use gson

Change-Id: I155a40ae8def8472b3e08551fc3dc112b06c68ec

* update webhook-optional-or-required to use gson

Change-Id: I6b4c19023982668c9c5d0c901bb72c41d3ca7457

* update test

Change-Id: I43ce833251dadfbc9e6c3e652ff1f278a2f7c2f2

* update to use gson

Change-Id: I509e163dcb69161ad8c18147bd2116b7014d4aef

* fix checkstyle violations

Change-Id: I2f882c298379630b71dbb533173ec3cc9ca4aee1

* 🦉 Updates from OwlBot post-processor

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

* v3beta1 to v3

Change-Id: If9d465d862ca5c7ae7f5e1fb03a5b09c878997a8

* add flag to kokoro build script

Change-Id: Ib5c9ccd81e1e6fabe1ed79fe6d3c96f455480c16

* remove flag

Change-Id: Ie1fc788f64c6dc14763f0c2df3ac9327ee7a1044

* add flag to kokoro build script

Change-Id: Ie7821b49ea0b954fdcf6ad7f0a5efd57c865c20c

* remove flag

Change-Id: I3ae6982f6a4f8dc3b2b4e5a404e5e46c8c8d6aaa

* add kokoro flag

Change-Id: Id7a7aae81cd43d920fa1fe750fb259b0d2ea0581

* 🦉 Updates from OwlBot post-processor

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

* add kokoro flag

Change-Id: I3a4733a05b86a135642c4667a52ab7b09f462559

* fix formatting

Change-Id: I491c924ed2fc6dff078f1d7944a3590a24b7530f

* 🦉 Updates from OwlBot post-processor

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

* remove extra gson

Change-Id: I7f6b52e600a8b2513f59560fe3e727dd5d822319

* fix formatting

Change-Id: Icb1ff15957d93fab961b49f05492282c4363fdcd

* fix formatting

Change-Id: Iabb1e248e03bd5300b9aad10e0504d8d96ea564b

* fix formatting

Change-Id: Icd6976bd8fc1795accfda722e00270aebf69fa43

* restructure json format

Change-Id: I1e919a48b857e4bb4e33fb26265ec7320b1ece24

* restructure json format

Change-Id: Ib36de1d54869dad920e1743834be56a84d8398ba

* restructure json format

Change-Id: I5c7be6894f91e26a8b59a46635a65953728ae591

* restructure mockito stubbing

Change-Id: Id67943d85113ba436844fe5b6a39593a030a89cb

* restructure mockito stubbing

Change-Id: Ib1df6375ec71353f4ab62bf83c6ff7712da59acb

* restructure mockito stubbing

Change-Id: I3243a7b5b70010d9f181039983bdeb35098c72b2

* restructure mockito stubbing

Change-Id: Idd80a7b9a29f0daaabc866b35b685efff1a80f94

* restructure mocks and variables

Change-Id: I2faa86d33aaf5d0b8a9941779f2b17934e848c74

* restructure mocks and variables

Change-Id: I9accbd7282c92b409c619e717cc05aa694d6e3e7

* restructure mocks and variables

Change-Id: I03bdc68502f8deff333e68cd3fb36d080a9f44ca

* fix variable names

Change-Id: Ic196c389141e834c4d7cc54531de808b0c941801

* fix variable names

Change-Id: I5d4ce6132bfdfb1d709e589eaff060520da8fcc0

* remove unnecessary mock

Change-Id: Ibff1531d1a6e6de27fee6e409d005d657ad2c86d

* remove static

Change-Id: Ie67fd74d024957fc8584794f30ccc1f953fb39e4

* add annotations

Change-Id: I727e43c0b98c4641414d950658e78c5c30b66506

* add annotations

Change-Id: Ib594629756b704a701739188e2d59c0273d68a8f

* add runwith import

Change-Id: I32b0746e89b2131106ef965b1712981633b8488f

* restructure test and mocks

Change-Id: I5bdf09bd4c262c20ec84ad4255170b084c0caa69

* fix imports

Change-Id: I48b1628e4f60372a767c354dcd78e9c1b3e0f415

* fix imports

Change-Id: Icbc59fc1d313a7224c2d472b49ca144f217bbf77

* fix imports

Change-Id: I2591dbdd32e3f97e7f61f38d88e222ed11418c25

* rename test files

Change-Id: Ifcd47fd1d94f7064a0fa178a36fe3e803a8bb234

* 🦉 Updates from OwlBot post-processor

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

* fix imports

Change-Id: I65c7c53359c87b0c00afe02871f5405ce629d996

* add variable

Change-Id: I140b08fdb80654140055e292057c0ddc74d23921

* remove json files and fix checkstyle

Change-Id: Idf77efee6e42f430efab1c9aae662756d4feb92c

* make public

Change-Id: I6bfaf4c002f513b3e517394d745c1548f886de77

* remove print

Change-Id: Iff827980844223c3323db37df587a075c1f226c5

* add flag to kokoro build script

Change-Id: I7a2d667a3460a59963b592d1504d0a465cc03565

* add mock

Change-Id: Iec351ef910c551a5a511454ad8edf3da6aec0734

* move mocks

Change-Id: Ic1b22fd72c7e624bff8f19767ce5b15f44356e7d

* 🦉 Updates from OwlBot post-processor

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
aribray and gcf-owl-bot[bot] authored Jul 16, 2022
1 parent 5f98884 commit 86a3ee5
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 0 deletions.
3 changes: 3 additions & 0 deletions java-dialogflow-cx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow-cx

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Configure Webhook To Set Form Parameters As Optional Or Required | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ConfigureWebhookToSetFormParametersAsOptionalOrRequired.java) |
| Create Agent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/CreateAgent.java) |
| Create Flow | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/CreateFlow.java) |
| Create Intent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/CreateIntent.java) |
Expand All @@ -99,6 +100,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-dialogflow-cx
| List Test Case Results | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListTestCaseResults.java) |
| List Training Phrases | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/ListTrainingPhrases.java) |
| Update Intent | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/UpdateIntent.java) |
| Webhook Configure Session Parameters | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/WebhookConfigureSessionParameters.java) |
| Webhook Validate Form Parameter | [source code](https://github.com/googleapis/java-dialogflow-cx/blob/main/samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-dialogflow-cx&page=editor&open_in_editor=samples/snippets/src/main/java/dialogflow/cx/WebhookValidateFormParameter.java) |



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2022 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;

// The following snippet is used in https://cloud.google.com/dialogflow/cx/docs/concept/webhook

// [START dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]

// TODO: Change class name to Example
// TODO: Uncomment the line below 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.JsonArray;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;

public class ConfigureWebhookToSetFormParametersAsOptionalOrRequired implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject parameterObject = new JsonObject();
parameterObject.addProperty("display_name", "order_number");
parameterObject.addProperty("required", "true");
parameterObject.addProperty("state", "VALID");

JsonArray parameterInfoList = new JsonArray();
parameterInfoList.add(parameterObject);

JsonObject parameterInfoObject = new JsonObject();
parameterInfoObject.add("parameter_info", parameterInfoList);

JsonObject formInfo = new JsonObject();
formInfo.add("form_info", parameterInfoObject);

// Constructs the webhook response object
JsonObject webhookResponse = new JsonObject();
webhookResponse.add("page_info", formInfo);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

System.out.println("Response Object: \n");
System.out.println(jsonResponseObject.toString());

/* {
* "page_info": {
* "form_info": {
* "parameter_info": [
* {
* "display_name": "order_number",
* "required": "true",
* "state": "VALID"
* }
* ]
* }
* }
* }
*/

BufferedWriter writer = response.getWriter();

// Sends the responseObject
writer.write(jsonResponseObject.toString());
}
}
// [END dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2022 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;

// The following snippet is used in https://cloud.google.com/dialogflow/cx/docs/concept/webhook
// Configures a webhook to set a session parameter

// [START dialogflow_cx_v3_webhook_configure_session_parameters]

// TODO: Change class name to Example
// TODO: Uncomment the line below 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 java.io.BufferedWriter;

public class WebhookConfigureSessionParameters implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject orderParameter = new JsonObject();
orderParameter.addProperty("order_number", "12345");

JsonObject parameterObject = new JsonObject();
parameterObject.add("parameters", orderParameter);

// Creates webhook response object
JsonObject webhookResponse = new JsonObject();
webhookResponse.add("session_info", parameterObject);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

System.out.println("Session Parameter Info: \n");
System.out.println(jsonResponseObject.toString());

/** { "session_info": { "parameters": { "order_number": "12345" } } } */
BufferedWriter writer = response.getWriter();
// Sends the webhookResponseObject
writer.write(jsonResponseObject.toString());
}
}
// [END dialogflow_cx_v3_webhook_configure_session_parameters]
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2022 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;

// The following snippet is used in https://cloud.google.com/dialogflow/cx/docs/concept/webhook

// [START dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]

// TODO: Change class name to Example
// TODO: Uncomment the line below 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.JsonArray;
import com.google.gson.JsonObject;
import java.io.BufferedWriter;

public class WebhookValidateFormParameter implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
JsonObject sessionInfo = new JsonObject();
JsonObject sessionParameter = new JsonObject();

sessionParameter.addProperty("order_number", "null");
sessionInfo.add("parameters", sessionParameter);

JsonObject parameterObject = new JsonObject();
parameterObject.addProperty("display_name", "order_number");
parameterObject.addProperty("required", "true");
parameterObject.addProperty("state", "INVALID");
parameterObject.addProperty("value", "123");

JsonArray parameterInfoList = new JsonArray();
parameterInfoList.add(parameterObject);

JsonObject parameterInfoObject = new JsonObject();
parameterInfoObject.add("parameter_info", parameterInfoList);

JsonObject pageInfo = new JsonObject();
pageInfo.add("form_info", parameterInfoObject);

// Constructs the webhook response object
JsonObject webhookResponse = new JsonObject();
webhookResponse.add("page_info", pageInfo);
webhookResponse.add("session_info", sessionInfo);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonResponseObject = gson.toJson(webhookResponse);

System.out.println("Response Object: \n");
System.out.println(jsonResponseObject.toString());
/**
* { "page_info": { "form_info": { "parameter_info": [ { "display_name": "order_number",
* "required": "true", "state": "INVALID", "value": "123" } ] } }, "session_info": {
* "parameters": { "order_number": "null" } } }
*/
BufferedWriter writer = response.getWriter();

// Sends the responseObject
writer.write(jsonResponseObject.toString());
}
}
// [END dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2022 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.dialogflow.cx.v3beta1.WebhookRequest;
import com.google.cloud.dialogflow.cx.v3beta1.WebhookRequest.FulfillmentInfo;
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.JsonArray;
import com.google.gson.JsonObject;
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 ConfigureWebhookToSetFormParametersAsOptionalOrRequiredIT {

@Mock HttpRequest request;
@Mock HttpResponse response;

BufferedReader jsonReader;
StringReader stringReader;
BufferedWriter writerOut;
StringWriter responseOut;

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

stringReader = new StringReader("{'fulfillmentInfo': {'tag': 'validate-form-parameter'}}");
jsonReader = new BufferedReader(stringReader);

responseOut = new StringWriter();
writerOut = new BufferedWriter(responseOut);

when(request.getReader()).thenReturn(jsonReader);
when(response.getWriter()).thenReturn(writerOut);
}

@Test
public void helloHttp_bodyParamsPost() throws IOException, Exception {

FulfillmentInfo fulfillmentInfo =
FulfillmentInfo.newBuilder().setTag("configure-session-parameters").build();

WebhookRequest webhookRequest =
WebhookRequest.newBuilder().setFulfillmentInfo(fulfillmentInfo).build();

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

JsonObject parameterObject = new JsonObject();
parameterObject.addProperty("display_name", "order_number");
parameterObject.addProperty("required", "true");
parameterObject.addProperty("state", "VALID");

JsonArray parameterInfoList = new JsonArray();
parameterInfoList.add(parameterObject);

JsonObject parameterInfoObject = new JsonObject();
parameterInfoObject.add("parameter_info", parameterInfoList);

JsonObject formInfo = new JsonObject();
formInfo.add("form_info", parameterInfoObject);

JsonObject webhookResponse = new JsonObject();
webhookResponse.add("page_info", formInfo);

Gson gson = new GsonBuilder().setPrettyPrinting().create();
String expectedResponse = gson.toJson(webhookResponse);

assertThat(responseOut.toString()).isEqualTo(expectedResponse);
}
}
Loading

0 comments on commit 86a3ee5

Please sign in to comment.