|
| 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] |
0 commit comments