@@ -24,23 +24,39 @@ const pump = util.promisify(pipeline);
24
24
25
25
function detectTextIntent ( projectId , sessionId , queries , languageCode ) {
26
26
// [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
+
27
44
// Imports the Dialogflow library
28
45
const dialogflow = require ( 'dialogflow' ) ;
29
46
30
47
// Instantiates a session client
31
48
const sessionClient = new dialogflow . SessionsClient ( ) ;
32
49
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 ) ;
36
59
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 ) {
44
60
// The text query request.
45
61
const request = {
46
62
session : sessionPath ,
@@ -52,43 +68,42 @@ function detectTextIntent(projectId, sessionId, queries, languageCode) {
52
68
} ,
53
69
} ;
54
70
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
+ } ;
80
75
}
81
- }
82
76
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
+ }
91
80
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 ) ;
92
107
// [END dialogflow_detect_intent_text]
93
108
}
94
109
0 commit comments