Skip to content

Commit

Permalink
docs: update 'Quickstart: Agent interaction using the API' (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
felimartina authored and JustinBeckwith committed Sep 12, 2019
1 parent 748a396 commit 6e0734b
Showing 1 changed file with 59 additions and 44 deletions.
103 changes: 59 additions & 44 deletions dialogflow/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,39 @@ const pump = util.promisify(pipeline);

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

/**
* TODO(developer): UPDATE these variables before running the sample.
*/
// projectId: ID of the GCP project where Dialogflow agent is deployed
// const projectId = 'PROJECT_ID';
// sessionId: Random number or hashed user identifier
// const sessionId = 123456;
// queries: A set of sequential queries to be send to Dialogflow agent for Intent Detection
// const queries = [
// 'Reserve a meeting room in Toronto office, there will be 5 of us',
// 'Next monday at 3pm for 1 hour, please', // Tell the bot when the meeting is taking place
// 'B' // Rooms are defined on the Dialogflow agent, default options are A, B, or C
// ]
// languaceCode: Indicates the language Dialogflow agent should use to detect intents
// const languageCode = 'en';

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

// Instantiates a session client
const sessionClient = new dialogflow.SessionsClient();

if (!queries || !queries.length) {
return;
}
async function detectIntent(
projectId,
sessionId,
query,
contexts,
languageCode
) {
// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);

// The path to identify the agent that owns the created intent.
const sessionPath = sessionClient.sessionPath(projectId, sessionId);

let promise;

// Detects the intent of the queries.
for (const query of queries) {
// The text query request.
const request = {
session: sessionPath,
Expand All @@ -52,43 +68,42 @@ function detectTextIntent(projectId, sessionId, queries, languageCode) {
},
};

if (!promise) {
// First query.
console.log(`Sending query "${query}"`);
promise = sessionClient.detectIntent(request);
} else {
promise = promise.then(responses => {
console.log('Detected intent');
const response = responses[0];
logQueryResult(sessionClient, response.queryResult);

// Use output contexts as input contexts for the next query.
response.queryResult.outputContexts.forEach(context => {
// There is a bug in gRPC that the returned google.protobuf.Struct
// value contains fields with value of null, which causes error
// when encoding it back. Converting to JSON and back to proto
// removes those values.
context.parameters = struct.encode(struct.decode(context.parameters));
});
request.queryParams = {
contexts: response.queryResult.outputContexts,
};

console.log(`Sending query "${query}"`);
return sessionClient.detectIntent(request);
});
if (contexts && contexts.length > 0) {
request.queryParams = {
contexts: contexts,
};
}
}

promise
.then(responses => {
console.log('Detected intent');
logQueryResult(sessionClient, responses[0].queryResult);
})
.catch(err => {
console.error('ERROR:', err);
});
const responses = await sessionClient.detectIntent(request);
return responses[0];
}

async function executeQueries(projectId, sessionId, queries, languageCode) {
// Keeping the context across queries let's us simulate an ongoing conversation with the bot
let context;
let intentResponse;
for (const query of queries) {
try {
console.log(`Sending Query: ${query}`);
intentResponse = await detectIntent(
projectId,
sessionId,
query,
context,
languageCode
);
console.log('Detected intent');
console.log(
`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`
);
// Use the context from this response for next queries
context = intentResponse.queryResult.outputContexts;
} catch (error) {
console.log(error);
}
}
}
executeQueries(projectId, sessionId, queries, languageCode);
// [END dialogflow_detect_intent_text]
}

Expand Down

0 comments on commit 6e0734b

Please sign in to comment.