Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

feat: add Webhook samples #259

Merged
merged 14 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-dialogflow-
| Long-running-operation | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/long-running-operation.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/long-running-operation.js,samples/README.md) |
| Quickstart | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
| Update-intent | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/update-intent.js,samples/README.md) |
| Webhook-configure-optional-or-required-form-parameters | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-optional-or-required-form-parameters.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-optional-or-required-form-parameters.js,samples/README.md) |
| Webhook-configure-session-parameters-enable-agent-response | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-enable-agent-response.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-enable-agent-response.js,samples/README.md) |
| Webhook-configure-session-parameters-trigger-transition | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-trigger-transition.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-trigger-transition.js,samples/README.md) |
| Webhook-configure-session-parameters | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters.js,samples/README.md) |
| Webhook-validate-form-parameter | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-validate-form-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-validate-form-parameter.js,samples/README.md) |
| Webhooks | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhooks.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhooks.js,samples/README.md) |


Expand Down
90 changes: 90 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
* [Long-running-operation](#long-running-operation)
* [Quickstart](#quickstart)
* [Update-intent](#update-intent)
* [Webhook-configure-optional-or-required-form-parameters](#webhook-configure-optional-or-required-form-parameters)
* [Webhook-configure-session-parameters-enable-agent-response](#webhook-configure-session-parameters-enable-agent-response)
* [Webhook-configure-session-parameters-trigger-transition](#webhook-configure-session-parameters-trigger-transition)
* [Webhook-configure-session-parameters](#webhook-configure-session-parameters)
* [Webhook-validate-form-parameter](#webhook-validate-form-parameter)
* [Webhooks](#webhooks)

## Before you begin
Expand Down Expand Up @@ -263,6 +268,91 @@ __Usage:__



### Webhook-configure-optional-or-required-form-parameters

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-optional-or-required-form-parameters.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-optional-or-required-form-parameters.js,samples/README.md)

__Usage:__


`node samples/webhook-configure-optional-or-required-form-parameters.js`


-----




### Webhook-configure-session-parameters-enable-agent-response

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-enable-agent-response.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-enable-agent-response.js,samples/README.md)

__Usage:__


`node samples/webhook-configure-session-parameters-enable-agent-response.js`


-----




### Webhook-configure-session-parameters-trigger-transition

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters-trigger-transition.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters-trigger-transition.js,samples/README.md)

__Usage:__


`node samples/webhook-configure-session-parameters-trigger-transition.js`


-----




### Webhook-configure-session-parameters

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters.js,samples/README.md)

__Usage:__


`node samples/webhook-configure-session-parameters.js`


-----




### Webhook-validate-form-parameter

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-validate-form-parameter.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-validate-form-parameter.js,samples/README.md)

__Usage:__


`node samples/webhook-validate-form-parameter.js`


-----




### Webhooks

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhooks.js).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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
//
// https://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.

'use strict';

// 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.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');
const webhook = require('../webhook-configure-optional-or-required-form-parameters');
const number = 100;

describe('configure optional or required form parameter', () => {
it('should test that webhook sets parameter as required', async () => {
const request = {
body: {
fulfillmentInfo: {
tag: 'required',
},
pageInfo: {
formInfo: {
parameterInfo: [
{
displayName: 'number',
value: number,
},
],
},
},
},
};
const temp = JSON.stringify(request);
let response = '';

const res = {
send: function (s) {
response = JSON.stringify(s);
},
};

webhook.configureOptionalFormParam(JSON.parse(temp), res);
assert.include(response, 'This parameter is required.');
});

it('should test that webhook sets parameter as optional', async () => {
const request = {
body: {
fulfillmentInfo: {
tag: 'optional',
},
pageInfo: {
formInfo: {
parameterInfo: [
{
displayName: 'number',
value: number,
},
],
},
},
},
};
const temp = JSON.stringify(request);
let response = '';

const res = {
send: function (s) {
response = JSON.stringify(s);
},
};

webhook.configureOptionalFormParam(JSON.parse(temp), res);
assert.include(response, 'This parameter is optional.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// 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
//
// https://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.

'use strict';

// 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.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');
const webhook = require('../webhook-configure-session-parameters-enable-agent-response');
const number = 100;

describe('enable agent response', () => {
it('should test webhook increases value of session parameter', async () => {
const request = {
body: {
fulfillmentInfo: {
tag: 'increase number',
},
sessionInfo: {
parameters: {
number: number,
},
},
},
};
const temp = JSON.stringify(request);
let response = '';

const res = {
send: function (s) {
response = JSON.stringify(s);
},
};

webhook.enableAgentResponse(JSON.parse(temp), res);
assert.include(response, 'increased value');
});

it('should test webhook decreases value of session parameter', async () => {
const request = {
body: {
fulfillmentInfo: {
tag: 'decrease number',
},
sessionInfo: {
parameters: {
number: number,
},
},
},
};
const temp = JSON.stringify(request);
let response = '';

const res = {
send: function (s) {
response = JSON.stringify(s);
},
};

webhook.enableAgentResponse(JSON.parse(temp), res);
assert.include(response, 'decreased value');
});
});
79 changes: 79 additions & 0 deletions samples/test/webhook-configure-session-parameters-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// 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
//
// https://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.

'use strict';

// 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.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');
const webhook = require('../webhook-configure-session-parameters');

describe('configure session parameters', () => {
it('should test that webhook adds new session parameter', async () => {
const request = {
body: {
fulfillmentInfo: {
tag: 'month',
},
},
};
const temp = JSON.stringify(request);
let response = '';

const res = {
send: function (s) {
response = JSON.stringify(s);
},
};

webhook.configureSessionParams(JSON.parse(temp), res);
assert.include(response, 'January');
});

it('should test that webhook configures session parameter', async () => {
const request = {
body: {
fulfillmentInfo: {
tag: 'year',
},
},
};
const temp = JSON.stringify(request);
let response = '';

const res = {
send: function (s) {
response = JSON.stringify(s);
},
};

webhook.configureSessionParams(JSON.parse(temp), res);
assert.include(response, '1999');
});
});
Loading