2525import com .ibm .watson .tone_analyzer .v3 .model .ToneOptions ;
2626import com .ibm .cloud .sdk .core .http .Response ;
2727import com .ibm .cloud .sdk .core .http .ServiceCallback ;
28- import com .ibm .cloud .sdk .core .service .security .IamOptions ;
2928
3029
3130/**
@@ -36,18 +35,12 @@ public class AssistantToneAnalyzerIntegrationExample {
3635 public static void main (String [] args ) throws Exception {
3736
3837 // instantiate the assistant service
39- Assistant assistantService = new Assistant ("2018-02-16" );
40- IamOptions options = new IamOptions .Builder ()
41- .apiKey ("<iam_api_key>" )
42- .build ();
43- assistantService .setIamCredentials (options );
38+ Authenticator assistantAuthenticator = new IamAuthenticator ("<iam_api_key>" );
39+ Assistant assistantService = new Assistant ("2018-02-16" , assistantAuthenticator );
4440
4541 // instantiate the tone analyzer service
46- ToneAnalyzer toneService = new ToneAnalyzer ("2017-09-21" );
47- IamOptions options = new IamOptions .Builder ()
48- .apiKey ("<iam_api_key>" )
49- .build ();
50- toneService .setIamCredentials (options );
42+ Authenticator toneAuthenticator = new IamAuthenticator ("<iam_api_key>" );
43+ ToneAnalyzer toneService = new ToneAnalyzer ("2017-09-21" , toneAuthenticator );
5144
5245 // workspace id
5346 String workspaceId = "<workspace-id>" ;
@@ -58,12 +51,12 @@ public static void main(String[] args) throws Exception {
5851 boolean maintainHistory = false ;
5952
6053 /**
61- * Input for the Assistant service: input (String): an input string (the user's conversation turn) and context
62- * (Map<String,Object> : any context that needs to be maintained - either added by the client app or passed in the
54+ * Input for the Assistant service: text (String): an input string (the user's conversation turn) and context
55+ * (Context) : any context that needs to be maintained - either added by the client app or passed in the
6356 * response from the Assistant service on the previous conversation turn.
6457 */
65- String input = "I am happy" ;
66- Map < String , Object > context = new HashMap <>() ;
58+ String text = "I am happy" ;
59+ Context context = null ;
6760
6861 // UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
6962 // set local context variable to the context from the last response from the
@@ -82,10 +75,14 @@ public void onResponse(Response<ToneAnalysis> toneResponsePayload) {
8275 // update context with the tone data returned by the Tone Analyzer
8376 context = ToneDetection .updateUserTone (context , toneResponsePayload .getResult (), maintainHistory );
8477
78+ // create input for message
79+ MessageInput input = new MessageInput ();
80+ input .setText (text );
81+
8582 // call Assistant Service with the input and tone-aware context
8683 MessageOptions messageOptions = new MessageOptions .Builder (workspaceId )
87- .input (new InputData . Builder ( input ). build () )
88- .context (( Context ) context )
84+ .input (input )
85+ .context (context )
8986 .build ();
9087 assistantService .message (messageOptions ).enqueue (new ServiceCallback <MessageResponse >() {
9188 @ Override
0 commit comments