Skip to content

Commit d003ec6

Browse files
authored
docs: added webhook sample (#876)
1 parent 6405252 commit d003ec6

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2021 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+
// http://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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const webhook = require('../webhook');
20+
21+
const request = {
22+
body: {
23+
queryResult: {
24+
intent: {
25+
name: 'projects/galstarter-316823/agent/intents/00c2877d-2440-447f-8dc1-045623a55bd4',
26+
displayName: 'Default Welcome Intent',
27+
},
28+
},
29+
},
30+
};
31+
32+
describe('create agent', () => {
33+
it('should test webhook returns correct response', async () => {
34+
const temp = JSON.stringify(request);
35+
let response = '';
36+
37+
const res = {
38+
send: function (s) {
39+
response = JSON.stringify(s);
40+
},
41+
};
42+
43+
webhook.handleWebhook(JSON.parse(temp), res);
44+
assert.include(response, 'Hello from a GCF Webhook');
45+
});
46+
});

dialogflow/webhook.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2021 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+
'use strict';
16+
17+
// [START dialogflow_es_webhook]
18+
19+
exports.handleWebhook = (request, response) => {
20+
const tag = request.body.queryResult.intent.displayName;
21+
22+
let jsonResponse = {};
23+
if (tag === 'Default Welcome Intent') {
24+
//fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
25+
jsonResponse = {
26+
fulfillment_messages: [
27+
{
28+
text: {
29+
//fulfillment text response to be sent to the agent
30+
text: ['Hello from a GCF Webhook'],
31+
},
32+
},
33+
],
34+
};
35+
} else if (tag === 'get-name') {
36+
//fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
37+
jsonResponse = {
38+
fulfillment_messages: [
39+
{
40+
text: {
41+
//fulfillment text response to be sent to the agent
42+
text: ['My name is Flowhook'],
43+
},
44+
},
45+
],
46+
};
47+
} else {
48+
jsonResponse = {
49+
//fulfillment text response to be sent to the agent if there are no defined responses for the specified tag
50+
fulfillment_messages: [
51+
{
52+
text: {
53+
////fulfillment text response to be sent to the agent
54+
text: [
55+
`There are no fulfillment responses defined for "${tag}"" tag`,
56+
],
57+
},
58+
},
59+
],
60+
};
61+
}
62+
response.send(jsonResponse);
63+
};
64+
// [END dialogflow_es_webhook]

0 commit comments

Comments
 (0)