Skip to content

Commit fe6cb09

Browse files
aribraygcf-owl-bot[bot]
authored and
Ace Nassri
committed
docs(samples) - streamliine and clarify webhook samples (#264)
* streamline and clarify webhook samples Change-Id: I80d80b42f60ac6ccc643afb7b2931916d6843813 * remove sessionInfo config Change-Id: I1b14852ad9e238e7cdc04dbb418ed5ed0fca2992 * 🦉 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>
1 parent ec65dff commit fe6cb09

8 files changed

+53
-117
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* Configures a webhook to set form parameters as optional/required
17+
*/
18+
19+
// [START dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]
20+
21+
exports.configureOptionalFormParam = (request, response) => {
22+
// The value of the parameter that the webhook will set as optional or required.
23+
// Note that the webhook cannot add or remove any form parameter
24+
const parameter = request.body.pageInfo.formInfo.parameterInfo[0].value;
25+
26+
const jsonResponse = {
27+
pageInfo: {
28+
formInfo: {
29+
parameterInfo: [
30+
{
31+
displayName: parameter,
32+
// if required: false, the agent will not reprompt for this parameter, even if the state is 'INVALID'
33+
required: true,
34+
state: 'VALID',
35+
},
36+
],
37+
},
38+
},
39+
};
40+
41+
// Info about form parameter that is sent in the webhook response:
42+
console.log(
43+
'Parameter Info: \n',
44+
jsonResponse.pageInfo.formInfo.parameterInfo[0]
45+
);
46+
47+
response.send(jsonResponse);
48+
};
49+
// [END dialogflow_cx_v3_configure_webhooks_to_set_form_parameter_as_optional_or_required]

dialogflow-cx/test/webhook-configure-optional-or-required-form-parameters-test.js renamed to dialogflow-cx/test/configure-webhook-to-set-form-parameter-as-optional-or-required.js

+4-36
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,21 @@
3232

3333
const {assert} = require('chai');
3434
const {describe, it} = require('mocha');
35-
const webhook = require('../webhook-configure-optional-or-required-form-parameters');
36-
const number = 100;
35+
const webhook = require('../configure-webhook-to-set-form-parameter-as-optional-or-required');
3736

3837
describe('configure optional or required form parameter', () => {
3938
it('should test that webhook sets parameter as required', async () => {
4039
const request = {
4140
body: {
4241
fulfillmentInfo: {
43-
tag: 'required',
42+
tag: 'optional-or-required',
4443
},
4544
pageInfo: {
4645
formInfo: {
4746
parameterInfo: [
4847
{
4948
displayName: 'number',
50-
value: number,
49+
value: 100,
5150
},
5251
],
5352
},
@@ -64,37 +63,6 @@ describe('configure optional or required form parameter', () => {
6463
};
6564

6665
webhook.configureOptionalFormParam(JSON.parse(temp), res);
67-
assert.include(response, 'This parameter is required.');
68-
});
69-
70-
it('should test that webhook sets parameter as optional', async () => {
71-
const request = {
72-
body: {
73-
fulfillmentInfo: {
74-
tag: 'optional',
75-
},
76-
pageInfo: {
77-
formInfo: {
78-
parameterInfo: [
79-
{
80-
displayName: 'number',
81-
value: number,
82-
},
83-
],
84-
},
85-
},
86-
},
87-
};
88-
const temp = JSON.stringify(request);
89-
let response = '';
90-
91-
const res = {
92-
send: function (s) {
93-
response = JSON.stringify(s);
94-
},
95-
};
96-
97-
webhook.configureOptionalFormParam(JSON.parse(temp), res);
98-
assert.include(response, 'This parameter is optional.');
66+
assert.include(response, '"required":true');
9967
});
10068
});

dialogflow-cx/webhook-configure-optional-or-required-form-parameters.js

-71
This file was deleted.

dialogflow-cx/webhook-configure-session-parameters-enable-agent-response.js

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
// [START dialogflow_cx_v3_webhook_configure_session_parameters_enable_agent_response]
2020

21-
// TODO (developer): change entry point to enableAgentResponse in Cloud Function
22-
2321
exports.enableAgentResponse = (request, response) => {
2422
const tag = request.body.fulfillmentInfo.tag;
2523
// The value of the parameter used to enable agent response

dialogflow-cx/webhook-configure-session-parameters-trigger-transition.js

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
// [START dialogflow_cx_v3_webhook_configure_session_parameters_trigger_transition]
2020

21-
// TODO (developer): change entry point to triggerTransition in Cloud Function
22-
2321
exports.triggerTransition = (request, response) => {
2422
// The target page to transition to.
2523
const targetPage = request.body.targetPage; // Must be format projects/<Project ID>/locations/<Location ID>/agents/<Agent ID>/flows/<Flow ID>/pages/<Page ID>

dialogflow-cx/webhook-configure-session-parameters.js

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
// [START dialogflow_cx_v3_webhook_configure_session_parameters]
2020

21-
// TODO (developer): change entry point to configureSessionParams in Cloud Function
22-
2321
exports.configureSessionParams = (request, response) => {
2422
const tag = request.body.fulfillmentInfo.tag;
2523
let newSessionParameter;

dialogflow-cx/webhook-validate-form-parameter.js

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
// [START dialogflow_cx_v3_webhook_validate_form_parameter]
2020

21-
// TODO (developer): change entry point to validateParameter in Cloud Function
22-
2321
exports.validateParameter = (request, response) => {
2422
// The value of the parameter to validate
2523
let paramToValidate = request.body.pageInfo.formInfo.parameterInfo[0].value;

dialogflow-cx/webhooks.js

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
// [START dialogflow_cx_webhook]
1818

19-
// TODO: change entry point to handleWebhook in cloud function
20-
2119
exports.handleWebhook = (request, response) => {
2220
const tag = request.body.fulfillmentInfo.tag;
2321
let text = '';

0 commit comments

Comments
 (0)