diff --git a/dialogflow/detect-intent-sentiment.v2.js b/dialogflow/detect-intent-sentiment.v2.js index e5355fcd9e..b59152f4e0 100644 --- a/dialogflow/detect-intent-sentiment.v2.js +++ b/dialogflow/detect-intent-sentiment.v2.js @@ -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 diff --git a/dialogflow/detect.js b/dialogflow/detect.js index 4a08381be3..79b597614d 100644 --- a/dialogflow/detect.js +++ b/dialogflow/detect.js @@ -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] @@ -113,6 +106,8 @@ async function detectEventIntent( eventName, languageCode ) { + const {struct} = require('pb-util'); + // Imports the Dialogflow library const dialogflow = require('dialogflow'); @@ -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( @@ -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'); @@ -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] } @@ -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'); @@ -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}`); + }); + } } }); @@ -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({ diff --git a/dialogflow/detect.v2beta1.js b/dialogflow/detect.v2beta1.js index f6a306a37d..78456f6ea9 100644 --- a/dialogflow/detect.v2beta1.js +++ b/dialogflow/detect.v2beta1.js @@ -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 diff --git a/dialogflow/system-test/detect.v2beta1.test.js b/dialogflow/system-test/detect.v2beta1.test.js index ed7b4b1fe0..9882832cba 100644 --- a/dialogflow/system-test/detect.v2beta1.test.js +++ b/dialogflow/system-test/detect.v2beta1.test.js @@ -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