Skip to content

Commit

Permalink
docs: added webhook sample (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
galz10 authored and NimJay committed Nov 18, 2022
1 parent 2d95ec7 commit 3c6b041
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
46 changes: 46 additions & 0 deletions dialogflow/system-test/webhook.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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');

const request = {
body: {
queryResult: {
intent: {
name: 'projects/galstarter-316823/agent/intents/00c2877d-2440-447f-8dc1-045623a55bd4',
displayName: 'Default Welcome Intent',
},
},
},
};

describe('create agent', () => {
it('should test webhook returns correct response', async () => {
const temp = JSON.stringify(request);
let response = '';

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

webhook.handleWebhook(JSON.parse(temp), res);
assert.include(response, 'Hello from a GCF Webhook');
});
});
64 changes: 64 additions & 0 deletions dialogflow/webhook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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
//
// 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';

// [START dialogflow_es_webhook]

exports.handleWebhook = (request, response) => {
const tag = request.body.queryResult.intent.displayName;

let jsonResponse = {};
if (tag === 'Default Welcome Intent') {
//fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
jsonResponse = {
fulfillment_messages: [
{
text: {
//fulfillment text response to be sent to the agent
text: ['Hello from a GCF Webhook'],
},
},
],
};
} else if (tag === 'get-name') {
//fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
jsonResponse = {
fulfillment_messages: [
{
text: {
//fulfillment text response to be sent to the agent
text: ['My name is Flowhook'],
},
},
],
};
} else {
jsonResponse = {
//fulfillment text response to be sent to the agent if there are no defined responses for the specified tag
fulfillment_messages: [
{
text: {
////fulfillment text response to be sent to the agent
text: [
`There are no fulfillment responses defined for "${tag}"" tag`,
],
},
},
],
};
}
response.send(jsonResponse);
};
// [END dialogflow_es_webhook]

0 comments on commit 3c6b041

Please sign in to comment.