简体   繁体   English

Alexa跳过插槽还是以编程方式设置?

[英]Alexa skip slot or set it programmatically?

I'm working on an Alexa skill that's basically a quiz where Alexa asks the user multiple questions in succession with the topic varying based on user state stored in a dynamo table. 我正在研究一种Alexa技能,该技能基本上是一个测验,其中Alexa连续询问用户多个问题,主题随发电机表中存储的用户状态而变化。 This works. 这可行。 I'm accomplishing this with an intent that has a slot for each answer and I use dialog management to elicit each response until they're all filled. 我的目的是为每个答案提供一个插槽,并且使用对话框管理来引发每个响应,直到所有答案都被填满。 Here is some of the code for that: 这是一些代码:

if(!answers.NewWordSpanishAnswer) {
  const newWordIntroAudio = sound('intro');
  const promptAudio = sound(`new-word-${word}-spanish-intro`);
  return handlerInput.responseBuilder
    .speak(newWordIntroAudio + promptAudio)
    .reprompt(promptAudio)
    .addElicitSlotDirective('NewWordSpanishAnswer')
    .getResponse();
}

if(!answers.NewWordEnglishAnswer) {
  const responseAudio = sound(`new-word-${word}-spanish-correct`);
  const promptAudio = sound(`new-word-${word}-english-intro`);
  return handlerInput.responseBuilder
    .speak(responseAudio + promptAudio)
    .reprompt(promptAudio)
    .addElicitSlotDirective('NewWordEnglishAnswer')
    .getResponse();
}

// etc. repeat for each question

The problem is I need to create a quiz that needs a variable number of questions, but slots are defined in the model, so I can't change the number of answers required to complete the intent. 问题是我需要创建一个需要可变数量问题的测验,但是插槽是在模型中定义的,因此我无法更改完成意图所需的答案数量。 I think the way to do this is to provide some arbitrary number of answer slots and assign default values to the ones I don't need (so if the quiz has 3 questions, but there are 5 slots, the last 2 slots would be assigned placeholder values). 我认为执行此操作的方法是提供任意数量的answer插槽,并将默认值分配给我不需要的answer插槽(因此,如果测验中有3个问题,但有5个插槽,则将分配最后2个插槽占位符值)。

How can I accomplish this? 我该怎么做? Is there a way to set slot values programmatically? 有没有办法以编程方式设置插槽值?

This Alexa blog post seems to describe what I need, but it's unfortunately written using the ASK SDK v1, so I'm not sure how to accomplish it using v2. 这个Alexa博客帖子似乎描述了我的需求,但是不幸的是,它是使用ASK SDK v1编写的,因此我不确定如何使用v2完成它。

Yes it is possible to skip 1 or more slot values. 是的,可以跳过1个或多个插槽值。

I can think of two solutions to your problem. 我可以想到两种解决您的问题的方法。

1) Use addDelegateDirective instead of addElicitSlotDirective to gather slot values and fill the slots that you don't need with some arbitrary value when dialogState is ' STARTED ' Like the Following snippet. 1)使用addDelegateDirective代替addElicitSlotDirective收集槽值和填充时dialogState是“ 已启动 ”像下面的代码片段你不一些任意值,需要的插槽。

 const { request } = handlerInput.requestEnvelope; const { intent } = request; if (request.dialogState === 'STARTED') { intent.slots.slotToSkip.value = 'skipped' return handlerInput.responseBuilder .addDelegateDirective(intent) .withShouldEndSession(false) .getResponse() } 

2) In 2nd solution you can use session variables to keep track of how many slots to elicit. 2)在第二个解决方案中,您可以使用会话变量来跟踪要引发的插槽数量。 Like 喜欢

 let sessionAttributes = handlerInput.attributesManager.getSessionAttributes(); sessionAttributes.count = 3 //Suppose you want to elicit 3 slots; handlerInput.attributesManager.setSessionAttributes(sessionAttributes); if (sessionAttributes.count >= 0) { //addElecitSlotDirective sessionAttributes.count = sessionAttributes.count--; handlerInput.attributesManager.setSessionAttributes(sessionAttributes); } else{ //here you will get the required number of slots } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM