From a9069cd9e4220dbd7fbee89ff25804aab12c5272 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Thu, 19 Nov 2020 12:35:31 -0500 Subject: [PATCH] docs: update quickstart to reflect actual use case (#11) --- .github/CODEOWNERS | 9 +++++ README.md | 73 ++++++++++++++++++++++++++------- samples/package.json | 3 +- samples/quickstart.js | 83 ++++++++++++++++++++++++++++++-------- samples/test/quickstart.js | 12 +++++- synth.metadata | 2 +- 6 files changed, 147 insertions(+), 35 deletions(-) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 00000000..676131fe --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1,9 @@ +# Code owners file. +# This file controls who is tagged for review for any given pull request. +# +# For syntax help see: +# https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax + + +# The yoshi-nodejs team is the default owner for nodejs repositories. +* @googleapis/yoshi-nodejs @googleapis/ml-apis diff --git a/README.md b/README.md index 2ff40fb4..f8067919 100644 --- a/README.md +++ b/README.md @@ -62,24 +62,67 @@ npm install @google-cloud/dialogflow-cx * TODO(developer): Uncomment these variables before running the sample. */ // const projectId = 'my-project'; -// const location = 'us'; -// const agent = 'foo'; +// const location = 'global'; +// const agentId = 'my-agent'; +// const audioFileName = '/path/to/audio.raw'; +// const encoding = 'AUDIO_ENCODING_LINEAR_16'; +// const sampleRateHertz = 16000; +// const languageCode = 'en' // Imports the Google Cloud Some API library -const {IntentsClient} = require('@google-cloud/dialogflow-cx'); -const client = new IntentsClient(); -async function listIntents() { - const parent = client.agentPath(projectId, location, agent); - console.info(parent); - // TODO: implement a quickstart that does something useful: - /* - const [intents] = await client.listIntents({ - parent, - }); - console.info(intents); - */ +const {SessionsClient} = require('@google-cloud/dialogflow-cx'); +const client = new SessionsClient(); + +const fs = require('fs'); +const util = require('util'); +// Assumes uuid module has been installed from npm, +// npm i uuid: +const {v4} = require('uuid'); + +async function detectIntentAudio() { + const sessionId = v4(); + const sessionPath = client.projectLocationAgentSessionPath( + projectId, + location, + agentId, + sessionId + ); + + // Read the content of the audio file and send it as part of the request. + const readFile = util.promisify(fs.readFile); + const inputAudio = await readFile(audioFileName); + + const request = { + session: sessionPath, + queryInput: { + audio: { + config: { + audioEncoding: encoding, + sampleRateHertz: sampleRateHertz, + }, + audio: inputAudio, + }, + languageCode, + }, + }; + const [response] = await client.detectIntent(request); + console.log(`User Query: ${response.queryResult.transcript}`); + for (const message of response.queryResult.responseMessages) { + if (message.text) { + console.log(`Agent Response: ${message.text.text}`); + } + } + if (response.queryResult.match.intent) { + console.log( + `Matched Intent: ${response.queryResult.match.intent.displayName}` + ); + } + console.log( + `Current Page: ${response.queryResult.currentPage.displayName}` + ); } -listIntents(); + +detectIntentAudio(); ``` diff --git a/samples/package.json b/samples/package.json index ec3faf35..6b29093e 100644 --- a/samples/package.json +++ b/samples/package.json @@ -13,7 +13,8 @@ "test": "c8 mocha --timeout 600000 test/*.js" }, "dependencies": { - "@google-cloud/dialogflow-cx": "^1.2.0" + "@google-cloud/dialogflow-cx": "^1.2.0", + "uuid": "^8.3.1" }, "devDependencies": { "c8": "^7.3.0", diff --git a/samples/quickstart.js b/samples/quickstart.js index 166d7556..20fb0df9 100644 --- a/samples/quickstart.js +++ b/samples/quickstart.js @@ -14,30 +14,81 @@ 'use strict'; -async function main(projectId = 'my-project', location = 'us', agent = 'foo') { +async function main( + projectId, + location, + agentId, + audioFileName, + encoding, + sampleRateHertz, + languageCode +) { // [START dialogflow_cx_quickstart] /** * TODO(developer): Uncomment these variables before running the sample. */ // const projectId = 'my-project'; - // const location = 'us'; - // const agent = 'foo'; + // const location = 'global'; + // const agentId = 'my-agent'; + // const audioFileName = '/path/to/audio.raw'; + // const encoding = 'AUDIO_ENCODING_LINEAR_16'; + // const sampleRateHertz = 16000; + // const languageCode = 'en' // Imports the Google Cloud Some API library - const {IntentsClient} = require('@google-cloud/dialogflow-cx'); - const client = new IntentsClient(); - async function listIntents() { - const parent = client.agentPath(projectId, location, agent); - console.info(parent); - // TODO: implement a quickstart that does something useful: - /* - const [intents] = await client.listIntents({ - parent, - }); - console.info(intents); - */ + const {SessionsClient} = require('@google-cloud/dialogflow-cx'); + const client = new SessionsClient(); + + const fs = require('fs'); + const util = require('util'); + // Assumes uuid module has been installed from npm, + // npm i uuid: + const {v4} = require('uuid'); + + async function detectIntentAudio() { + const sessionId = v4(); + const sessionPath = client.projectLocationAgentSessionPath( + projectId, + location, + agentId, + sessionId + ); + + // Read the content of the audio file and send it as part of the request. + const readFile = util.promisify(fs.readFile); + const inputAudio = await readFile(audioFileName); + + const request = { + session: sessionPath, + queryInput: { + audio: { + config: { + audioEncoding: encoding, + sampleRateHertz: sampleRateHertz, + }, + audio: inputAudio, + }, + languageCode, + }, + }; + const [response] = await client.detectIntent(request); + console.log(`User Query: ${response.queryResult.transcript}`); + for (const message of response.queryResult.responseMessages) { + if (message.text) { + console.log(`Agent Response: ${message.text.text}`); + } + } + if (response.queryResult.match.intent) { + console.log( + `Matched Intent: ${response.queryResult.match.intent.displayName}` + ); + } + console.log( + `Current Page: ${response.queryResult.currentPage.displayName}` + ); } - listIntents(); + + detectIntentAudio(); // [END dialogflow_cx_quickstart] } diff --git a/samples/test/quickstart.js b/samples/test/quickstart.js index 62c44f8c..eca23119 100644 --- a/samples/test/quickstart.js +++ b/samples/test/quickstart.js @@ -30,7 +30,15 @@ const project = process.env.GCLOUD_PROJECT; describe('Quickstart', () => { it('should run quickstart', async () => { - const stdout = execSync(`node ./quickstart.js ${project}`, {cwd}); - assert.match(stdout, /projects/); + try { + // TODO: write an actual functional test: + execSync( + `node ./quickstart.js ${project} global my-agent audio.raw AUDIO_ENCODING_LINEAR_16 16000 en`, + {cwd} + ); + assert('unreachable'); + } catch (err) { + assert.match(err.message, /no such file or directory, open 'audio.raw/); + } }); }); diff --git a/synth.metadata b/synth.metadata index 72a9e53f..aa14d72b 100644 --- a/synth.metadata +++ b/synth.metadata @@ -158,4 +158,4 @@ "tsconfig.json", "webpack.config.js" ] -} \ No newline at end of file +}