Skip to content

Commit d3b3d5a

Browse files
committed
Fix DialogFlow tests and update to canonical sample format.
1 parent 68f12df commit d3b3d5a

29 files changed

+778
-1690
lines changed

dialogflow/cloud-client/README.md

+4-201
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,9 @@ To get started, [download][maven-download] and [install][maven-install] it.
3232
variable:
3333

3434
```bash
35+
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
3536
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
3637
```
37-
* To run tests, set GOOGLE_CLOUD_PROJECT to your PROJECT_ID:
38-
```
39-
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
40-
```
41-
```
42-
mvn clean verify
43-
```
4438
* Set PROJECT_ID in pom.xml to your Google Cloud Project Id.
4539
* Set SESSION_ID in pom.xml to a session name of your choice. (Defaults to SESSION_ID)
4640
* Set CONTEXT_ID in pom.xml to a context name of your choice. (Defaults to CONTEXT_ID)
@@ -49,201 +43,10 @@ To get started, [download][maven-download] and [install][maven-install] it.
4943
[dialogflow-api]: https://console.cloud.google.com/apis/library/dialogflow.googleapis.com
5044
[auth]: https://cloud.google.com/docs/authentication/getting-started
5145

52-
## Run the sample
46+
## Run the Tests
5347

54-
To build the sample, we use Maven.
48+
To verify the API's are enabled, run the unit tests via
5549
5650
```bash
57-
mvn clean package
58-
```
59-
60-
### Set up the Agent
61-
Import the sample agent (RoomReservation.zip) from the resources directory to your
62-
[Dialogflow Project][dialogflow-import] using the following guide:
63-
[Versioning with Import/Export][import-export-versioning]
64-
65-
[dialogflow-import]: https://console.dialogflow.com/api-client/#/editAgent/
66-
[import-export-versioning]: https://dialogflow.com/docs/best-practices/import-export-for-versions
67-
68-
## Samples
69-
70-
### Detect Intent Texts
71-
DialogFlow API Detect Intent sample with text inputs.
72-
73-
```
74-
mvn exec:java -DDetectIntentText
75-
```
76-
77-
### Detect Intent Audio
78-
DialogFlow API Detect Intent sample with audio files. Returns the result of detect intent with an
79-
audio file as input.
80-
81-
Note: Execute the following commands in order to yield reasonable outputs.
82-
```
83-
mvn exec:java -DDetectIntentAudioBookARoom
84-
```
85-
```
86-
mvn exec:java -DDetectIntentAudioMountainView
87-
```
88-
```
89-
mvn exec:java -DDetectIntentAudioToday
90-
```
91-
```
92-
mvn exec:java -DDetectIntentAudio230PM
93-
```
94-
```
95-
mvn exec:java -DDetectIntentAudioHalfAnHour
96-
```
97-
```
98-
mvn exec:java -DDetectIntentAudioTwoPeople
99-
```
100-
101-
### Detect Intent Stream
102-
DialogFlow API Detect Intent sample with audio files processes as an audio stream.
103-
104-
```
105-
mvn exec:java -DDetectIntentStreamBookARoom
106-
```
107-
```
108-
mvn exec:java -DDetectIntentStreamMountainView
109-
```
110-
111-
### Detect Intent With Model Selection
112-
DialogFlow API Detect Intent sample with model selection
113-
```
114-
mvn exec:java -DDetectIntentWithModelSelection -Dexec.args='--projectId PROJECT_ID --audioFilePath resources/book_a_room.wav --sessionId SESSION_ID'
115-
```
116-
117-
### Detect Intent With Sentiment Analysis
118-
DialogFlow API Detect Intent sample with sentiment analysis
119-
```
120-
mvn exec:java -DDetectIntentWithSentimentAnalysis -Dexec.args="--projectId PROJECT_ID -sessionId SESSION_ID 'hello'"
121-
```
122-
123-
### Detect Intent With Text-to-Speech
124-
DialogFlow API Detect Intent sample with Text-to-Speech
125-
```
126-
mvn exec:java -DetectIntentTTSResponse
127-
```
128-
129-
### Detect Intent Knowledge
130-
DialogFlow API Detect Intent sample with querying knowledge connector.
131-
```
132-
mvn exec:java -DDetectIntentKnowledge -Dexec.args="--projectId PROJECT_ID --knowledgeBaseId KNOWLEDGE_BASE_ID -sessionId SESSION_ID 'Where can I find pricing information?'"
133-
```
134-
135-
### Context Management
136-
DialogFlow API Context sample.
137-
138-
Lists contexts
139-
```
140-
mvn exec:java -DContextManagementList
141-
```
142-
Create an entity type
143-
```
144-
mvn exec:java -DContextManagementCreate
145-
```
146-
Delete entity type
147-
```
148-
mvn exec:java -DContextManagementDelete
149-
```
150-
151-
### Entity Management
152-
DialogFlow API Entity sample.
153-
154-
List entities
155-
```
156-
mvn exec:java -DEntityManagementList
157-
```
158-
Create an entity
159-
```
160-
mvn exec:java -DEntityManagementCreate
161-
```
162-
Delete entity
163-
```
164-
mvn exec:java -DEntityManagementDelete
165-
```
166-
167-
### Entity Type Management
168-
DialogFlow API EntityType sample.
169-
170-
List entity types
171-
```
172-
mvn exec:java -DEntityTypeManagementList
173-
```
174-
Create an entity type
175-
```
176-
mvn exec:java -DEntityTypeManagementCreate
177-
```
178-
Delete entity type
179-
```
180-
mvn exec:java -DEntityTypeManagementDelete
181-
```
182-
183-
### Intent Management
184-
DialogFlow API Intent sample.
185-
186-
List intents
187-
```
188-
mvn exec:java -DIntentManagementList
189-
```
190-
Create an intent
191-
```
192-
mvn exec:java -DIntentManagementCreate
193-
```
194-
Delete intent
195-
```
196-
mvn exec:java -DIntentManagementDelete
197-
```
198-
199-
### Session Entity Type Management
200-
DialogFlow API SessionEntityType sample.
201-
202-
List session entity types
203-
```
204-
mvn exec:java -DSessionEntityTypeManagementList
205-
```
206-
Create session entity type
207-
```
208-
mvn exec:java -DSessionEntityTypeManagementCreate
209-
```
210-
Delete session entity type
211-
```
212-
mvn exec:java -DSessionEntityTypeManagementDelete
213-
```
214-
215-
### Knowledge Base Management
216-
DialogFlow API KnowledgeBaseManagement sample
217-
218-
List knowledge base
219-
```
220-
mvn exec:java -DKnowledgeBaseManagement -Dexec.args='list --projectId PROJECT_ID'
221-
```
222-
Create knowledge base
223-
```
224-
mvn exec:java -DKnowledgeBaseManagement -Dexec.args='create DISPLAY_NAME --projectId PROJECT_ID'
225-
```
226-
Delete knowledge base
227-
```
228-
mvn exec:java -DKnowledgeBaseManagement -Dexec.args='delete KNOWLEDGE_BASE_ID --projectId PROJECT_ID'
229-
```
230-
231-
### Document Management
232-
DialogFlow API DocumentManagement sample
233-
234-
List documents
235-
```
236-
mvn exec:java -DDocumentManagement -Dexec.args='list --projectId PROJECT_ID --knowledgeBaseId KNOWLEDGE_BASE_ID'
237-
```
238-
Create doucment
239-
```
240-
mvn exec:java -DDocumentManagement -Dexec.args='create KNOWLEDGE_BASE_ID --projectId PROJECT_ID
241-
--displayName DISPLAY_NAME
242-
--mimeType text/html
243-
--knowledgeType FAQ'
244-
--contentUri https://cloud.google.com/storage/docs/faq'
245-
```
246-
Delete doucment
247-
```
248-
mvn exec:java -DDocumentManagement -Dexec.args='delete KNOWLEDGE_BASE_ID --projectId PROJECT_ID --documentId DOCUMENT_ID
51+
mvn clean verify
24952
```

