diff --git a/dialogflow-cx/package.json b/dialogflow-cx/package.json index ec3faf35942..6b29093efea 100644 --- a/dialogflow-cx/package.json +++ b/dialogflow-cx/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/dialogflow-cx/quickstart.js b/dialogflow-cx/quickstart.js index 166d7556732..20fb0df9733 100644 --- a/dialogflow-cx/quickstart.js +++ b/dialogflow-cx/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/dialogflow-cx/test/quickstart.js b/dialogflow-cx/test/quickstart.js index 62c44f8c5e0..eca23119728 100644 --- a/dialogflow-cx/test/quickstart.js +++ b/dialogflow-cx/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/); + } }); });