如何在Alexa的java SDK中使用Dialog指令

时间:2017-05-02 11:14:24

标签: java alexa-skills-kit alexa-skill

我正在尝试使用java技能工具包创建自己的Alexa技能,我想使用Dialog接口。我已经使用测试版的技能构建器创建了我的Dialog模型,但现在我不明白我需要通过我的webservice返回什么才能委派我的对话框。

我应该使用哪个类向Alexa发送一个命令来处理对话框中的下一个回合? 而且,我在IntentRequest类中没有dialogState属性......

2 个答案:

答案 0 :(得分:1)

首先,dialogState属性位于IntentRequest。我使用以下依赖项(maven)的1.3.1版。要使用yourIntentRequestObject.getDialogState()来获取值。

<dependency>
            <groupId>com.amazon.alexa</groupId>
            <artifactId>alexa-skills-kit</artifactId>
            <version>1.3.1</version>
</dependency>

下面您将看到Speechlet方法中onIntent的一些示例用法:

        if ("DoSomethingSpecialIntent".equals(intentName))
        {

            // If the IntentRequest dialog state is STARTED
            // This is where you can pre-fill slot values with defaults
            if (dialogueState == IntentRequest.DialogState.STARTED)
            {
                // 1.
                DialogIntent dialogIntent = new DialogIntent(intent);

                // 2.
                DelegateDirective dd = new DelegateDirective();
                dd.setUpdatedIntent(dialogIntent);

                List<Directive> directiveList = new ArrayList<Directive>();
                directiveList.add(dd);

                SpeechletResponse speechletResp = new SpeechletResponse();
                speechletResp.setDirectives(directiveList);
                // 3.
                speechletResp.setShouldEndSession(false);
                return speechletResp;
            }
            else if (dialogueState == IntentRequest.DialogState.COMPLETED)
            {
                String sampleSlotValue = intent.getSlot("sampleSlotName").getValue();
                String speechText = "found " + sampleSlotValue;

                // Create the Simple card content.
                SimpleCard card = new SimpleCard();
                card.setTitle("HelloWorld");
                card.setContent(speechText);

                // Create the plain text output.
                PlainTextOutputSpeech speech = new PlainTextOutputSpeech();
                speech.setText(speechText);

                return SpeechletResponse.newTellResponse(speech, card);
            }
            else
            {
                // This is executed when the dialog is in state e.g. IN_PROGESS. If there is only one slot this shouldn't be called
                DelegateDirective dd = new DelegateDirective();

                List<Directive> directiveList = new ArrayList<Directive>();
                directiveList.add(dd);

                SpeechletResponse speechletResp = new SpeechletResponse();
                speechletResp.setDirectives(directiveList);
                speechletResp.setShouldEndSession(false);
                return speechletResp;
            }
        }
  1. 制作新的DialogIntent
  2. 创建DelegateDirective并将其分配给updatedIntent属性
  3. shoulEndSession标记设置为false,否则Alexa会终止会话
  4. 在SkillBuilder中选择你的Intent,它需要至少有一个标记为必需的插槽。配置话语和提示。您也可以在提示中使用{slotNames}。

    -Sal

答案 1 :(得分:-1)

我想您可能希望在https://github.com/amzn/alexa-skills-kit-java/pull/45查看最新更新的工作示例。