1717package com .example .dialogflow ;
1818
1919// Imports the Google Cloud client library
20+
2021import com .google .cloud .dialogflow .v2 .Context ;
2122import com .google .cloud .dialogflow .v2 .ContextName ;
2223import com .google .cloud .dialogflow .v2 .ContextsClient ;
2324import com .google .cloud .dialogflow .v2 .SessionName ;
25+ import com .google .common .collect .Lists ;
2426import com .google .protobuf .Value ;
2527
28+ import java .util .List ;
2629import 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-
3530
3631/**
3732 * DialogFlow API Context sample.
3833 */
3934public class ContextManagement {
40-
4135 // [START dialogflow_list_contexts]
36+
4237 /**
4338 * Lists contexts
39+ *
4440 * @param sessionId Identifier of the DetectIntent session.
4541 * @param projectId Project/Agent Id.
42+ * @return List of Contexts found.
4643 */
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 ();
4846 // Instantiates a client
4947 try (ContextsClient contextsClient = ContextsClient .create ()) {
5048 // Set the session name using the sessionId (UUID) and projectId (my-project-id)
5149 SessionName session = SessionName .of (projectId , sessionId );
5250
5351 // Performs the list contexts request
54- System .out .format ("Contexts for session %s:\n " , session .toString ());
5552 for (Context context : contextsClient .listContexts (session ).iterateAll ()) {
5653 System .out .format ("Context name: %s\n " , context .getName ());
5754 System .out .format ("Lifespan Count: %d\n " , context .getLifespanCount ());
@@ -61,20 +58,29 @@ public static void listContexts(String sessionId, String projectId) throws Excep
6158 System .out .format ("\t %s: %s\n " , entry .getKey (), entry .getValue ());
6259 }
6360 }
61+
62+ contexts .add (context );
6463 }
6564 }
65+ return contexts ;
6666 }
6767 // [END dialogflow_list_contexts]
6868
6969 // [START dialogflow_create_context]
70+
7071 /**
7172 * 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.
7476 * @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.
7679 */
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 ,
7884 int lifespanCount ) throws Exception {
7985 // Instantiates a client
8086 try (ContextsClient contextsClient = ContextsClient .create ()) {
@@ -97,13 +103,17 @@ public static void createContext(String contextId, String sessionId, String proj
97103 // Performs the create context request
98104 Context response = contextsClient .createContext (session , context );
99105 System .out .format ("Context created: %s\n " , response );
106+
107+ return response ;
100108 }
101109 }
102110 // [END dialogflow_create_context]
103111
104112 // [START dialogflow_delete_context]
113+
105114 /**
106115 * Delete entity type with the given entity type name
116+ *
107117 * @param contextId The Id of the context.
108118 * @param sessionId Identifier of the DetectIntent session.
109119 * @param projectId Project/Agent Id.
@@ -119,59 +129,4 @@ public static void deleteContext(String contextId, String sessionId, String proj
119129 }
120130 }
121131 // [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- }
177132}
0 commit comments