Skip to content

Commit 763dad5

Browse files
galz10gcf-owl-bot[bot]Benjamin E. Coe
authored
docs: add update intent sample (#160)
* 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>
1 parent 81ce2f0 commit 763dad5

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
const {assert} = require('chai');
18+
const {describe, before, it, after} = require('mocha');
19+
const execSync = require('child_process').execSync;
20+
const uuid = require('uuid');
21+
const dialogflow = require('@google-cloud/dialogflow-cx');
22+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
23+
const intentId = [];
24+
const location = 'global';
25+
let agentId = '';
26+
let agentPath = '';
27+
28+
describe('update intent', async () => {
29+
const intentClient = new dialogflow.IntentsClient();
30+
const agentClient = new dialogflow.AgentsClient();
31+
const projectId = await agentClient.getProjectId();
32+
const displayName = `fake_display_name_${uuid.v4().split('-')[0]}`;
33+
const agentDisplayName = `temp_agent_${uuid.v4().split('-')[0]}`;
34+
const parent = 'projects/' + projectId + '/locations/' + location;
35+
const cmd = 'node update-intent.js';
36+
37+
before('get intent ID and agent ID', async () => {
38+
// The path to identify the agent that owns the intents.
39+
40+
const agent = {
41+
displayName: agentDisplayName,
42+
defaultLanguageCode: 'en',
43+
timeZone: 'America/Los_Angeles',
44+
};
45+
46+
const request = {
47+
agent,
48+
parent,
49+
};
50+
51+
const [agentResponse] = await agentClient.createAgent(request);
52+
53+
agentPath = agentResponse.name;
54+
agentId = agentPath.split('/')[5];
55+
56+
const intentRequest = {
57+
parent: agentPath,
58+
};
59+
60+
const [response] = await intentClient.listIntents(intentRequest);
61+
response.forEach(intent => {
62+
intentId.push(intent.name.split('/')[7]);
63+
});
64+
});
65+
66+
after('delete Agent', async () => {
67+
agentClient.deleteAgent({name: agentPath});
68+
});
69+
70+
it('should update an intent using fieldmasks', async () => {
71+
const output = exec(
72+
`${cmd} ${projectId} ${agentId} ${intentId[0]} ${location} ${displayName}`
73+
);
74+
assert.include(output, displayName);
75+
});
76+
});

dialogflow-cx/update-intent.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
async function main(projectId, agentId, intentId, location, displayName) {
18+
// [START dialogflow_cx_update_intent]
19+
20+
const {IntentsClient} = require('@google-cloud/dialogflow-cx');
21+
22+
const intentClient = new IntentsClient();
23+
24+
//TODO(developer): Uncomment these variables before running the sample.
25+
// const projectId = 'your-project-id';
26+
// const agentId = 'your-agent-id';
27+
// const intentId = 'your-intent-id';
28+
// const location = 'your-location';
29+
// const displayName = 'your-display-name';
30+
31+
async function updateIntent() {
32+
const agentPath = intentClient.projectPath(projectId);
33+
const intentPath = `${agentPath}/locations/${location}/agents/${agentId}/intents/${intentId}`;
34+
35+
//Gets the intent from intentPath
36+
const intent = await intentClient.getIntent({name: intentPath});
37+
intent[0].displayName = displayName;
38+
39+
//Specifies what is being updated
40+
const updateMask = {
41+
paths: ['display_name'],
42+
};
43+
44+
const updateIntentRequest = {
45+
intent: intent[0],
46+
updateMask,
47+
languageCode: 'en',
48+
};
49+
50+
//Send the request for update the intent.
51+
const result = await intentClient.updateIntent(updateIntentRequest);
52+
console.log(result);
53+
}
54+
55+
updateIntent();
56+
57+
// [END dialogflow_cx_update_intent]
58+
}
59+
60+
process.on('unhandledRejection', err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
65+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)