尝试使用 Intents 使用 AWS Java SDK 创建机器人

Trying to Create Bot with AWS Java SDK using Intents

我想以编程方式创建一个具有简单 Intent 的机器人,遗憾的是我收到一条错误消息,指出找不到资源(意图),尽管我可以在控制台中看到创建的 Intent。

这就是我的简单代码:

package building;

import java.util.ArrayList;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.lexmodelbuilding.AmazonLexModelBuilding;
import com.amazonaws.services.lexmodelbuilding.AmazonLexModelBuildingClientBuilder;
import com.amazonaws.services.lexmodelbuilding.model.ContentType;
import com.amazonaws.services.lexmodelbuilding.model.FulfillmentActivity;
import com.amazonaws.services.lexmodelbuilding.model.FulfillmentActivityType;
import com.amazonaws.services.lexmodelbuilding.model.Intent;
import com.amazonaws.services.lexmodelbuilding.model.Message;
import com.amazonaws.services.lexmodelbuilding.model.Prompt;
import com.amazonaws.services.lexmodelbuilding.model.PutBotRequest;
import com.amazonaws.services.lexmodelbuilding.model.PutBotResult;
import com.amazonaws.services.lexmodelbuilding.model.PutIntentRequest;
import com.amazonaws.services.lexmodelbuilding.model.PutIntentResult;

public class ChatBotBuilder {

    public static void main(String [] args) {

        AmazonLexModelBuilding botBuilder = AmazonLexModelBuildingClientBuilder.standard().withRegion(Regions.EU_WEST_1).build();
        ArrayList<Intent> intents = new ArrayList<Intent>();
        Intent test = new Intent().withIntentName("Testintent").withIntentVersion("1");


        intents.add(test);

        PutIntentRequest putIntentRequest = new PutIntentRequest()
                .withDescription("das ist ein intenttest")
                .withFulfillmentActivity(new FulfillmentActivity().withType(FulfillmentActivityType.ReturnIntent))
                .withName("Testintent")
                .withSampleUtterances(new String[] {"Ich möchte den Test starten","Gib mir was"});


        PutIntentResult intentResult = botBuilder.putIntent(putIntentRequest);

        System.out.println(intentResult.getName());

        PutBotRequest putBotRequest = new PutBotRequest().withChildDirected(false)
                .withClarificationPrompt(new Prompt()
                                            .withMaxAttempts(5)
                                            .withMessages(new Message[] {new Message().withContent("Was genau willst du?").withContentType(ContentType.PlainText), new Message().withContent("Was genau du wollen?").withContentType(ContentType.PlainText)}))
                .withDescription("Programmbot")
                .withIntents(intents)
                .withName("TestBot")
                .withLocale("en-US");


        PutBotResult result = botBuilder.putBot(putBotRequest);
        System.out.println(result.getName());
        System.out.println(result.getFailureReason());

    }
}

我希望代码创建一个 Intent,然后是一个具有创建的 Intent 的机器人。

我实际得到的是:

Exception in thread "main" com.amazonaws.services.lexmodelbuilding.model.BadRequestException: The resource 'Testintent' referenced in resource 'TestBot' was not found. Choose another resource. (Service: AmazonLexModelBuilding; Status Code: 400; Error Code: BadRequestException; Request ID: b42d37ee-d8d7-11e7-ac95-f9876d663323)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1639)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1304)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1056)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:743)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:717)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:699)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:667)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:649)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:513)
    at com.amazonaws.services.lexmodelbuilding.AmazonLexModelBuildingClient.doInvoke(AmazonLexModelBuildingClient.java:2626)
    at com.amazonaws.services.lexmodelbuilding.AmazonLexModelBuildingClient.invoke(AmazonLexModelBuildingClient.java:2602)
    at com.amazonaws.services.lexmodelbuilding.AmazonLexModelBuildingClient.executePutBot(AmazonLexModelBuildingClient.java:2302)
    at com.amazonaws.services.lexmodelbuilding.AmazonLexModelBuildingClient.putBot(AmazonLexModelBuildingClient.java:2278)
    at building.ChatBotBuilder.main(ChatBotBuilder.java:51)

我指定了错误的 Intent 版本。

Intent test = new Intent().withIntentName("Testintent").withIntentVersion("1");

无法找到此 Intent,因为创建的 Intent 的版本是“$LATEST”而不是“1”。

这是正确的:

Intent test = new Intent().withIntentName("Testintent").withIntentVersion("$LATEST");