dialogflow/cloud-client/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@
4242
<artifactId>google-cloud-dialogflow</artifactId>
4343
<version>0.55.1-alpha</version>
4444
</dependency>
45-
<dependency>
46-
<groupId>net.sourceforge.argparse4j</groupId>
47-
<artifactId>argparse4j</artifactId>
48-
<version>0.8.1</version>
49-
</dependency>
5045

5146
<!-- Test dependencies -->
5247
<dependency>

dialogflow/cloud-client/src/main/java/com/example/dialogflow/ContextManagement.java

+25-70
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,38 @@
1717
package com.example.dialogflow;
1818

1919
// Imports the Google Cloud client library
20+
2021
import com.google.cloud.dialogflow.v2.Context;
2122
import com.google.cloud.dialogflow.v2.ContextName;
2223
import com.google.cloud.dialogflow.v2.ContextsClient;
2324
import com.google.cloud.dialogflow.v2.SessionName;
25+
import com.google.common.collect.Lists;
2426
import com.google.protobuf.Value;
2527

28+
import java.util.List;
2629
import java.util.Map.Entry;
27-
import net.sourceforge.argparse4j.ArgumentParsers;
28-
import net.sourceforge.argparse4j.inf.ArgumentParser;
29-
import net.sourceforge.argparse4j.inf.ArgumentParserException;
30-
import net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup;
31-
import net.sourceforge.argparse4j.inf.Namespace;
32-
import net.sourceforge.argparse4j.inf.Subparser;
33-
import net.sourceforge.argparse4j.inf.Subparsers;
34-
3530

