如何使用Java创建多语言Alexa技能?

时间:2017-10-13 08:04:56

标签: java eclipse amazon-web-services aws-lambda alexa-voice-service

我对VUI和Alexa很新。我为Alexa语音服务开发了一个很好的工作技能。现在我想添加第二种语言。我在 the develop documation找到了一个教程,但我正在使用Eclipse和Java来创建我的Lambda函数,如walkthrough。 问题是,我不知道如何在Lambda函数中启用第二语言选项。我必须对两种语言使用相同的Lambda函数。

我的StreamRequestHandler:

public class ApiOmatBlogSpeechletStreamRequestHandler extends SpeechletRequestStreamHandler {
private static final Set<String> supportedApplicationIds = new HashSet<String>();
static {
    /*
     * This Id can be found on https://developer.amazon.com/edw/home.html#/ "Edit" the relevant
     * Alexa Skill and put the relevant Application Ids in this Set.
     */
    supportedApplicationIds.add("amzn1.ask.skill.xxxxxxxx");
}

public ApiOmatBlogSpeechletStreamRequestHandler() {
    super(new ApiOmatBlogSkillSpeechlet(), supportedApplicationIds);
    System.out.println("Super ApiOmatBlogSpeechletStreamRequestHandler");
}

}

My Spechlet:

public SpeechletResponse onIntent(IntentRequest intentRequest, Session session) {
    Intent intent = intentRequest.getIntent();
    String intentName = (intent != null) ? intent.getName() : null;
    System.out.println("onIntent requestId={ " + intentRequest.getRequestId() + " }, sessionId={ "
            + session.getSessionId() + " } ");

    Integer step = (Integer) session.getAttribute("step");
    System.out.println("IntentName= " + intentName + " | step = " + step);

    if ("AMAZON.HelpIntent".equals(intentName)) {
        return getHelpResponse();
    } else if ("AMAZON.StopIntent".equals(intentName)) {
        return getStopResponse();
    } else if (step != null) {
        return testing(intent, session, step);
    } else {
        if ("TestIntent".equals(intentName)) {
            step = 1;
            session.setAttribute("step", step);
            return testing(intent, session, step);
        } else {
            SsmlOutputSpeech speechText = new SsmlOutputSpeech();

            speechText.setSsml("<speak> " + "The intent is invalid." + "Please repeat your demand. "
                    + "<break time='0.5s'/> " + "  </speak>");

            // Create reprompt
            PlainTextOutputSpeech speech2 = new PlainTextOutputSpeech();
            speech2.setText("I'm sorry. Please repeat your statement.");
            Reprompt reprompt = new Reprompt();
            reprompt.setOutputSpeech(speech2);

            return SpeechletResponse.newAskResponse(speechText, reprompt);
        }
    }
}

/**
 * This function will be called if you say 'start'
 */
public SpeechletResponse onLaunch(final LaunchRequest request, final Session session) throws SpeechletException {
    System.out.println(
            "onLaunch requestId={ " + request.getRequestId() + " }, sessionId={ " + session.getSessionId() + " } ");

    return getHelpResponse();
}

谢谢

2 个答案:

答案 0 :(得分:1)

很难解决这个问题。 我通过询问用户说的语言来解决它。

在Spechlet.java中我添加了代码:

private Boolean german;
    public SpeechletResponse onIntent(IntentRequest intentRequest, Session session) {
        if (intentRequest.getLocale().getLanguage().equals(new Locale("de").getLanguage())){
            System.out.println("Spoken language == Deutsch");
            german=true;
        }else{
            System.out.println("Spoken language == Englisch");
            german=false;
        }
...
}

然后我可以使用布尔德语并设置我的回复。

如果有一个更好的解决方案,它适用于两种语言,那么我期待发布的答案或评论。但目前这是在Java中处理多种语言的最佳解决方法。

答案 1 :(得分:0)

您不需要不同的Lambda逻辑来支持每种语言。您可以根据区域创建多个Lambda函数以避免延迟。如果选择将代码部署到多个端点,则必须在所有端点上维护相同的代码。代码必须处理您支持的所有语言。您需要做的唯一设置是在Alexa技能信息中添加Alexa支持的语言,该信息标记如下,

enter image description here

请阅读官方文档here