Skip to content

Commit 7c374f8

Browse files
authored
chore: split samples into individual files (#622)
* chore: split samples into individual files * chore: removed cli option * chore: update copyright * fix: update to latest format * fix: tests * fix: broken tests
1 parent 5559328 commit 7c374f8

File tree

4 files changed

+177
-144
lines changed

4 files changed

+177
-144
lines changed

dialogflow/create-intent.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2020 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+
/**
18+
* Create an intent.
19+
* @param {string} projectId The project to be used
20+
* @param {string} displayName Display Name
21+
* @param {string} trainingPhrasesParts Training Phrases
22+
* @param {string} messageTexts Message Texts
23+
*/
24+
function main(
25+
projectId = 'YOUR_PROJECT_ID',
26+
displayName = 'YOUR_INTENT_DISPLAY_NAME',
27+
trainingPhrasesParts = [
28+
'Hello, What is weather today?',
29+
'How is the weather today?',
30+
],
31+
messageTexts = ['Rainy', 'Sunny']
32+
) {
33+
// [START dialogflow_create_intent]
34+
35+
/**
36+
* TODO(developer): Uncomment the following lines before running the sample.
37+
*/
38+
// const projectId = 'The Project ID to use, e.g. 'YOUR_GCP_ID';
39+
// const displayName = 'The display name of the intent, e.g. 'MAKE_RESERVATION';
40+
// const trainingPhrasesParts = 'Training phrases, e.g. 'How many people are staying?';
41+
// const messageTexts = 'Message texts for the agent's response when the intent is detected, e.g. 'Your reservation has been confirmed';
42+
43+
// Imports the Dialogflow library
44+
const dialogflow = require('@google-cloud/dialogflow');
45+
46+
// Instantiates the Intent Client
47+
const intentsClient = new dialogflow.IntentsClient();
48+
49+
async function createIntent() {
50+
// Construct request
51+
52+
// The path to identify the agent that owns the created intent.
53+
const agentPath = intentsClient.agentPath(projectId);
54+
55+
const trainingPhrases = [];
56+
57+
trainingPhrasesParts.forEach(trainingPhrasesPart => {
58+
const part = {
59+
text: trainingPhrasesPart,
60+
};
61+
62+
// Here we create a new training phrase for each provided part.
63+
const trainingPhrase = {
64+
type: 'EXAMPLE',
65+
parts: [part],
66+
};
67+
68+
trainingPhrases.push(trainingPhrase);
69+
});
70+
71+
const messageText = {
72+
text: messageTexts,
73+
};
74+
75+
const message = {
76+
text: messageText,
77+
};
78+
79+
const intent = {
80+
displayName: displayName,
81+
trainingPhrases: trainingPhrases,
82+
messages: [message],
83+
};
84+
85+
const createIntentRequest = {
86+
parent: agentPath,
87+
intent: intent,
88+
};
89+
90+
// Create the intent
91+
const [response] = await intentsClient.createIntent(createIntentRequest);
92+
console.log(`Intent ${response.name} created`);
93+
}
94+
95+
createIntent();
96+
97+
// [END dialogflow_create_intent]
98+
}
99+
main(...process.argv.slice(2));

dialogflow/list-intents.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2020 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+
/**
18+
* List of all intents in the specified project.
19+
* @param {string} projectId The project to be used
20+
*/
21+
function main(projectId = 'YOUR_PROJECT_ID') {
22+
// [START dialogflow_list_intents]
23+
24+
/**
25+
* TODO(developer): Uncomment the following lines before running the sample.
26+
*/
27+
// const projectId = 'The Project ID to use, e.g. 'YOUR_GCP_ID';
28+
29+
// Imports the Dialogflow library
30+
const dialogflow = require('@google-cloud/dialogflow');
31+
32+
// Instantiates clients
33+
const intentsClient = new dialogflow.IntentsClient();
34+
35+
async function listIntents() {
36+
// Construct request
37+
38+
// The path to identify the agent that owns the intents.
39+
const projectAgentPath = intentsClient.agentPath(projectId);
40+
41+
console.log(projectAgentPath);
42+
43+
const request = {
44+
parent: projectAgentPath,
45+
};
46+
47+
// Send the request for listing intents.
48+
const [response] = await intentsClient.listIntents(request);
49+
response.forEach(intent => {
50+
console.log('====================');
51+
console.log(`Intent name: ${intent.name}`);
52+
console.log(`Intent display name: ${intent.displayName}`);
53+
console.log(`Action: ${intent.action}`);
54+
console.log(`Root folowup intent: ${intent.rootFollowupIntentName}`);
55+
console.log(`Parent followup intent: ${intent.parentFollowupIntentName}`);
56+
57+
console.log('Input contexts:');
58+
intent.inputContextNames.forEach(inputContextName => {
59+
console.log(`\tName: ${inputContextName}`);
60+
});
61+
62+
console.log('Output contexts:');
63+
intent.outputContexts.forEach(outputContext => {
64+
console.log(`\tName: ${outputContext.name}`);
65+
});
66+
});
67+
}
68+
69+
listIntents();
70+
71+
// [END dialogflow_list_intents]
72+
}
73+
74+
main(...process.argv.slice(2));

dialogflow/resource.js

-136
Original file line numberDiff line numberDiff line change
@@ -18,103 +18,6 @@
1818
// Operations for intents
1919
// /////////////////////////////////////////////////////////////////////////////
2020

21-
async function listIntents(projectId) {
22-
// [START dialogflow_list_intents]
23-
// Imports the Dialogflow library
24-
const dialogflow = require('@google-cloud/dialogflow');
25-
26-
// Instantiates clients
27-
const intentsClient = new dialogflow.IntentsClient();
28-
29-
// The path to identify the agent that owns the intents.
30-
const projectAgentPath = intentsClient.agentPath(projectId);
31-
32-
const request = {
33-
parent: projectAgentPath,
34-
};
35-
36-
console.log(projectAgentPath);
37-
38-
// Send the request for listing intents.
39-
const [response] = await intentsClient.listIntents(request);
40-
response.forEach(intent => {
41-
console.log('====================');
42-
console.log(`Intent name: ${intent.name}`);
43-
console.log(`Intent display name: ${intent.displayName}`);
44-
console.log(`Action: ${intent.action}`);
45-
console.log(`Root folowup intent: ${intent.rootFollowupIntentName}`);
46-
console.log(`Parent followup intent: ${intent.parentFollowupIntentName}`);
47-
48-
console.log('Input contexts:');
49-
intent.inputContextNames.forEach(inputContextName => {
50-
console.log(`\tName: ${inputContextName}`);
51-
});
52-
53-
console.log('Output contexts:');
54-
intent.outputContexts.forEach(outputContext => {
55-
console.log(`\tName: ${outputContext.name}`);
56-
});
57-
});
58-
// [END dialogflow_list_intents]
59-
}
60-
61-
async function createIntent(
62-
projectId,
63-
displayName,
64-
trainingPhrasesParts,
65-
messageTexts
66-
) {
67-
// [START dialogflow_create_intent]
68-
// Imports the Dialogflow library
69-
const dialogflow = require('@google-cloud/dialogflow');
70-
71-
// Instantiates the Intent Client
72-
const intentsClient = new dialogflow.IntentsClient();
73-
74-
// The path to identify the agent that owns the created intent.
75-
const agentPath = intentsClient.agentPath(projectId);
76-
77-
const trainingPhrases = [];
78-
79-
trainingPhrasesParts.forEach(trainingPhrasesPart => {
80-
const part = {
81-
text: trainingPhrasesPart,
82-
};
83-
84-
// Here we create a new training phrase for each provided part.
85-
const trainingPhrase = {
86-
type: 'EXAMPLE',
87-
parts: [part],
88-
};
89-
90-
trainingPhrases.push(trainingPhrase);
91-
});
92-
93-
const messageText = {
94-
text: messageTexts,
95-
};
96-
97-
const message = {
98-
text: messageText,
99-
};
100-
101-
const intent = {
102-
displayName: displayName,
103-
trainingPhrases: trainingPhrases,
104-
messages: [message],
105-
};
106-
107-
const createIntentRequest = {
108-
parent: agentPath,
109-
intent: intent,
110-
};
111-
112-
// Create the intent
113-
const responses = await intentsClient.createIntent(createIntentRequest);
114-
console.log(`Intent ${responses[0].name} created`);
115-
// [END dialogflow_create_intent]
116-
}
117-
11821
async function deleteIntent(projectId, intentId) {
11922
// [START dialogflow_delete_intent]
12023
// Imports the Dialogflow library
@@ -264,45 +167,6 @@ const cli = require('yargs')
264167
.boolean('force')
265168
.alias('force', ['f'])
266169
.describe('force', 'force operation without a prompt')
267-
.command(
268-
'create-intent',
269-
'Create Intent',
270-
{
271-
displayName: {
272-
alias: 'd',
273-
string: true,
274-
demandOption: true,
275-
requiresArg: true,
276-
description: 'Display Name',
277-
},
278-
trainingPhrasesParts: {
279-
alias: 't',
280-
array: true,
281-
string: true,
282-
demandOption: true,
283-
requiresArg: true,
284-
description: 'Training Phrases',
285-
},
286-
messageTexts: {
287-
alias: 'm',
288-
array: true,
289-
string: true,
290-
demandOption: true,
291-
requiresArg: true,
292-
description: 'Message Texts',
293-
},
294-
},
295-
opts =>
296-
createIntent(
297-
opts.projectId,
298-
opts.displayName,
299-
opts.trainingPhrasesParts,
300-
opts.messageTexts
301-
)
302-
)
303-
.command('list-intents', 'List Intent', {}, opts =>
304-
listIntents(opts.projectId)
305-
)
306170
.command(
307171
'delete-intent',
308172
'Delete Intent',

dialogflow/system-test/create-intent.test.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,20 @@ const {assert} = require('chai');
1818
const {after, describe, it} = require('mocha');
1919
const execSync = require('child_process').execSync;
2020
const uuid = require('uuid');
21+
const projectId =
22+
process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT;
2123
const dialogflow = require('@google-cloud/dialogflow');
2224

2325
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
2426

2527
describe('create intent', () => {
2628
const client = new dialogflow.IntentsClient();
27-
const cmd = 'node resource.js';
29+
const cmd = 'node create-intent.js';
2830
const displayName = `fake_display_name_${uuid.v4().split('-')[0]}`;
29-
const phrase1 = 'training_phrase_1';
30-
const phrase2 = 'training_phrase_2';
31-
const message1 = 'message_1';
32-
const message2 = 'message_2';
3331
let intentId;
3432

3533
it('should create an intent', async () => {
36-
const output = exec(
37-
`${cmd} create-intent -d ${displayName} -t ${phrase1} -t ${phrase2} -m ${message1} -m ${message2}`
38-
);
34+
const output = exec(`${cmd} ${projectId} ${displayName}`);
3935
assert.include(output, 'intents');
4036
intentId = output.split(' ')[1].split('/')[4];
4137
});

0 commit comments

Comments
 (0)