3631
/**
3732
* DialogFlow API Context sample.
3833
*/
3934
public class ContextManagement {
40-
4135
// [START dialogflow_list_contexts]
36+
4237
/**
4338
* Lists contexts
39+
*
4440
* @param sessionId Identifier of the DetectIntent session.
4541
* @param projectId Project/Agent Id.
42+
* @return List of Contexts found.
4643
*/
47-
public static void listContexts(String sessionId, String projectId) throws Exception {
44+
public static List<Context> listContexts(String sessionId, String projectId) throws Exception {
45+
List<Context> contexts = Lists.newArrayList();
4846
// Instantiates a client
4947
try (ContextsClient contextsClient = ContextsClient.create()) {
5048
// Set the session name using the sessionId (UUID) and projectId (my-project-id)
5149
SessionName session = SessionName.of(projectId, sessionId);
5250

5351
// Performs the list contexts request
54-
System.out.format("Contexts for session %s:\n", session.toString());
5552
for (Context context : contextsClient.listContexts(session).iterateAll()) {
5653
System.out.format("Context name: %s\n", context.getName());
5754
System.out.format("Lifespan Count: %d\n", context.getLifespanCount());
@@ -61,20 +58,29 @@ public static void listContexts(String sessionId, String projectId) throws Excep
6158
System.out.format("\t%s: %s\n", entry.getKey(), entry.getValue());
6259
}
6360
}
61+
62+
contexts.add(context);
6463
}
6564
}
65+
return contexts;
6666
}
6767
// [END dialogflow_list_contexts]
6868

