Skip to content

Commit c8f3e2f

Browse files
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>
1 parent bfb34d0 commit c8f3e2f

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_update_intent]
20+
21+
import com.google.cloud.dialogflow.cx.v3.Intent;
22+
import com.google.cloud.dialogflow.cx.v3.Intent.Builder;
23+
import com.google.cloud.dialogflow.cx.v3.IntentsClient;
24+
import com.google.cloud.dialogflow.cx.v3.UpdateIntentRequest;
25+
import com.google.protobuf.FieldMask;
26+
import java.io.IOException;
27+
28+
public class UpdateIntent {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "my-project-id";
33+
String agentId = "my-agent-id";
34+
String intentId = "my-intent-id";
35+
String location = "my-location";
36+
String displayName = "my-display-name";
37+
updateIntent(projectId, agentId, intentId, location, displayName);
38+
}
39+
40+
// DialogFlow API Update Intent sample.
41+
public static void updateIntent(
42+
String projectId, String agentId, String intentId, String location, String displayName)
43+
throws IOException {
44+
try (IntentsClient client = IntentsClient.create()) {
45+
String intentPath =
46+
"projects/"
47+
+ projectId
48+
+ "/locations/"
49+
+ location
50+
+ "/agents/"
51+
+ agentId
52+
+ "/intents/"
53+
+ intentId;
54+
55+
Builder intentBuilder = client.getIntent(intentPath).toBuilder();
56+
57+
intentBuilder.setDisplayName(displayName);
58+
FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build();
59+
60+
Intent intent = intentBuilder.build();
61+
UpdateIntentRequest request =
62+
UpdateIntentRequest.newBuilder()
63+
.setIntent(intent)
64+
.setLanguageCode("en")
65+
.setUpdateMask(fieldMask)
66+
.build();
67+
68+
// Make API request to update intent using fieldmask
69+
Intent response = client.updateIntent(request);
70+
System.out.println(response);
71+
}
72+
}
73+
}
74+
// [END dialogflow_cx_update_intent]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
import static com.google.common.truth.Truth.assertThat;
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 com.google.cloud.dialogflow.cx.v3.Intent;
26+
import com.google.cloud.dialogflow.cx.v3.IntentsClient;
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import java.util.UUID;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
35+
public class UpdateIntentTest {
36+
37+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
38+
private static String parent = "";
39+
private static String intentID = "";
40+
private static String intentPath = "";
41+
private static String agentID = "";
42+
43+
private ByteArrayOutputStream stdOut;
44+
45+
@Before
46+
public void setUp() throws IOException {
47+
48+
stdOut = new ByteArrayOutputStream();
49+
System.setOut(new PrintStream(stdOut));
50+
51+
Builder build = Agent.newBuilder();
52+
build.setDefaultLanguageCode("en");
53+
build.setDisplayName("temp_agent_" + UUID.randomUUID().toString());
54+
build.setTimeZone("America/Los_Angeles");
55+
56+
Agent agent = build.build();
57+
58+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
59+
String parentPath = "projects/" + PROJECT_ID + "/locations/global";
60+
61+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
62+
AgentsClient client = AgentsClient.create(agentsSettings);
63+
64+
parent = client.createAgent(parentPath, agent).getName();
65+
UpdateIntentTest.agentID = parent.split("/")[5];
66+
67+
try (IntentsClient intentsClient = IntentsClient.create()) {
68+
com.google.cloud.dialogflow.cx.v3.Intent.Builder intent = Intent.newBuilder();
69+
intent.setDisplayName("temp_intent_" + UUID.randomUUID().toString());
70+
71+
UpdateIntentTest.intentPath = intentsClient.createIntent(parent, intent.build()).getName();
72+
UpdateIntentTest.intentID = UpdateIntentTest.intentPath.split("/")[7];
73+
}
74+
}
75+
76+
@After
77+
public void tearDown() throws IOException {
78+
stdOut = null;
79+
System.setOut(null);
80+
String apiEndpoint = "global-dialogflow.googleapis.com:443";
81+
String parentPath = "projects/" + PROJECT_ID + "/locations/global";
82+
83+
AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
84+
AgentsClient client = AgentsClient.create(agentsSettings);
85+
86+
client.deleteAgent(parent);
87+
}
88+
89+
@Test
90+
public void testUpdateIntent() throws IOException {
91+
92+
String fakeIntent = "fake_intent_" + UUID.randomUUID().toString();
93+
94+
UpdateIntent.updateIntent(
95+
PROJECT_ID, UpdateIntentTest.agentID, UpdateIntentTest.intentID, "global", fakeIntent);
96+
97+
assertThat(stdOut.toString()).contains(fakeIntent);
98+
}
99+
}

0 commit comments

Comments
 (0)