17
17
package com .example .dialogflow ;
18
18
19
19
// Imports the Google Cloud client library
20
+
20
21
import com .google .cloud .dialogflow .v2 .Context ;
21
22
import com .google .cloud .dialogflow .v2 .ContextName ;
22
23
import com .google .cloud .dialogflow .v2 .ContextsClient ;
23
24
import com .google .cloud .dialogflow .v2 .SessionName ;
25
+ import com .google .common .collect .Lists ;
24
26
import com .google .protobuf .Value ;
25
27
28
+ import java .util .List ;
26
29
import java .util .Map .Entry ;
27
- import net .sourceforge .argparse4j .ArgumentParsers ;
28
- import net .sourceforge .argparse4j .inf .ArgumentParser ;
29
- import net .sourceforge .argparse4j .inf .ArgumentParserException ;
30
- import net .sourceforge .argparse4j .inf .MutuallyExclusiveGroup ;
31
- import net .sourceforge .argparse4j .inf .Namespace ;
32
- import net .sourceforge .argparse4j .inf .Subparser ;
33
- import net .sourceforge .argparse4j .inf .Subparsers ;
34
-
35
30
36
31
/**
37
32
* DialogFlow API Context sample.
38
33
*/
39
34
public class ContextManagement {
40
-
41
35
// [START dialogflow_list_contexts]
36
+
42
37
/**
43
38
* Lists contexts
39
+ *
44
40
* @param sessionId Identifier of the DetectIntent session.
45
41
* @param projectId Project/Agent Id.
42
+ * @return List of Contexts found.
46
43
*/
47
- public static void listContexts (String sessionId , String projectId ) throws Exception {
44
+ public static List <Context > listContexts (String sessionId , String projectId ) throws Exception {
45
+ List <Context > contexts = Lists .newArrayList ();
48
46
// Instantiates a client
49
47
try (ContextsClient contextsClient = ContextsClient .create ()) {
50
48
// Set the session name using the sessionId (UUID) and projectId (my-project-id)
51
49
SessionName session = SessionName .of (projectId , sessionId );
52
50
53
51
// Performs the list contexts request
54
- System .out .format ("Contexts for session %s:\n " , session .toString ());
55
52
for (Context context : contextsClient .listContexts (session ).iterateAll ()) {
56
53
System .out .format ("Context name: %s\n " , context .getName ());
57
54
System .out .format ("Lifespan Count: %d\n " , context .getLifespanCount ());
@@ -61,20 +58,29 @@ public static void listContexts(String sessionId, String projectId) throws Excep
61
58
System .out .format ("\t %s: %s\n " , entry .getKey (), entry .getValue ());
62
59
}
63
60
}
61
+
62
+ contexts .add (context );
64
63
}
65
64
}
65
+ return contexts ;
66
66
}
67
67
// [END dialogflow_list_contexts]
68
68
69
69
// [START dialogflow_create_context]
70
+
70
71
/**
71
72
* Create an entity type with the given display name
72
- * @param contextId The Id of the context.
73
- * @param sessionId Identifier of the DetectIntent session.
73
+ *
74
+ * @param contextId The Id of the context.
75
+ * @param sessionId Identifier of the DetectIntent session.
74
76
* @param lifespanCount The lifespan count of the context.
75
- * @param projectId Project/Agent Id.
77
+ * @param projectId Project/Agent Id.
78
+ * @return The new Context.
76
79
*/
77
- public static void createContext (String contextId , String sessionId , String projectId ,
80
+ public static Context createContext (
81
+ String contextId ,
82
+ String sessionId ,
83
+ String projectId ,
78
84
int lifespanCount ) throws Exception {
79
85
// Instantiates a client
80
86
try (ContextsClient contextsClient = ContextsClient .create ()) {
@@ -97,13 +103,17 @@ public static void createContext(String contextId, String sessionId, String proj
97
103
// Performs the create context request
98
104
Context response = contextsClient .createContext (session , context );
99
105
System .out .format ("Context created: %s\n " , response );
106
+
107
+ return response ;
100
108
}
101
109
}
102
110
// [END dialogflow_create_context]
103
111
104
112
// [START dialogflow_delete_context]
113
+
105
114
/**
106
115
* Delete entity type with the given entity type name
116
+ *
107
117
* @param contextId The Id of the context.
108
118
* @param sessionId Identifier of the DetectIntent session.
109
119
* @param projectId Project/Agent Id.
@@ -119,59 +129,4 @@ public static void deleteContext(String contextId, String sessionId, String proj
119
129
}
120
130
}
121
131
// [END dialogflow_delete_context]
122
-
123
- public static void main (String [] args ) throws Exception {
124
- ArgumentParser parser =
125
- ArgumentParsers .newFor ("ContextManagement" )
126
- .build ()
127
- .defaultHelp (true )
128
- .description ("Create / List / Delete a context." );
129
-
130
- Subparsers subparsers = parser .addSubparsers ().dest ("command" ).title ("Commands" );
131
-
132
- Subparser listParser = subparsers .addParser ("list" )
133
- .help ("mvn exec:java -DContextManagement -Dexec.args='list --sessionId SESSION_ID "
134
- + "--projectId PROJECT_ID'" );
135
- listParser .addArgument ("--sessionId" )
136
- .help ("Identifier of the DetectIntent session" ).required (true );
137
- listParser .addArgument ("--projectId" ).help ("Project/Agent Id" ).required (true );
138
-
139
- Subparser createParser = subparsers .addParser ("create" )
140
- .help ("mvn exec:java -DContextManagement -Dexec.args='create --sessionId SESSION_ID "
141
- + "--projectId PROJECT_ID --contextId CONTEXT_ID'" );
142
- createParser .addArgument ("--sessionId" )
143
- .help ("Identifier of the DetectIntent session" ).required (true );
144
- createParser .addArgument ("--projectId" ).help ("Project/Agent Id" ).required (true );
145
- createParser .addArgument ("--contextId" )
146
- .help ("The Id of the context" )
147
- .required (true );
148
- createParser .addArgument ("--lifespanCount" )
149
- .help ("The lifespan count of the context (Default: 1)" ).setDefault (1 );
150
-
151
- Subparser deleteParser = subparsers .addParser ("delete" )
152
- .help ("mvn exec:java -DContextManagement -Dexec.args='delete --sessionId SESSION_ID "
153
- + "--projectId PROJECT_ID --contextId CONTEXT_ID'" );
154
- deleteParser .addArgument ("--sessionId" )
155
- .help ("Identifier of the DetectIntent session" ).required (true );
156
- deleteParser .addArgument ("--projectId" ).help ("Project/Agent Id" ).required (true );
157
- deleteParser .addArgument ("--contextId" )
158
- .help ("The Id of the context" )
159
- .required (true );
160
-
161
- try {
162
- Namespace namespace = parser .parseArgs (args );
163
-
164
- if (namespace .get ("command" ).equals ("list" )) {
165
- listContexts (namespace .get ("sessionId" ), namespace .get ("projectId" ));
166
- } else if (namespace .get ("command" ).equals ("create" )) {
167
- createContext (namespace .get ("contextId" ), namespace .get ("sessionId" ),
168
- namespace .get ("projectId" ), namespace .get ("lifespanCount" ));
169
- } else if (namespace .get ("command" ).equals ("delete" )) {
170
- deleteContext (namespace .get ("contextId" ), namespace .get ("sessionId" ),
171
- namespace .get ("projectId" ));
172
- }
173
- } catch (ArgumentParserException e ) {
174
- parser .handleError (e );
175
- }
176
- }
177
132
}
0 commit comments