From 35895c6ae3e591515fe337493a3f17e54b15374a Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Wed, 10 Jun 2020 16:44:53 -0700 Subject: [PATCH] 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 --- dialogflow/create-intent.js | 99 ++++++++++++++ dialogflow/list-intents.js | 74 ++++++++++ dialogflow/resource.js | 136 ------------------- dialogflow/system-test/create-intent.test.js | 12 +- 4 files changed, 177 insertions(+), 144 deletions(-) create mode 100644 dialogflow/create-intent.js create mode 100644 dialogflow/list-intents.js diff --git a/dialogflow/create-intent.js b/dialogflow/create-intent.js new file mode 100644 index 0000000000..1b8994d2a9 --- /dev/null +++ b/dialogflow/create-intent.js @@ -0,0 +1,99 @@ +// Copyright 2020 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'; + +/** + * Create an intent. + * @param {string} projectId The project to be used + * @param {string} displayName Display Name + * @param {string} trainingPhrasesParts Training Phrases + * @param {string} messageTexts Message Texts + */ +function main( + projectId = 'YOUR_PROJECT_ID', + displayName = 'YOUR_INTENT_DISPLAY_NAME', + trainingPhrasesParts = [ + 'Hello, What is weather today?', + 'How is the weather today?', + ], + messageTexts = ['Rainy', 'Sunny'] +) { + // [START dialogflow_create_intent] + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'The Project ID to use, e.g. 'YOUR_GCP_ID'; + // const displayName = 'The display name of the intent, e.g. 'MAKE_RESERVATION'; + // const trainingPhrasesParts = 'Training phrases, e.g. 'How many people are staying?'; + // const messageTexts = 'Message texts for the agent's response when the intent is detected, e.g. 'Your reservation has been confirmed'; + + // Imports the Dialogflow library + const dialogflow = require('@google-cloud/dialogflow'); + + // Instantiates the Intent Client + const intentsClient = new dialogflow.IntentsClient(); + + async function createIntent() { + // Construct request + + // The path to identify the agent that owns the created intent. + const agentPath = intentsClient.agentPath(projectId); + + const trainingPhrases = []; + + trainingPhrasesParts.forEach(trainingPhrasesPart => { + const part = { + text: trainingPhrasesPart, + }; + + // Here we create a new training phrase for each provided part. + const trainingPhrase = { + type: 'EXAMPLE', + parts: [part], + }; + + trainingPhrases.push(trainingPhrase); + }); + + const messageText = { + text: messageTexts, + }; + + const message = { + text: messageText, + }; + + const intent = { + displayName: displayName, + trainingPhrases: trainingPhrases, + messages: [message], + }; + + const createIntentRequest = { + parent: agentPath, + intent: intent, + }; + + // Create the intent + const [response] = await intentsClient.createIntent(createIntentRequest); + console.log(`Intent ${response.name} created`); + } + + createIntent(); + + // [END dialogflow_create_intent] +} +main(...process.argv.slice(2)); diff --git a/dialogflow/list-intents.js b/dialogflow/list-intents.js new file mode 100644 index 0000000000..13482263dd --- /dev/null +++ b/dialogflow/list-intents.js @@ -0,0 +1,74 @@ +// Copyright 2020 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'; + +/** + * List of all intents in the specified project. + * @param {string} projectId The project to be used + */ +function main(projectId = 'YOUR_PROJECT_ID') { + // [START dialogflow_list_intents] + + /** + * TODO(developer): Uncomment the following lines before running the sample. + */ + // const projectId = 'The Project ID to use, e.g. 'YOUR_GCP_ID'; + + // Imports the Dialogflow library + const dialogflow = require('@google-cloud/dialogflow'); + + // Instantiates clients + const intentsClient = new dialogflow.IntentsClient(); + + async function listIntents() { + // Construct request + + // The path to identify the agent that owns the intents. + const projectAgentPath = intentsClient.agentPath(projectId); + + console.log(projectAgentPath); + + const request = { + parent: projectAgentPath, + }; + + // Send the request for listing intents. + const [response] = await intentsClient.listIntents(request); + response.forEach(intent => { + console.log('===================='); + console.log(`Intent name: ${intent.name}`); + console.log(`Intent display name: ${intent.displayName}`); + console.log(`Action: ${intent.action}`); + console.log(`Root folowup intent: ${intent.rootFollowupIntentName}`); + console.log(`Parent followup intent: ${intent.parentFollowupIntentName}`); + + console.log('Input contexts:'); + intent.inputContextNames.forEach(inputContextName => { + console.log(`\tName: ${inputContextName}`); + }); + + console.log('Output contexts:'); + intent.outputContexts.forEach(outputContext => { + console.log(`\tName: ${outputContext.name}`); + }); + }); + } + + listIntents(); + + // [END dialogflow_list_intents] +} + +main(...process.argv.slice(2)); diff --git a/dialogflow/resource.js b/dialogflow/resource.js index 8a47edd924..e18792bd8d 100644 --- a/dialogflow/resource.js +++ b/dialogflow/resource.js @@ -18,103 +18,6 @@ // Operations for intents // ///////////////////////////////////////////////////////////////////////////// -async function listIntents(projectId) { - // [START dialogflow_list_intents] - // Imports the Dialogflow library - const dialogflow = require('@google-cloud/dialogflow'); - - // Instantiates clients - const intentsClient = new dialogflow.IntentsClient(); - - // The path to identify the agent that owns the intents. - const projectAgentPath = intentsClient.agentPath(projectId); - - const request = { - parent: projectAgentPath, - }; - - console.log(projectAgentPath); - - // Send the request for listing intents. - const [response] = await intentsClient.listIntents(request); - response.forEach(intent => { - console.log('===================='); - console.log(`Intent name: ${intent.name}`); - console.log(`Intent display name: ${intent.displayName}`); - console.log(`Action: ${intent.action}`); - console.log(`Root folowup intent: ${intent.rootFollowupIntentName}`); - console.log(`Parent followup intent: ${intent.parentFollowupIntentName}`); - - console.log('Input contexts:'); - intent.inputContextNames.forEach(inputContextName => { - console.log(`\tName: ${inputContextName}`); - }); - - console.log('Output contexts:'); - intent.outputContexts.forEach(outputContext => { - console.log(`\tName: ${outputContext.name}`); - }); - }); - // [END dialogflow_list_intents] -} - -async function createIntent( - projectId, - displayName, - trainingPhrasesParts, - messageTexts -) { - // [START dialogflow_create_intent] - // Imports the Dialogflow library - const dialogflow = require('@google-cloud/dialogflow'); - - // Instantiates the Intent Client - const intentsClient = new dialogflow.IntentsClient(); - - // The path to identify the agent that owns the created intent. - const agentPath = intentsClient.agentPath(projectId); - - const trainingPhrases = []; - - trainingPhrasesParts.forEach(trainingPhrasesPart => { - const part = { - text: trainingPhrasesPart, - }; - - // Here we create a new training phrase for each provided part. - const trainingPhrase = { - type: 'EXAMPLE', - parts: [part], - }; - - trainingPhrases.push(trainingPhrase); - }); - - const messageText = { - text: messageTexts, - }; - - const message = { - text: messageText, - }; - - const intent = { - displayName: displayName, - trainingPhrases: trainingPhrases, - messages: [message], - }; - - const createIntentRequest = { - parent: agentPath, - intent: intent, - }; - - // Create the intent - const responses = await intentsClient.createIntent(createIntentRequest); - console.log(`Intent ${responses[0].name} created`); - // [END dialogflow_create_intent] -} - async function deleteIntent(projectId, intentId) { // [START dialogflow_delete_intent] // Imports the Dialogflow library @@ -264,45 +167,6 @@ const cli = require('yargs') .boolean('force') .alias('force', ['f']) .describe('force', 'force operation without a prompt') - .command( - 'create-intent', - 'Create Intent', - { - displayName: { - alias: 'd', - string: true, - demandOption: true, - requiresArg: true, - description: 'Display Name', - }, - trainingPhrasesParts: { - alias: 't', - array: true, - string: true, - demandOption: true, - requiresArg: true, - description: 'Training Phrases', - }, - messageTexts: { - alias: 'm', - array: true, - string: true, - demandOption: true, - requiresArg: true, - description: 'Message Texts', - }, - }, - opts => - createIntent( - opts.projectId, - opts.displayName, - opts.trainingPhrasesParts, - opts.messageTexts - ) - ) - .command('list-intents', 'List Intent', {}, opts => - listIntents(opts.projectId) - ) .command( 'delete-intent', 'Delete Intent', diff --git a/dialogflow/system-test/create-intent.test.js b/dialogflow/system-test/create-intent.test.js index 82e209fcde..61cb273e26 100644 --- a/dialogflow/system-test/create-intent.test.js +++ b/dialogflow/system-test/create-intent.test.js @@ -18,24 +18,20 @@ const {assert} = require('chai'); const {after, describe, it} = require('mocha'); const execSync = require('child_process').execSync; const uuid = require('uuid'); +const projectId = + process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT; const dialogflow = require('@google-cloud/dialogflow'); const exec = cmd => execSync(cmd, {encoding: 'utf8'}); describe('create intent', () => { const client = new dialogflow.IntentsClient(); - const cmd = 'node resource.js'; + const cmd = 'node create-intent.js'; const displayName = `fake_display_name_${uuid.v4().split('-')[0]}`; - const phrase1 = 'training_phrase_1'; - const phrase2 = 'training_phrase_2'; - const message1 = 'message_1'; - const message2 = 'message_2'; let intentId; it('should create an intent', async () => { - const output = exec( - `${cmd} create-intent -d ${displayName} -t ${phrase1} -t ${phrase2} -m ${message1} -m ${message2}` - ); + const output = exec(`${cmd} ${projectId} ${displayName}`); assert.include(output, 'intents'); intentId = output.split(' ')[1].split('/')[4]; });