-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add update intent code sample (#275)
* add update intent samples * Fixed lint * lint fix * lint fix * lint fix * lint fix * lint fix * lint fix * lint fix * fixed failing test * lint fix * failing test fix * Fix failing test * fixed fialing test * Added Agent Builder * removed unused builder * lint fix * lint fix * Fixed comments * Updated main * Lint fix * removed return type * Fix build error * added exception * fixed failing test * added missing include * Rebased * 🦉 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
1 parent
bfb34d0
commit c8f3e2f
Showing
2 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
dialogflow-cx/snippets/src/main/java/dialogflow/cx/UpdateIntent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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_update_intent] | ||
|
||
import com.google.cloud.dialogflow.cx.v3.Intent; | ||
import com.google.cloud.dialogflow.cx.v3.Intent.Builder; | ||
import com.google.cloud.dialogflow.cx.v3.IntentsClient; | ||
import com.google.cloud.dialogflow.cx.v3.UpdateIntentRequest; | ||
import com.google.protobuf.FieldMask; | ||
import java.io.IOException; | ||
|
||
public class UpdateIntent { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project-id"; | ||
String agentId = "my-agent-id"; | ||
String intentId = "my-intent-id"; | ||
String location = "my-location"; | ||
String displayName = "my-display-name"; | ||
updateIntent(projectId, agentId, intentId, location, displayName); | ||
} | ||
|
||
// DialogFlow API Update Intent sample. | ||
public static void updateIntent( | ||
String projectId, String agentId, String intentId, String location, String displayName) | ||
throws IOException { | ||
try (IntentsClient client = IntentsClient.create()) { | ||
String intentPath = | ||
"projects/" | ||
+ projectId | ||
+ "/locations/" | ||
+ location | ||
+ "/agents/" | ||
+ agentId | ||
+ "/intents/" | ||
+ intentId; | ||
|
||
Builder intentBuilder = client.getIntent(intentPath).toBuilder(); | ||
|
||
intentBuilder.setDisplayName(displayName); | ||
FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build(); | ||
|
||
Intent intent = intentBuilder.build(); | ||
UpdateIntentRequest request = | ||
UpdateIntentRequest.newBuilder() | ||
.setIntent(intent) | ||
.setLanguageCode("en") | ||
.setUpdateMask(fieldMask) | ||
.build(); | ||
|
||
// Make API request to update intent using fieldmask | ||
Intent response = client.updateIntent(request); | ||
System.out.println(response); | ||
} | ||
} | ||
} | ||
// [END dialogflow_cx_update_intent] |
99 changes: 99 additions & 0 deletions
99
dialogflow-cx/snippets/src/test/java/dialogflow/cx/UpdateIntentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.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 com.google.cloud.dialogflow.cx.v3.Intent; | ||
import com.google.cloud.dialogflow.cx.v3.IntentsClient; | ||
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 UpdateIntentTest { | ||
|
||
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); | ||
private static String parent = ""; | ||
private static String intentID = ""; | ||
private static String intentPath = ""; | ||
private static String agentID = ""; | ||
|
||
private ByteArrayOutputStream stdOut; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
|
||
stdOut = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(stdOut)); | ||
|
||
Builder build = Agent.newBuilder(); | ||
build.setDefaultLanguageCode("en"); | ||
build.setDisplayName("temp_agent_" + UUID.randomUUID().toString()); | ||
build.setTimeZone("America/Los_Angeles"); | ||
|
||
Agent agent = build.build(); | ||
|
||
String apiEndpoint = "global-dialogflow.googleapis.com:443"; | ||
String parentPath = "projects/" + PROJECT_ID + "/locations/global"; | ||
|
||
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
AgentsClient client = AgentsClient.create(agentsSettings); | ||
|
||
parent = client.createAgent(parentPath, agent).getName(); | ||
UpdateIntentTest.agentID = parent.split("/")[5]; | ||
|
||
try (IntentsClient intentsClient = IntentsClient.create()) { | ||
com.google.cloud.dialogflow.cx.v3.Intent.Builder intent = Intent.newBuilder(); | ||
intent.setDisplayName("temp_intent_" + UUID.randomUUID().toString()); | ||
|
||
UpdateIntentTest.intentPath = intentsClient.createIntent(parent, intent.build()).getName(); | ||
UpdateIntentTest.intentID = UpdateIntentTest.intentPath.split("/")[7]; | ||
} | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
stdOut = null; | ||
System.setOut(null); | ||
String apiEndpoint = "global-dialogflow.googleapis.com:443"; | ||
String parentPath = "projects/" + PROJECT_ID + "/locations/global"; | ||
|
||
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build(); | ||
AgentsClient client = AgentsClient.create(agentsSettings); | ||
|
||
client.deleteAgent(parent); | ||
} | ||
|
||
@Test | ||
public void testUpdateIntent() throws IOException { | ||
|
||
String fakeIntent = "fake_intent_" + UUID.randomUUID().toString(); | ||
|
||
UpdateIntent.updateIntent( | ||
PROJECT_ID, UpdateIntentTest.agentID, UpdateIntentTest.intentID, "global", fakeIntent); | ||
|
||
assertThat(stdOut.toString()).contains(fakeIntent); | ||
} | ||
} |