Skip to content

Commit

Permalink
samples: add create agent code sample (#279)
Browse files Browse the repository at this point in the history
* 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
galz10 and gcf-owl-bot[bot] authored Aug 23, 2021
1 parent c8f3e2f commit c5c6e7c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
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]
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);
}
}

0 comments on commit c5c6e7c

Please sign in to comment.