我创建了一个简单的dialogflow目的。 我想从本地环境(Linux)中的Java应用程序中使用它。 我收到错误消息:
应用程序默认凭据不可用。如果它们在Google Compute Engine中运行,则可用。否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS指向指向定义凭据的文件。
我这样设置环境变量:
export GOOGLE_APPLICATION_CREDENTIALS="/home/me/******.json"
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dialogflow</artifactId>
<version>0.99.0-alpha</version>
</dependency>
public class App {
public static void main(String[] args) throws Exception {
String projectId = "my-project-id";
String sessionId = "fake_session_for_testing";
String languageCode = "fr-FR";
List<String> texts = Arrays.asList("hello", "Qui tu es");
Map<String, QueryResult> result = detectIntentTexts(projectId, texts, sessionId, languageCode);
result.forEach((s, q) -> System.out.println(s + " " + q));
}
public static Map<String, QueryResult> detectIntentTexts(
String projectId,
List<String> texts,
String sessionId,
String languageCode) throws Exception {
Map<String, QueryResult> queryResults = new HashMap<>();
// Instantiates a client
try (SessionsClient sessionsClient = SessionsClient.create()) {
// Set the session name using the sessionId (UUID) and projectID (my-project-id)
SessionName session = SessionName.of(projectId, sessionId);
System.out.println("Session Path: " + session.toString());
// Detect intents for each text input
for (String text : texts) {
// Set the text (hello) and language code (en-US) for the query
TextInput.Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
// Build the query with the TextInput
QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
// Performs the detect intent request
DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
// Display the query result
QueryResult queryResult = response.getQueryResult();
System.out.println("====================");
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
System.out.format("Detected Intent: %s (confidence: %f)\n",
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());
queryResults.put(text, queryResult);
}
}
return queryResults;
}
}