use of com.ibm.watson.assistant.v1.model.MessageOptions in project java-sdk by watson-developer-cloud.
the class Assistant method message.
/**
* Get response to user input.
*
* <p>Send user input to a workspace and receive a response.
*
* <p>**Important:** This method has been superseded by the new v2 runtime API. The v2 API offers
* significant advantages, including ease of deployment, automatic state management, versioning,
* and search capabilities. For more information, see the
* [documentation](https://cloud.ibm.com/docs/assistant?topic=assistant-api-overview).
*
* @param messageOptions the {@link MessageOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link MessageResponse}
*/
public ServiceCall<MessageResponse> message(MessageOptions messageOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(messageOptions, "messageOptions cannot be null");
Map<String, String> pathParamsMap = new HashMap<String, String>();
pathParamsMap.put("workspace_id", messageOptions.workspaceId());
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/message", pathParamsMap));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "message");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
if (messageOptions.nodesVisitedDetails() != null) {
builder.query("nodes_visited_details", String.valueOf(messageOptions.nodesVisitedDetails()));
}
final JsonObject contentJson = new JsonObject();
if (messageOptions.input() != null) {
contentJson.add("input", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(messageOptions.input()));
}
if (messageOptions.intents() != null) {
contentJson.add("intents", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(messageOptions.intents()));
}
if (messageOptions.entities() != null) {
contentJson.add("entities", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(messageOptions.entities()));
}
if (messageOptions.alternateIntents() != null) {
contentJson.addProperty("alternate_intents", messageOptions.alternateIntents());
}
if (messageOptions.context() != null) {
contentJson.add("context", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(messageOptions.context()));
}
if (messageOptions.output() != null) {
contentJson.add("output", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(messageOptions.output()));
}
if (messageOptions.userId() != null) {
contentJson.addProperty("user_id", messageOptions.userId());
}
builder.bodyJson(contentJson);
ResponseConverter<MessageResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<MessageResponse>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
use of com.ibm.watson.assistant.v1.model.MessageOptions in project java-sdk by watson-developer-cloud.
the class AssistantToneAnalyzerIntegrationExample method main.
public static void main(String[] args) throws Exception {
// instantiate the assistant service
Authenticator assistantAuthenticator = new IamAuthenticator("<iam_api_key>");
final Assistant assistantService = new Assistant("2019-02-28", assistantAuthenticator);
// instantiate the tone analyzer service
Authenticator toneAuthenticator = new IamAuthenticator("<iam_api_key>");
ToneAnalyzer toneService = new ToneAnalyzer("2017-09-21", toneAuthenticator);
// workspace id
final String workspaceId = "<workspace-id>";
// maintain history in the context variable - will add a history variable to
// each of the emotion, social
// and language tones
final boolean maintainHistory = false;
/**
* Input for the Assistant service: text (String): an input string (the user's conversation
* turn) and context (Context): any context that needs to be maintained - either added by the
* client app or passed in the response from the Assistant service on the previous conversation
* turn.
*/
final String text = "I am happy";
final Context context = new Context();
// UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
// set local context variable to the context from the last response from the
// Assistant Service
// (see the getContext() method of the MessageResponse class in
// com.ibm.watson.assistant.v1.model)
// async call to Tone Analyzer
ToneOptions toneOptions = new ToneOptions.Builder().text(text).build();
toneService.tone(toneOptions).enqueue(new ServiceCallback<ToneAnalysis>() {
@Override
public void onResponse(Response<ToneAnalysis> toneResponsePayload) {
// update context with the tone data returned by the Tone Analyzer
context.setSystem(ToneDetection.updateUserTone(context, toneResponsePayload.getResult(), maintainHistory));
// create input for message
MessageInput input = new MessageInput();
input.setText(text);
// call Assistant Service with the input and tone-aware context
MessageOptions messageOptions = new MessageOptions.Builder(workspaceId).input(input).context(context).build();
assistantService.message(messageOptions).enqueue(new ServiceCallback<MessageResponse>() {
@Override
public void onResponse(Response<MessageResponse> response) {
System.out.println(response.getResult());
}
@Override
public void onFailure(Exception e) {
}
});
}
@Override
public void onFailure(Exception e) {
}
});
}
Aggregations