Skip to content

Commit fa10ddf

Browse files
galz10Shabirmean
authored andcommitted
samples: add update intent code sample (#639)
* Added Fieldmask snippet * Update lint * Fixed lint issue * Rename updateIntentTest.java to UpdateIntentTest.java * Lint Fix * Lint fix * Fixed spacing * Update Testing * Added missing imports * Added Missing Imports * Added Missing Imports * delete intent after testing * change name of test * Updated Code and Test * lint fix * Fix test * Fixed test * Test fix * Update failing test * Lint fix * lint fix * lint fix * Fixed failing test * Corrected intent path * Revised code per comments * lint fix * lint fix * failing test fix
1 parent 84c6b30 commit fa10ddf

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 com.example.dialogflow;
18+
19+
// [START dialogflow_es_update_intent]
20+
import com.google.cloud.dialogflow.v2.Intent;
21+
import com.google.cloud.dialogflow.v2.Intent.Builder;
22+
import com.google.cloud.dialogflow.v2.IntentsClient;
23+
import com.google.cloud.dialogflow.v2.UpdateIntentRequest;
24+
import com.google.protobuf.FieldMask;
25+
import java.io.IOException;
26+
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 intentId = "my-intent-id";
34+
String location = "my-location";
35+
String displayName = "my-display-name";
36+
updateIntent(projectId, intentId, location, displayName);
37+
}
38+
39+
// DialogFlow API Update Intent sample.
40+
public static void updateIntent(
41+
String projectId, String intentId, String location, String displayName)
42+
throws IOException {
43+
try (IntentsClient client = IntentsClient.create()) {
44+
String intentPath =
45+
"projects/"
46+
+ projectId
47+
+ "/locations/"
48+
+ location
49+
+ "/agent/intents/"
50+
+ intentId;
51+
52+
Builder intentBuilder = client.getIntent(intentPath).toBuilder();
53+
54+
intentBuilder.setDisplayName(displayName);
55+
FieldMask fieldMask = FieldMask.newBuilder()
56+
.addPaths("display_name")
57+
.build();
58+
59+
Intent intent = intentBuilder.build();
60+
UpdateIntentRequest request =
61+
UpdateIntentRequest.newBuilder()
62+
.setIntent(intent)
63+
.setLanguageCode("en")
64+
.setUpdateMask(fieldMask)
65+
.build();
66+
67+
// Make API request to update intent using fieldmask
68+
Intent response = client.updateIntent(request);
69+
System.out.println(response);
70+
}
71+
}
72+
}
73+
// [END dialogflow_es_update_intent]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 com.example.dialogflow;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.dialogflow.v2.Agent;
22+
import com.google.cloud.dialogflow.v2.Agent.Builder;
23+
import com.google.cloud.dialogflow.v2.AgentsClient;
24+
import com.google.cloud.dialogflow.v2.AgentsSettings;
25+
import com.google.cloud.dialogflow.v2.Intent;
26+
import com.google.cloud.dialogflow.v2.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 UpdateIntentIT {
36+
37+
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
38+
39+
private static String parent = "projects/" + PROJECT_ID + "/locations/global/agent";
40+
private static String intentID = "";
41+
private static String intentPath = "";
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+
try (IntentsClient intentsClient = IntentsClient.create()) {
52+
com.google.cloud.dialogflow.v2.Intent.Builder intent = Intent.newBuilder();
53+
intent.setDisplayName("temp_intent_" + UUID.randomUUID().toString());
54+
55+
UpdateIntentIT.intentPath = intentsClient.createIntent(parent, intent.build()).getName();
56+
UpdateIntentIT.intentID = UpdateIntentIT.intentPath.split("/")[6];
57+
}
58+
}
59+
60+
@After
61+
public void tearDown() throws IOException {
62+
stdOut = null;
63+
System.setOut(null);
64+
65+
IntentsClient client = IntentsClient.create();
66+
67+
String intentPath =
68+
"projects/"
69+
+ PROJECT_ID
70+
+ "/locations/global/agent/intents/"
71+
+ UpdateIntentIT.intentID;
72+
73+
client.deleteIntent(intentPath);
74+
}
75+
76+
@Test
77+
public void testUpdateIntent() throws IOException {
78+
79+
String fakeIntent = "fake_intent_" + UUID.randomUUID().toString();
80+
81+
UpdateIntent.updateIntent(
82+
PROJECT_ID, UpdateIntentIT.intentID, "global", fakeIntent);
83+
84+
assertThat(stdOut.toString()).contains(fakeIntent);
85+
}
86+
}

0 commit comments

Comments
 (0)