Search in sources :

Example 31 with Workspace

use of com.ibm.watson.assistant.v1.model.Workspace in project java-sdk by watson-developer-cloud.

the class Assistant method updateWorkspace.

/**
 * Update workspace.
 *
 * <p>Update an existing workspace with new or modified data. You must provide component objects
 * defining the content of the updated workspace.
 *
 * @param updateWorkspaceOptions the {@link UpdateWorkspaceOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link Workspace}
 */
public ServiceCall<Workspace> updateWorkspace(UpdateWorkspaceOptions updateWorkspaceOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(updateWorkspaceOptions, "updateWorkspaceOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", updateWorkspaceOptions.workspaceId());
    RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "updateWorkspace");
    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 (updateWorkspaceOptions.append() != null) {
        builder.query("append", String.valueOf(updateWorkspaceOptions.append()));
    }
    if (updateWorkspaceOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(updateWorkspaceOptions.includeAudit()));
    }
    final JsonObject contentJson = new JsonObject();
    if (updateWorkspaceOptions.name() != null) {
        contentJson.addProperty("name", updateWorkspaceOptions.name());
    }
    if (updateWorkspaceOptions.description() != null) {
        contentJson.addProperty("description", updateWorkspaceOptions.description());
    }
    if (updateWorkspaceOptions.language() != null) {
        contentJson.addProperty("language", updateWorkspaceOptions.language());
    }
    if (updateWorkspaceOptions.dialogNodes() != null) {
        contentJson.add("dialog_nodes", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.dialogNodes()));
    }
    if (updateWorkspaceOptions.counterexamples() != null) {
        contentJson.add("counterexamples", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.counterexamples()));
    }
    if (updateWorkspaceOptions.metadata() != null) {
        contentJson.add("metadata", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.metadata()));
    }
    if (updateWorkspaceOptions.learningOptOut() != null) {
        contentJson.addProperty("learning_opt_out", updateWorkspaceOptions.learningOptOut());
    }
    if (updateWorkspaceOptions.systemSettings() != null) {
        contentJson.add("system_settings", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.systemSettings()));
    }
    if (updateWorkspaceOptions.webhooks() != null) {
        contentJson.add("webhooks", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.webhooks()));
    }
    if (updateWorkspaceOptions.intents() != null) {
        contentJson.add("intents", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.intents()));
    }
    if (updateWorkspaceOptions.entities() != null) {
        contentJson.add("entities", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(updateWorkspaceOptions.entities()));
    }
    builder.bodyJson(contentJson);
    ResponseConverter<Workspace> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<Workspace>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) Workspace(com.ibm.watson.assistant.v1.model.Workspace)

Example 32 with Workspace

use of com.ibm.watson.assistant.v1.model.Workspace in project java-sdk by watson-developer-cloud.

the class Assistant method listIntents.

/**
 * List intents.
 *
 * <p>List the intents for a workspace.
 *
 * @param listIntentsOptions the {@link ListIntentsOptions} containing the options for the call
 * @return a {@link ServiceCall} with a result of type {@link IntentCollection}
 */
public ServiceCall<IntentCollection> listIntents(ListIntentsOptions listIntentsOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(listIntentsOptions, "listIntentsOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", listIntentsOptions.workspaceId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/intents", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "listIntents");
    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 (listIntentsOptions.export() != null) {
        builder.query("export", String.valueOf(listIntentsOptions.export()));
    }
    if (listIntentsOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listIntentsOptions.pageLimit()));
    }
    if (listIntentsOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listIntentsOptions.includeCount()));
    }
    if (listIntentsOptions.sort() != null) {
        builder.query("sort", String.valueOf(listIntentsOptions.sort()));
    }
    if (listIntentsOptions.cursor() != null) {
        builder.query("cursor", String.valueOf(listIntentsOptions.cursor()));
    }
    if (listIntentsOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listIntentsOptions.includeAudit()));
    }
    ResponseConverter<IntentCollection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<IntentCollection>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) IntentCollection(com.ibm.watson.assistant.v1.model.IntentCollection) HashMap(java.util.HashMap)

Example 33 with Workspace

use of com.ibm.watson.assistant.v1.model.Workspace in project java-sdk by watson-developer-cloud.

the class Assistant method listDialogNodes.

