Skip to content

Commit efb992b

Browse files
galz10chingor13
authored andcommitted
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 <[email protected]> * Update samples/snippets/src/main/java/com/example/dialogflow/SetAgent.java Co-authored-by: Jeff Ching <[email protected]> * Update samples/snippets/src/main/java/com/example/dialogflow/SetAgent.java Co-authored-by: Jeff Ching <[email protected]> * Update samples/snippets/src/main/java/com/example/dialogflow/SetAgent.java Co-authored-by: Jeff Ching <[email protected]> * updated tests * Test and lint fix * Lint fix * Changed package name * revised code Co-authored-by: Jeff Ching <[email protected]>
1 parent f55d4bc commit efb992b

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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;
18+
19+
// [START dialogflow_es_create_agent]
20+
21+
import com.google.cloud.dialogflow.v2.Agent;
22+
import com.google.cloud.dialogflow.v2.Agent.Builder;
23+
import com.google.cloud.dialogflow.v2.AgentsClient;
24+
import com.google.cloud.dialogflow.v2.AgentsSettings;
25+
import java.io.IOException;
26+
27+
public class SetAgent {
28+
29+
public static void main(String[] args) throws IOException {
30+
String projectId = "my-project-id";
31+
32+
// The display name will set the name of your agent
33+
String displayName = "my-display-name";
34+
35+
setAgent(projectId, displayName);
36+
}
37+
38+
public static Agent setAgent(String parent, String displayName) throws IOException {
39+
40+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().build();
41+
try (AgentsClient client = AgentsClient.create(agentsSettings)) {
42+
// Set the details of the Agent to create
43+
Builder build = Agent.newBuilder();
44+
45+
build.setDefaultLanguageCode("en");
46+
build.setDisplayName(displayName);
47+
48+
Agent agent = build.build();
49+
50+
// Make API request to create agent
51+
Agent response = client.setAgent(agent);
52+
System.out.println(response);
53+
return response;
54+
}
55+
}
56+
}
57+
// [END dialogflow_es_create_agent]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
public class SetAgentIT {
23+
24+
25+
/*
26+
* We cannot test setAgent because Dialogflow ES can only have one agent
27+
* and if we create a agent it will delete the exisitng testing agent and
28+
* would cause all tests to fail
29+
*/
30+
@Test
31+
public void testCreateAgent() {
32+
Assert.assertTrue(true);
33+
}

0 commit comments

Comments
 (0)