Skip to content

Commit 6e0734b

Browse files
felimartinaJustinBeckwith
authored andcommitted
docs: update 'Quickstart: Agent interaction using the API' (#404)
1 parent 748a396 commit 6e0734b

File tree

1 file changed

+59
-44
lines changed

1 file changed

+59
-44
lines changed

dialogflow/detect.js

+59-44
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,39 @@ const pump = util.promisify(pipeline);
2424

2525
function detectTextIntent(projectId, sessionId, queries, languageCode) {
2626
// [START dialogflow_detect_intent_text]
27+
28+
/**
29+
* TODO(developer): UPDATE these variables before running the sample.
30+
*/
31+
// projectId: ID of the GCP project where Dialogflow agent is deployed
32+
// const projectId = 'PROJECT_ID';
33+
// sessionId: Random number or hashed user identifier
34+
// const sessionId = 123456;
35+
// queries: A set of sequential queries to be send to Dialogflow agent for Intent Detection
36+
// const queries = [
37+
// 'Reserve a meeting room in Toronto office, there will be 5 of us',
38+
// 'Next monday at 3pm for 1 hour, please', // Tell the bot when the meeting is taking place
39+
// 'B' // Rooms are defined on the Dialogflow agent, default options are A, B, or C
40+
// ]
41+
// languaceCode: Indicates the language Dialogflow agent should use to detect intents
42+
// const languageCode = 'en';
43+
2744
// Imports the Dialogflow library
2845
const dialogflow = require('dialogflow');
2946

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

33-
if (!queries || !queries.length) {
34-
return;
35-
}
50+
async function detectIntent(
51+
projectId,
52+
sessionId,
53+
query,
54+
contexts,
55+
languageCode
56+
) {
57+
// The path to identify the agent that owns the created intent.
58+
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
3659

37-
// The path to identify the agent that owns the created intent.
38-
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
39-
40-
let promise;
41-
42-
// Detects the intent of the queries.
43-
for (const query of queries) {
4460
// The text query request.
4561
const request = {
4662
session: sessionPath,
@@ -52,43 +68,42 @@ function detectTextIntent(projectId, sessionId, queries, languageCode) {
5268
},
5369
};
5470

55-
if (!promise) {
56-
// First query.
57-
console.log(`Sending query "${query}"`);
58-
promise = sessionClient.detectIntent(request);
59-
} else {
60-
promise = promise.then(responses => {
61-
console.log('Detected intent');
62-
const response = responses[0];
63-
logQueryResult(sessionClient, response.queryResult);
64-
65-
// Use output contexts as input contexts for the next query.
66-
response.queryResult.outputContexts.forEach(context => {
67-
// There is a bug in gRPC that the returned google.protobuf.Struct
68-
// value contains fields with value of null, which causes error
69-
// when encoding it back. Converting to JSON and back to proto
70-
// removes those values.
71-
context.parameters = struct.encode(struct.decode(context.parameters));
72-
});
73-
request.queryParams = {
74-
contexts: response.queryResult.outputContexts,
75-
};
76-
77-
console.log(`Sending query "${query}"`);
78-
return sessionClient.detectIntent(request);
79-
});
71+
if (contexts && contexts.length > 0) {
72+
request.queryParams = {
73+
contexts: contexts,
74+
};
8075
}
81-
}
8276

83-
promise
84-
.then(responses => {
85-
console.log('Detected intent');
86-
logQueryResult(sessionClient, responses[0].queryResult);
87-
})
88-
.catch(err => {
89-
console.error('ERROR:', err);
90-
});
77+
const responses = await sessionClient.detectIntent(request);
78+
return responses[0];
79+
}
9180

81+
async function executeQueries(projectId, sessionId, queries, languageCode) {
82+
// Keeping the context across queries let's us simulate an ongoing conversation with the bot
83+
let context;
84+
let intentResponse;
85+
for (const query of queries) {
86+
try {
87+
console.log(`Sending Query: ${query}`);
88+
intentResponse = await detectIntent(
89+
projectId,
90+
sessionId,
91+
query,
92+
context,
93+
languageCode
94+
);
95+
console.log('Detected intent');
96+
console.log(
97+
`Fulfillment Text: ${intentResponse.queryResult.fulfillmentText}`
98+
);
99+
// Use the context from this response for next queries
100+
context = intentResponse.queryResult.outputContexts;
101+
} catch (error) {
102+
console.log(error);
103+
}
104+
}
105+
}
106+
executeQueries(projectId, sessionId, queries, languageCode);
92107
// [END dialogflow_detect_intent_text]
93108
}
94109

0 commit comments

Comments
 (0)