6969
// [START dialogflow_create_context]
70+
7071
/**
7172
* Create an entity type with the given display name
72-
* @param contextId The Id of the context.
73-
* @param sessionId Identifier of the DetectIntent session.
73+
*
74+
* @param contextId The Id of the context.
75+
* @param sessionId Identifier of the DetectIntent session.
7476
* @param lifespanCount The lifespan count of the context.
75-
* @param projectId Project/Agent Id.
77+
* @param projectId Project/Agent Id.
78+
* @return The new Context.
7679
*/
77-
public static void createContext(String contextId, String sessionId, String projectId,
80+
public static Context createContext(
81+
String contextId,
82+
String sessionId,
83+
String projectId,
7884
int lifespanCount) throws Exception {
7985
// Instantiates a client
8086
try (ContextsClient contextsClient = ContextsClient.create()) {
@@ -97,13 +103,17 @@ public static void createContext(String contextId, String sessionId, String proj
97103
// Performs the create context request
98104
Context response = contextsClient.createContext(session, context);
99105
System.out.format("Context created: %s\n", response);
106+
107+
return response;
100108
}
101109
}
102110
// [END dialogflow_create_context]
103111

104112
// [START dialogflow_delete_context]
113+
105114
/**
106115
* Delete entity type with the given entity type name
116+
*
107117
* @param contextId The Id of the context.
108118
* @param sessionId Identifier of the DetectIntent session.
109119
* @param projectId Project/Agent Id.
@@ -119,59 +129,4 @@ public static void deleteContext(String contextId, String sessionId, String proj
119129
}
120130
}
121131
// [END dialogflow_delete_context]
122-
123-
public static void main(String[] args) throws Exception {
124-
ArgumentParser parser =
125-
ArgumentParsers.newFor("ContextManagement")
126-
.build()
127-
.defaultHelp(true)
128-
.description("Create / List / Delete a context.");
129-
130-
Subparsers subparsers = parser.addSubparsers().dest("command").title("Commands");
131-
132-
Subparser listParser = subparsers.addParser("list")
133-
.help("mvn exec:java -DContextManagement -Dexec.args='list --sessionId SESSION_ID "
134-
+ "--projectId PROJECT_ID'");
135-
listParser.addArgument("--sessionId")
136-
.help("Identifier of the DetectIntent session").required(true);
137-
listParser.addArgument("--projectId").help("Project/Agent Id").required(true);
138-
139-
Subparser createParser = subparsers.addParser("create")
140-
.help("mvn exec:java -DContextManagement -Dexec.args='create --sessionId SESSION_ID "
141-
+ "--projectId PROJECT_ID --contextId CONTEXT_ID'");
142-
createParser.addArgument("--sessionId")
143-
.help("Identifier of the DetectIntent session").required(true);
144-
createParser.addArgument("--projectId").help("Project/Agent Id").required(true);
145-
createParser.addArgument("--contextId")
146-
.help("The Id of the context")
147-
.required(true);
148-
createParser.addArgument("--lifespanCount")
149-
.help("The lifespan count of the context (Default: 1)").setDefault(1);
150-
151-
Subparser deleteParser = subparsers.addParser("delete")
152-
.help("mvn exec:java -DContextManagement -Dexec.args='delete --sessionId SESSION_ID "
153-
+ "--projectId PROJECT_ID --contextId CONTEXT_ID'");
154-
deleteParser.addArgument("--sessionId")
155-
.help("Identifier of the DetectIntent session").required(true);
156-
deleteParser.addArgument("--projectId").help("Project/Agent Id").required(true);
157-
deleteParser.addArgument("--contextId")
158-
.help("The Id of the context")
159-
.required(true);
160-
161-
try {
162-
Namespace namespace = parser.parseArgs(args);
163-
164-
if (namespace.get("command").equals("list")) {
165-
listContexts(namespace.get("sessionId"), namespace.get("projectId"));
166-
} else if (namespace.get("command").equals("create")) {
167-
createContext(namespace.get("contextId"), namespace.get("sessionId"),
168-
namespace.get("projectId"), namespace.get("lifespanCount"));
169-
} else if (namespace.get("command").equals("delete")) {
170-
deleteContext(namespace.get("contextId"), namespace.get("sessionId"),
171-
namespace.get("projectId"));
172-
}
173-
} catch (ArgumentParserException e) {
174-
parser.handleError(e);
175-
}
176-
}
177132
}

0 commit comments

Comments
 (0)