如何在Java中使用Alexa的Dialogue Directives?

时间:2018-09-17 21:57:05

标签: java alexa alexa-skills-kit

我几乎没有与该主题相关的在线信息。 我不知道如何访问意图,或者使用给定意图满足某些参数的情况,使用它们来返回响应。我试图创建一个简单的对话

我:“添加单位”

Alexa:“该部队叫什么?”

我:“工程”

Alexa:“好吧,添加单位Engineering。”

目前,我所知道的唯一方法是在调用该技能后立即执行一项操作,无论说什么。例如,我可以直译为

我:“ Alexa,打开StudyPal”

Alexa:“激活该技能后会返回的内容”

或...

我:“ Alexa,向StudyPal询问我的单位”

Alexa:“激活该技能后会返回的内容”

任何帮助将不胜感激。供参考,这是我的一些代码...

public class StudyPalHandler implements RequestStreamHandler {
private final Skill skill;
private final JacksonSerializer serializer;

public StudyPalHandler() {
    skill = new StandardSkillBuilder()
            .addRequestHandler(new StudyPalExtraHandler())
            .build();
    serializer = new JacksonSerializer();
}

@Override
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
    String request = IOUtils.toString(inputStream);
    RequestEnvelope requestEnvelope = serializer.deserialize(request, RequestEnvelope.class);
    ResponseEnvelope responseEnvelope = skill.invoke(requestEnvelope);
    byte[] response = serializer.serialize(responseEnvelope).getBytes(StandardCharsets.UTF_8);
    outputStream.write(response);
}
}

public class StudyPalExtraHandler implements RequestHandler {
@Override
public boolean canHandle(HandlerInput handlerInput) {
    return true;
}

@Override
public Optional<Response> handle(HandlerInput handlerInput) {
    return handlerInput.getResponseBuilder().withSpeech("Something that is returned whenever the skill is activated").build();
}
}

1 个答案:

答案 0 :(得分:0)

您应该使用关联的处理程序类的canHandle()方法来检查该特定处理程序是否可以处理该请求。

例如:如果您要处理StudyPalIntent,则

public class StudyPalIntentHandler implements RequestHandler {

@Override
public boolean canHandle(HandlerInput input) {
    return input.matches(intentName("StudyPalIntent"));
}

@Override
public Optional<Response> handle(HandlerInput input) {
    return input.getResponseBuilder()
            .withSpeech("your response speech here")
            .withReprompt("your re prompt here")              
            .build();
}

从sdk源代码中,您可以使用像这样的对话框指令

return input.getResponseBuilder()
   .withSpeech("your response speech here")
   .withReprompt("your re prompt here") 
   .addDelegateDirective(updatedIntent)              
   .build();

其他对话框指令辅助方法是

addElicitSlotDirective(String slotName, Intent updatedIntent)
addConfirmSlotDirective(String slotName, Intent updatedIntent)
addConfirmIntentDirective(Intent updatedIntent)

检查此ResponseBuilder.java