Skip to content

Commit

Permalink
samples: set agent code sample (#660)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
galz10 and chingor13 authored Sep 8, 2021
1 parent dbcd399 commit 98a4fa1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
57 changes: 57 additions & 0 deletions dialogflow/snippets/src/main/dialogflow/SetAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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;

// [START dialogflow_es_create_agent]

import com.google.cloud.dialogflow.v2.Agent;
import com.google.cloud.dialogflow.v2.Agent.Builder;
import com.google.cloud.dialogflow.v2.AgentsClient;
import com.google.cloud.dialogflow.v2.AgentsSettings;
import java.io.IOException;

public class SetAgent {

public static void main(String[] args) throws IOException {
String projectId = "my-project-id";

// The display name will set the name of your agent
String displayName = "my-display-name";

setAgent(projectId, displayName);
}

public static Agent setAgent(String parent, String displayName) throws IOException {

AgentsSettings agentsSettings = AgentsSettings.newBuilder().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);

Agent agent = build.build();

// Make API request to create agent
Agent response = client.setAgent(agent);
System.out.println(response);
return response;
}
}
}
// [END dialogflow_es_create_agent]
33 changes: 33 additions & 0 deletions dialogflow/snippets/src/test/dialogflow/SetAgentIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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;

import org.junit.Assert;
import org.junit.Test;

public class SetAgentIT {


/*
* We cannot test setAgent because Dialogflow ES can only have one agent
* and if we create a agent it will delete the exisitng testing agent and
* would cause all tests to fail
*/
@Test
public void testCreateAgent() {
Assert.assertTrue(true);
}

0 comments on commit 98a4fa1

Please sign in to comment.