|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.dialogflow; |
| 18 | + |
| 19 | +// Imports the Google Cloud client library |
| 20 | +import com.google.cloud.dialogflow.v2.Context; |
| 21 | +import com.google.cloud.dialogflow.v2.ContextName; |
| 22 | +import com.google.cloud.dialogflow.v2.ContextsClient; |
| 23 | +import com.google.cloud.dialogflow.v2.SessionName; |
| 24 | +import com.google.protobuf.Value; |
| 25 | + |
| 26 | +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 | + |
| 36 | +/** |
| 37 | + * DialogFlow API Context sample. |
| 38 | + */ |
| 39 | +public class ContextManagement { |
| 40 | + |
| 41 | + // [START dialogflow_list_contexts] |
| 42 | + /** |
| 43 | + * Lists contexts |
| 44 | + * @param sessionId Identifier of the DetectIntent session. |
| 45 | + * @param projectId Project/Agent Id. |
| 46 | + */ |
| 47 | + public static void listContexts(String sessionId, String projectId) throws Exception { |
| 48 | + // Instantiates a client |
| 49 | + try (ContextsClient contextsClient = ContextsClient.create()) { |
| 50 | + // Set the session name using the sessionId (UUID) and projectId (my-project-id) |
| 51 | + SessionName session = SessionName.of(projectId, sessionId); |
| 52 | + |
| 53 | + // Performs the list contexts request |
| 54 | + System.out.format("Contexts for session %s:\n", session.toString()); |
| 55 | + for (Context context : contextsClient.listContexts(session).iterateAll()) { |
| 56 | + System.out.format("Context name: %s\n", context.getName()); |
| 57 | + System.out.format("Lifespan Count: %d\n", context.getLifespanCount()); |
| 58 | + System.out.format("Fields:\n"); |
| 59 | + for (Entry<String, Value> entry : context.getParameters().getFieldsMap().entrySet()) { |
| 60 | + if (entry.getValue().hasStructValue()) { |
| 61 | + System.out.format("\t%s: %s\n", entry.getKey(), entry.getValue()); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + // [END dialogflow_list_contexts] |
| 68 | + |
| 69 | + // [START dialogflow_create_context] |
| 70 | + /** |
| 71 | + * 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. |
| 74 | + * @param lifespanCount The lifespan count of the context. |
| 75 | + * @param projectId Project/Agent Id. |
| 76 | + */ |
| 77 | + public static void createContext(String contextId, String sessionId, String projectId, |
| 78 | + int lifespanCount) throws Exception { |
| 79 | + // Instantiates a client |
| 80 | + try (ContextsClient contextsClient = ContextsClient.create()) { |
| 81 | + // Set the session name using the sessionId (UUID) and projectID (my-project-id) |
| 82 | + SessionName session = SessionName.of(projectId, sessionId); |
| 83 | + |
| 84 | + // Create the context name with the projectId, sessionId, and contextId |
| 85 | + ContextName contextName = ContextName.newBuilder() |
| 86 | + .setProject(projectId) |
| 87 | + .setSession(sessionId) |
| 88 | + .setContext(contextId) |
| 89 | + .build(); |
| 90 | + |
| 91 | + // Create the context with the context name and lifespan count |
| 92 | + Context context = Context.newBuilder() |
| 93 | + .setName(contextName.toString()) // The unique identifier of the context |
| 94 | + .setLifespanCount(lifespanCount) // Number of query requests before the context expires. |
| 95 | + .build(); |
| 96 | + |
| 97 | + // Performs the create context request |
| 98 | + Context response = contextsClient.createContext(session, context); |
| 99 | + System.out.format("Context created: %s\n", response); |
| 100 | + } |
| 101 | + } |
| 102 | + // [END dialogflow_create_context] |
| 103 | + |
| 104 | + // [START dialogflow_delete_context] |
| 105 | + /** |
| 106 | + * Delete entity type with the given entity type name |
| 107 | + * @param contextId The Id of the context. |
| 108 | + * @param sessionId Identifier of the DetectIntent session. |
| 109 | + * @param projectId Project/Agent Id. |
| 110 | + */ |
| 111 | + public static void deleteContext(String contextId, String sessionId, String projectId) |
| 112 | + throws Exception { |
| 113 | + // Instantiates a client |
| 114 | + try (ContextsClient contextsClient = ContextsClient.create()) { |
| 115 | + // Create the context name with the projectId, sessionId, and contextId |
| 116 | + ContextName contextName = ContextName.of(projectId, sessionId, contextId); |
| 117 | + // Performs the delete context request |
| 118 | + contextsClient.deleteContext(contextName); |
| 119 | + } |
| 120 | + } |
| 121 | + // [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 | +} |
0 commit comments