Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

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 committed Nov 19, 2020
1 parent 558403f commit a9069cd
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 35 deletions.
9 changes: 9 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -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
73 changes: 58 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();

```

Expand Down
3 changes: 2 additions & 1 deletion samples/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 samples/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 samples/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/);
}
});
});
2 changes: 1 addition & 1 deletion synth.metadata
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@
"tsconfig.json",
"webpack.config.js"
]
}
}

0 comments on commit a9069cd

Please sign in to comment.