Skip to content

Commit ff1a0ab

Browse files
galz10gcf-owl-bot[bot]
authored andcommitted
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>
1 parent 32d56f5 commit ff1a0ab

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 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 dialogflow.cx;
18+
19+
// [START dialogflow_cx_create_agent]
20+
21+
import com.google.cloud.dialogflow.cx.v3.Agent;
22+
import com.google.cloud.dialogflow.cx.v3.Agent.Builder;
23+
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
24+
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
25+
import java.io.IOException;
26+
27+
public class CreateAgent {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "my-project-id";
32+
String displayName = "my-display-name";
33+
34+
createAgent(projectId, displayName);
35+
}
36+
37+
public static Agent createAgent(String parent, String displayName) throws IOException {
38+
39+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
40+
41+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
42+
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
43+
// Set the details of the Agent to create
44+
Builder build = Agent.newBuilder();
45+
46+
build.setDefaultLanguageCode("en");
47+
build.setDisplayName(displayName);
48+
// Correct format for timezone is location/city
49+
// For example America/Los_Angeles, Europe/Madrid, Asia/Tokyo
50+
build.setTimeZone("America/Los_Angeles");
51+
52+
Agent agent = build.build();
53+
String parentPath = String.format("projects/%s/locations/%s", parent, "global");
54+
55+
// Calls the create agent api and returns the created Agent
56+
Agent response = client.createAgent(parentPath, agent);
57+
System.out.println(response);
58+
return response;
59+
}
60+
}
61+
}
62+
// [END dialogflow_cx_create_agent]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2021 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 dialogflow.cx;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
22+
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.io.PrintStream;
26+
import java.util.UUID;
27+
import org.junit.After;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
public class CreateAgentIT {
32+
33+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
34+
private static String agentPath = "";
35+
private ByteArrayOutputStream stdOut;
36+
private static PrintStream originalOut;
37+
38+
@Before
39+
public void setUp() throws IOException {
40+
originalOut = System.out;
41+
stdOut = new ByteArrayOutputStream();
42+
System.setOut(new PrintStream(stdOut));
43+
}
44+
45+
@After
46+
public void tearDown() throws IOException {
47+
System.setOut(originalOut);
48+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
49+
50+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
51+
AgentsClient client = AgentsClient.create(agentsSettings);
52+
53+
client.deleteAgent(CreateAgentIT.agentPath);
54+
}
55+
56+
@Test
57+
public void testCreateAgent() throws IOException {
58+
String fakeAgent = String.format("fake_agent_%s", UUID.randomUUID().toString());
59+
60+
CreateAgentIT.agentPath = CreateAgent.createAgent(PROJECT_ID, fakeAgent).getName();
61+
62+
assertThat(stdOut.toString()).contains(fakeAgent);
63+
}
64+
}

0 commit comments

Comments
 (0)