From 763dad585aa2d3c89736259ab3ef72da2aeb73a8 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Fri, 20 Aug 2021 13:58:42 -0700 Subject: [PATCH] docs: add update intent sample (#160) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add sample * Fixed lint * lint fix * fix lint * lint fix * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added broken link * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added broken link to skip * Revised come from feedback * Fixed lint and added async * Fixed getProjectID call * Reverted cmd change * Fixing assert since match breaks testing * added match back * added async * reverted to include because match produces error * removed displayName Print * Revised Code * Lint Fix * Removed Debug * Updated Intent Path * lint fix * Added Field comments * added function to main * lint fix * lint fix Co-authored-by: Owl Bot Co-authored-by: Benjamin E. Coe --- dialogflow-cx/test/update-intent.test.js | 76 ++++++++++++++++++++++++ dialogflow-cx/update-intent.js | 65 ++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 dialogflow-cx/test/update-intent.test.js create mode 100644 dialogflow-cx/update-intent.js diff --git a/dialogflow-cx/test/update-intent.test.js b/dialogflow-cx/test/update-intent.test.js new file mode 100644 index 00000000000..63e44cf664a --- /dev/null +++ b/dialogflow-cx/test/update-intent.test.js @@ -0,0 +1,76 @@ +// 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'; + +const {assert} = require('chai'); +const {describe, before, it, after} = require('mocha'); +const execSync = require('child_process').execSync; +const uuid = require('uuid'); +const dialogflow = require('@google-cloud/dialogflow-cx'); +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); +const intentId = []; +const location = 'global'; +let agentId = ''; +let agentPath = ''; + +describe('update intent', async () => { + const intentClient = new dialogflow.IntentsClient(); + const agentClient = new dialogflow.AgentsClient(); + const projectId = await agentClient.getProjectId(); + const displayName = `fake_display_name_${uuid.v4().split('-')[0]}`; + const agentDisplayName = `temp_agent_${uuid.v4().split('-')[0]}`; + const parent = 'projects/' + projectId + '/locations/' + location; + const cmd = 'node update-intent.js'; + + before('get intent ID and agent ID', async () => { + // The path to identify the agent that owns the intents. + + const agent = { + displayName: agentDisplayName, + defaultLanguageCode: 'en', + timeZone: 'America/Los_Angeles', + }; + + const request = { + agent, + parent, + }; + + const [agentResponse] = await agentClient.createAgent(request); + + agentPath = agentResponse.name; + agentId = agentPath.split('/')[5]; + + const intentRequest = { + parent: agentPath, + }; + + const [response] = await intentClient.listIntents(intentRequest); + response.forEach(intent => { + intentId.push(intent.name.split('/')[7]); + }); + }); + + after('delete Agent', async () => { + agentClient.deleteAgent({name: agentPath}); + }); + + it('should update an intent using fieldmasks', async () => { + const output = exec( + `${cmd} ${projectId} ${agentId} ${intentId[0]} ${location} ${displayName}` + ); + assert.include(output, displayName); + }); +}); diff --git a/dialogflow-cx/update-intent.js b/dialogflow-cx/update-intent.js new file mode 100644 index 00000000000..129e4ff4f66 --- /dev/null +++ b/dialogflow-cx/update-intent.js @@ -0,0 +1,65 @@ +// 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'; + +async function main(projectId, agentId, intentId, location, displayName) { + // [START dialogflow_cx_update_intent] + + const {IntentsClient} = require('@google-cloud/dialogflow-cx'); + + const intentClient = new IntentsClient(); + + //TODO(developer): Uncomment these variables before running the sample. + // const projectId = 'your-project-id'; + // const agentId = 'your-agent-id'; + // const intentId = 'your-intent-id'; + // const location = 'your-location'; + // const displayName = 'your-display-name'; + + async function updateIntent() { + const agentPath = intentClient.projectPath(projectId); + const intentPath = `${agentPath}/locations/${location}/agents/${agentId}/intents/${intentId}`; + + //Gets the intent from intentPath + const intent = await intentClient.getIntent({name: intentPath}); + intent[0].displayName = displayName; + + //Specifies what is being updated + const updateMask = { + paths: ['display_name'], + }; + + const updateIntentRequest = { + intent: intent[0], + updateMask, + languageCode: 'en', + }; + + //Send the request for update the intent. + const result = await intentClient.updateIntent(updateIntentRequest); + console.log(result); + } + + updateIntent(); + + // [END dialogflow_cx_update_intent] +} + +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); + +main(...process.argv.slice(2));