Skip to content

Commit

Permalink
docs: update quickstart to reflect actual use case (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Nov 19, 2020
1 parent dff1d44 commit f8cf319
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
3 changes: 2 additions & 1 deletion dialogflow-cx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
83 changes: 67 additions & 16 deletions dialogflow-cx/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down
12 changes: 10 additions & 2 deletions dialogflow-cx/test/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
}
});
});

0 comments on commit f8cf319

Please sign in to comment.