-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatConfig.java
47 lines (39 loc) · 1.81 KB
/
ChatConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.example.account.domain;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.dialogflow.v2.SessionsClient;
import com.google.cloud.dialogflow.v2.SessionsSettings;
import com.google.cloud.dialogflow.v2.DetectIntentResponse;
import com.google.cloud.dialogflow.v2.QueryInput;
import com.google.cloud.dialogflow.v2.SessionName;
import com.google.cloud.dialogflow.v2.TextInput;
public class ChatConfig {
private SessionsClient client;
private String project;
public ChatConfig (String credentialFile, String project) throws FileNotFoundException, IOException {
this.project = project;
Credentials credentials = GoogleCredentials.fromStream(new FileInputStream(credentialFile));
SessionsSettings settings = SessionsSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
client = SessionsClient.create(settings);
}
public String request(String sessionId, String message) {
QueryInput queryInput;
queryInput = QueryInput.newBuilder()
.setText(
TextInput.newBuilder()
.setText(message)
.setLanguageCode("ko")
.build())
.build();
// Perform query
SessionName session = SessionName.of(project, sessionId);
DetectIntentResponse actualResponse = client.detectIntent(session, queryInput);
return actualResponse.getQueryResult().getFulfillmentText();
}
}