/**
 * List dialog nodes.
 *
 * <p>List the dialog nodes for a workspace.
 *
 * @param listDialogNodesOptions the {@link ListDialogNodesOptions} containing the options for the
 *     call
 * @return a {@link ServiceCall} with a result of type {@link DialogNodeCollection}
 */
public ServiceCall<DialogNodeCollection> listDialogNodes(ListDialogNodesOptions listDialogNodesOptions) {
    com.ibm.cloud.sdk.core.util.Validator.notNull(listDialogNodesOptions, "listDialogNodesOptions cannot be null");
    Map<String, String> pathParamsMap = new HashMap<String, String>();
    pathParamsMap.put("workspace_id", listDialogNodesOptions.workspaceId());
    RequestBuilder builder = RequestBuilder.get(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/workspaces/{workspace_id}/dialog_nodes", pathParamsMap));
    Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("conversation", "v1", "listDialogNodes");
    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 (listDialogNodesOptions.pageLimit() != null) {
        builder.query("page_limit", String.valueOf(listDialogNodesOptions.pageLimit()));
    }
    if (listDialogNodesOptions.includeCount() != null) {
        builder.query("include_count", String.valueOf(listDialogNodesOptions.includeCount()));
    }
    if (listDialogNodesOptions.sort() != null) {
        builder.query("sort", String.valueOf(listDialogNodesOptions.sort()));
    }
    if (listDialogNodesOptions.cursor() != null) {
        builder.query("cursor", String.valueOf(listDialogNodesOptions.cursor()));
    }
    if (listDialogNodesOptions.includeAudit() != null) {
        builder.query("include_audit", String.valueOf(listDialogNodesOptions.includeAudit()));
    }
    ResponseConverter<DialogNodeCollection> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<DialogNodeCollection>() {
    }.getType());
    return createServiceCall(builder.build(), responseConverter);
}
Also used : RequestBuilder(com.ibm.cloud.sdk.core.http.RequestBuilder) HashMap(java.util.HashMap) DialogNodeCollection(com.ibm.watson.assistant.v1.model.DialogNodeCollection)

Example 34 with Workspace

use of com.ibm.watson.assistant.v1.model.Workspace 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) {
        }
    });
}
Also used : Context(com.ibm.watson.assistant.v1.model.Context) ToneAnalysis(com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) MessageInput(com.ibm.watson.assistant.v1.model.MessageInput) ToneOptions(com.ibm.watson.tone_analyzer.v3.model.ToneOptions) MessageResponse(com.ibm.watson.assistant.v1.model.MessageResponse) Response(com.ibm.cloud.sdk.core.http.Response) ServiceCallback(com.ibm.cloud.sdk.core.http.ServiceCallback) ToneAnalyzer(com.ibm.watson.tone_analyzer.v3.ToneAnalyzer) MessageOptions(com.ibm.watson.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.assistant.v1.Assistant) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Aggregations

HashMap (java.util.HashMap)21 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)18 JsonObject (com.google.gson.JsonObject)15 Test (org.junit.Test)8 Workspace (com.ibm.watson.assistant.v1.model.Workspace)6 Workspace (com.ibm.watson.developer_cloud.assistant.v1.model.Workspace)6 Workspace (com.ibm.watson.developer_cloud.conversation.v1.model.Workspace)6 NotFoundException (com.ibm.watson.developer_cloud.service.exception.NotFoundException)6 UnauthorizedException (com.ibm.watson.developer_cloud.service.exception.UnauthorizedException)6 DialogNode (com.ibm.watson.assistant.v1.model.DialogNode)5 Counterexample (com.ibm.watson.assistant.v1.model.Counterexample)3 CreateWorkspaceOptions (com.ibm.watson.developer_cloud.assistant.v1.model.CreateWorkspaceOptions)3 DeleteWorkspaceOptions (com.ibm.watson.developer_cloud.assistant.v1.model.DeleteWorkspaceOptions)3 CreateWorkspaceOptions (com.ibm.watson.developer_cloud.conversation.v1.model.CreateWorkspaceOptions)3 DeleteWorkspaceOptions (com.ibm.watson.developer_cloud.conversation.v1.model.DeleteWorkspaceOptions)3 RequestBuilder (com.ibm.watson.developer_cloud.http.RequestBuilder)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 CreateEntity (com.ibm.watson.assistant.v1.model.CreateEntity)2 CreateIntent (com.ibm.watson.assistant.v1.model.CreateIntent)2