-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add create agent code sample (#279)
* samples: add create agent code sample * Lint fix * Fix failing test * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Rebased * Revised Code per comments * lint fix * Added Comment for timezone * Fixed lint * Used String.format * Added stdOut Var * removed parent variable * Update Tests * lint fix * removed parentPath * removed unreachable statemt * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Revised Code Per Comments * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
c8f3e2f
commit c5c6e7c
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateAgent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.cx; | ||
|
||
// [START dialogflow_cx_create_agent] | ||
|
||
import com.google.cloud.dialogflow.cx.v3.Agent; | ||
import com.google.cloud.dialogflow.cx.v3.Agent.Builder; | ||
import com.google.cloud.dialogflow.cx.v3.AgentsClient; | ||
import com.google.cloud.dialogflow.cx.v3.AgentsSettings; | ||
import java.io.IOException; | ||
|
||
public class CreateAgent { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String displayName = "my-display-name"; | ||
|
||
createAgent(projectId, displayName); | ||
} | ||
|
||
public static Agent createAgent(String parent, String displayName) throws IOException { | ||
|
||
String apiEndpoint = "global-dialogflow.googleapis.com:443"; | ||
|
||
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).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); | ||
// Correct format for timezone is location/city | ||
// For example America/Los_Angeles, Europe/Madrid, Asia/Tokyo | ||
build.setTimeZone("America/Los_Angeles"); | ||
|
||
Agent agent = build.build(); | ||
String parentPath = String.format("projects/%s/locations/%s", parent, "global"); | ||
|
||
// Calls the create agent api and returns the created Agent | ||
Agent response = client.createAgent(parentPath, agent); | ||
System.out.println(response); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END dialogflow_cx_create_agent] |
64 changes: 64 additions & 0 deletions
64
dialogflow-cx/snippets/src/test/java/dialogflow/cx/CreateAgentIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* 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.cx; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.dialogflow.cx.v3.AgentsClient; | ||
import com.google.cloud.dialogflow.cx.v3.AgentsSettings; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.UUID; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class CreateAgentIT { | ||
|
||
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); | ||
private static String agentPath = ""; | ||
private ByteArrayOutputStream stdOut; | ||
private static PrintStream originalOut; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
originalOut = System.out; | ||
stdOut = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(stdOut)); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
System.setOut(originalOut); | ||
String apiEndpoint = "global-dialogflow.googleapis.com:443"; | ||
|
||
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
AgentsClient client = AgentsClient.create(agentsSettings); | ||
|
||
client.deleteAgent(CreateAgentIT.agentPath); | ||
} | ||
|
||
@Test | ||
public void testCreateAgent() throws IOException { | ||
String fakeAgent = String.format("fake_agent_%s", UUID.randomUUID().toString()); | ||
|
||
CreateAgentIT.agentPath = CreateAgent.createAgent(PROJECT_ID, fakeAgent).getName(); | ||
|
||
assertThat(stdOut.toString()).contains(fakeAgent); | ||
} | ||
} |