Skip to content

Commit

Permalink
docs: move code outside of region tags (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnegrey authored and NimJay committed Nov 18, 2022
1 parent 7ad21d6 commit 0481f18
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 43 deletions.
2 changes: 1 addition & 1 deletion dialogflow/detect-intent-sentiment.v2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018, Google, LLC.
* Copyright 2018 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
Expand Down
129 changes: 89 additions & 40 deletions dialogflow/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@

'use strict';

const util = require('util');
const fs = require('fs');
const {struct} = require('pb-util');
const {Transform, pipeline} = require('stream');

const pump = util.promisify(pipeline);

function detectTextIntent(projectId, sessionId, queries, languageCode) {
// [START dialogflow_detect_intent_text]

Expand Down Expand Up @@ -113,6 +106,8 @@ async function detectEventIntent(
eventName,
languageCode
) {
const {struct} = require('pb-util');

// Imports the Dialogflow library
const dialogflow = require('dialogflow');

Expand All @@ -136,7 +131,31 @@ async function detectEventIntent(

const [response] = await sessionClient.detectIntent(request);
console.log('Detected intent');
logQueryResult(sessionClient, response.queryResult);
const result = response.queryResult;
// Instantiates a context client
const contextClient = new dialogflow.ContextsClient();

console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
const parameters = JSON.stringify(struct.decode(result.parameters));
console.log(` Parameters: ${parameters}`);
if (result.outputContexts && result.outputContexts.length) {
console.log(` Output contexts:`);
result.outputContexts.forEach(context => {
const contextId = contextClient.matchContextFromContextName(context.name);
const contextParameters = JSON.stringify(
struct.decode(context.parameters)
);
console.log(` ${contextId}`);
console.log(` lifespan: ${context.lifespanCount}`);
console.log(` parameters: ${contextParameters}`);
});
}
}

async function detectAudioIntent(
Expand All @@ -148,6 +167,9 @@ async function detectAudioIntent(
languageCode
) {
// [START dialogflow_detect_intent_audio]
const fs = require('fs');
const util = require('util');
const {struct} = require('pb-util');
// Imports the Dialogflow library
const dialogflow = require('dialogflow');

Expand Down Expand Up @@ -176,7 +198,31 @@ async function detectAudioIntent(
const [response] = await sessionClient.detectIntent(request);

console.log('Detected intent:');
logQueryResult(sessionClient, response.queryResult);
const result = response.queryResult;
// Instantiates a context client
const contextClient = new dialogflow.ContextsClient();

console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
const parameters = JSON.stringify(struct.decode(result.parameters));
console.log(` Parameters: ${parameters}`);
if (result.outputContexts && result.outputContexts.length) {
console.log(` Output contexts:`);
result.outputContexts.forEach(context => {
const contextId = contextClient.matchContextFromContextName(context.name);
const contextParameters = JSON.stringify(
struct.decode(context.parameters)
);
console.log(` ${contextId}`);
console.log(` lifespan: ${context.lifespanCount}`);
console.log(` parameters: ${contextParameters}`);
});
}
// [END dialogflow_detect_intent_audio]
}

Expand All @@ -189,6 +235,12 @@ async function streamingDetectIntent(
languageCode
) {
// [START dialogflow_detect_intent_streaming]
const fs = require('fs');
const util = require('util');
const {Transform, pipeline} = require('stream');
const {struct} = require('pb-util');

const pump = util.promisify(pipeline);
// Imports the Dialogflow library
const dialogflow = require('dialogflow');

Expand Down Expand Up @@ -231,7 +283,34 @@ async function streamingDetectIntent(
);
} else {
console.log(`Detected intent:`);
logQueryResult(sessionClient, data.queryResult);

const result = data.queryResult;
// Instantiates a context client
const contextClient = new dialogflow.ContextsClient();

console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
const parameters = JSON.stringify(struct.decode(result.parameters));
console.log(` Parameters: ${parameters}`);
if (result.outputContexts && result.outputContexts.length) {
console.log(` Output contexts:`);
result.outputContexts.forEach(context => {
const contextId = contextClient.matchContextFromContextName(
context.name
);
const contextParameters = JSON.stringify(
struct.decode(context.parameters)
);
console.log(` ${contextId}`);
console.log(` lifespan: ${context.lifespanCount}`);
console.log(` parameters: ${contextParameters}`);
});
}
}
});

Expand All @@ -254,36 +333,6 @@ async function streamingDetectIntent(
// [END dialogflow_detect_intent_streaming]
}

function logQueryResult(sessionClient, result) {
// Imports the Dialogflow library
const dialogflow = require('dialogflow');

// Instantiates a context client
const contextClient = new dialogflow.ContextsClient();

console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
} else {
console.log(` No intent matched.`);
}
const parameters = JSON.stringify(struct.decode(result.parameters));
console.log(` Parameters: ${parameters}`);
if (result.outputContexts && result.outputContexts.length) {
console.log(` Output contexts:`);
result.outputContexts.forEach(context => {
const contextId = contextClient.matchContextFromContextName(context.name);
const contextParameters = JSON.stringify(
struct.decode(context.parameters)
);
console.log(` ${contextId}`);
console.log(` lifespan: ${context.lifespanCount}`);
console.log(` parameters: ${contextParameters}`);
});
}
}

const cli = require(`yargs`)
.demand(1)
.options({
Expand Down
2 changes: 1 addition & 1 deletion dialogflow/detect.v2beta1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018, Google, LLC.
* Copyright 2018 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
Expand Down
2 changes: 1 addition & 1 deletion dialogflow/system-test/detect.v2beta1.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018, Google, LLC.
* Copyright 2018 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
Expand Down

0 comments on commit 0481f18

Please sign in to comment.