【问题标题】:How do I use Alexa's Dialogue Directives in Java?如何在 Java 中使用 Alexa 的对话指令?
【发布时间】:2018-09-17 21:57:05
【问题描述】:

我在网上几乎找不到与此主题相关的任何信息。 我不知道如何访问意图,或者使用它们来返回响应,因为某些参数符合该意图。我试图创建一个简单的对话,

我:“添加一个单位”

Alexa:“单位应该叫什么?”

我:“工程”

Alexa:“好的,添加了工程单元。”

目前我所知道的只是在调用该技能后执行一个动作,而不管说什么。例如,我可以直接说,

我:“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();
}
}

【问题讨论】:

    标签: java alexa alexa-skills-kit


    【解决方案1】:

    您应该使用关联处理程序类的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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多