From efb992b0201a7120746e12d1356dfa2911459d7f Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Wed, 8 Sep 2021 13:10:07 -0700 Subject: [PATCH] samples: set agent code sample (#660) * samples: set agent code sample * Lint fix * Update samples/snippets/src/test/java/com/example/dialogflow/SetAgentIT.java Co-authored-by: Jeff Ching * Update samples/snippets/src/main/java/com/example/dialogflow/SetAgent.java Co-authored-by: Jeff Ching * Update samples/snippets/src/main/java/com/example/dialogflow/SetAgent.java Co-authored-by: Jeff Ching * Update samples/snippets/src/main/java/com/example/dialogflow/SetAgent.java Co-authored-by: Jeff Ching * updated tests * Test and lint fix * Lint fix * Changed package name * revised code Co-authored-by: Jeff Ching --- .../src/main/dialogflow/SetAgent.java | 57 +++++++++++++++++++ .../src/test/dialogflow/SetAgentIT.java | 33 +++++++++++ 2 files changed, 90 insertions(+) create mode 100644 dialogflow/snippets/src/main/dialogflow/SetAgent.java create mode 100644 dialogflow/snippets/src/test/dialogflow/SetAgentIT.java diff --git a/dialogflow/snippets/src/main/dialogflow/SetAgent.java b/dialogflow/snippets/src/main/dialogflow/SetAgent.java new file mode 100644 index 00000000000..cfa73edf26d --- /dev/null +++ b/dialogflow/snippets/src/main/dialogflow/SetAgent.java @@ -0,0 +1,57 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow; + +// [START dialogflow_es_create_agent] + +import com.google.cloud.dialogflow.v2.Agent; +import com.google.cloud.dialogflow.v2.Agent.Builder; +import com.google.cloud.dialogflow.v2.AgentsClient; +import com.google.cloud.dialogflow.v2.AgentsSettings; +import java.io.IOException; + +public class SetAgent { + + public static void main(String[] args) throws IOException { + String projectId = "my-project-id"; + + // The display name will set the name of your agent + String displayName = "my-display-name"; + + setAgent(projectId, displayName); + } + + public static Agent setAgent(String parent, String displayName) throws IOException { + + AgentsSettings agentsSettings = AgentsSettings.newBuilder().build(); + try (AgentsClient client = AgentsClient.create(agentsSettings)) { + // Set the details of the Agent to create + Builder build = Agent.newBuilder(); + + build.setDefaultLanguageCode("en"); + build.setDisplayName(displayName); + + Agent agent = build.build(); + + // Make API request to create agent + Agent response = client.setAgent(agent); + System.out.println(response); + return response; + } + } +} +// [END dialogflow_es_create_agent] diff --git a/dialogflow/snippets/src/test/dialogflow/SetAgentIT.java b/dialogflow/snippets/src/test/dialogflow/SetAgentIT.java new file mode 100644 index 00000000000..146ce24c7bd --- /dev/null +++ b/dialogflow/snippets/src/test/dialogflow/SetAgentIT.java @@ -0,0 +1,33 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dialogflow; + +import org.junit.Assert; +import org.junit.Test; + +public class SetAgentIT { + + + /* + * We cannot test setAgent because Dialogflow ES can only have one agent + * and if we create a agent it will delete the exisitng testing agent and + * would cause all tests to fail + */ + @Test + public void testCreateAgent() { + Assert.assertTrue(true); +}