Skip to content

Commit

Permalink
chore: split samples into individual files (#622)
Browse files Browse the repository at this point in the history
* chore: split samples into individual files

* chore: removed cli option

* chore: update copyright

* fix: update to latest format

* fix: tests

* fix: broken tests
  • Loading branch information
munkhuushmgl authored Jun 10, 2020
1 parent a18943a commit d1dbeda
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 144 deletions.
99 changes: 99 additions & 0 deletions dialogflow/create-intent.js
Original file line number Diff line number Diff line change
@@ -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));
74 changes: 74 additions & 0 deletions dialogflow/list-intents.js
Original file line number Diff line number Diff line change
@@ -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));
136 changes: 0 additions & 136 deletions dialogflow/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
12 changes: 4 additions & 8 deletions dialogflow/system-test/create-intent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
Expand Down

0 comments on commit d1dbeda

Please sign in to comment.