Skip to content

Commit

Permalink
docs: add update intent sample (#160)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
3 people authored Aug 20, 2021
1 parent 81ce2f0 commit 763dad5
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
76 changes: 76 additions & 0 deletions dialogflow-cx/test/update-intent.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
65 changes: 65 additions & 0 deletions dialogflow-cx/update-intent.js
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 763dad5

Please sign in to comment.