14
14
15
15
import com .ibm .cloud .sdk .core .http .Response ;
16
16
import com .ibm .cloud .sdk .core .http .ServiceCallback ;
17
+ import com .ibm .cloud .sdk .core .security .Authenticator ;
18
+ import com .ibm .cloud .sdk .core .security .IamAuthenticator ;
17
19
import com .ibm .watson .assistant .v1 .Assistant ;
18
20
import com .ibm .watson .assistant .v1 .model .Context ;
21
+ import com .ibm .watson .assistant .v1 .model .MessageInput ;
19
22
import com .ibm .watson .assistant .v1 .model .MessageOptions ;
20
23
import com .ibm .watson .assistant .v1 .model .MessageResponse ;
21
24
import com .ibm .watson .tone_analyzer .v3 .ToneAnalyzer ;
@@ -29,28 +32,28 @@ public static void main(String[] args) throws Exception {
29
32
30
33
// instantiate the assistant service
31
34
Authenticator assistantAuthenticator = new IamAuthenticator ("<iam_api_key>" );
32
- Assistant assistantService = new Assistant ("2019-02-28" , assistantAuthenticator );
35
+ final Assistant assistantService = new Assistant ("2019-02-28" , assistantAuthenticator );
33
36
34
37
// instantiate the tone analyzer service
35
38
Authenticator toneAuthenticator = new IamAuthenticator ("<iam_api_key>" );
36
39
ToneAnalyzer toneService = new ToneAnalyzer ("2017-09-21" , toneAuthenticator );
37
40
38
41
// workspace id
39
- String workspaceId = "<workspace-id>" ;
42
+ final String workspaceId = "<workspace-id>" ;
40
43
41
44
// maintain history in the context variable - will add a history variable to
42
45
// each of the emotion, social
43
46
// and language tones
44
- boolean maintainHistory = false ;
47
+ final boolean maintainHistory = false ;
45
48
46
49
/**
47
50
* Input for the Assistant service: text (String): an input string (the user's conversation
48
51
* turn) and context (Context): any context that needs to be maintained - either added by the
49
52
* client app or passed in the response from the Assistant service on the previous conversation
50
53
* turn.
51
54
*/
52
- String text = "I am happy" ;
53
- Context context = null ;
55
+ final String text = "I am happy" ;
56
+ final Context context = new Context () ;
54
57
55
58
// UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
56
59
// set local context variable to the context from the last response from the
@@ -59,7 +62,7 @@ public static void main(String[] args) throws Exception {
59
62
// com.ibm.watson.assistant.v1.model)
60
63
61
64
// async call to Tone Analyzer
62
- ToneOptions toneOptions = new ToneOptions .Builder ().text (input ).build ();
65
+ ToneOptions toneOptions = new ToneOptions .Builder ().text (text ).build ();
63
66
toneService
64
67
.tone (toneOptions )
65
68
.enqueue (
@@ -68,9 +71,9 @@ public static void main(String[] args) throws Exception {
68
71
public void onResponse (Response <ToneAnalysis > toneResponsePayload ) {
69
72
70
73
// update context with the tone data returned by the Tone Analyzer
71
- context =
74
+ context . setSystem (
72
75
ToneDetection .updateUserTone (
73
- context , toneResponsePayload .getResult (), maintainHistory );
76
+ context , toneResponsePayload .getResult (), maintainHistory )) ;
74
77
75
78
// create input for message
76
79
MessageInput input = new MessageInput ();
0 commit comments