首页 > 解决方案 > Alexa跳过插槽或以编程方式设置它?

问题描述

我正在研究一项 Alexa 技能,该技能基本上是一个测验,其中 Alexa 连续向用户询问多个问题,主题根据存储在发电机表中的用户状态而变化。这行得通。我这样做的目的是为每个答案都有一个槽,我使用对话管理来引发每个响应,直到它们都被填满。以下是其中的一些代码:

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

问题是我需要创建一个需要可变数量问题的测验,但模型中定义了插槽,因此我无法更改完成意图所需的答案数量。我认为这样做的方法是提供一些任意数量的answer插槽并将默认值分配给我不需要的插槽(因此,如果测验有 3 个问题,但有 5 个插槽,最后 2 个插槽将被分配占位符值)。

我怎样才能做到这一点?有没有办法以编程方式设置插槽值?

这篇 Alexa 博客文章似乎描述了我的需求,但不幸的是它是使用 ASK SDK v1 编写的,所以我不确定如何使用 v2 来完成它。

标签: javascriptnode.jsalexaalexa-skills-kitalexa-skill

解决方案


是的,可以跳过 1 个或多个插槽值。

我可以想到两种解决您问题的方法。

1) 使用addDelegateDirective而不是addElicitSlotDirective来收集槽值,并在dialogState为 ' STARTED '时使用任意值填充不需要的槽,如以下代码段。

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)在第二个解决方案中,您可以使用会话变量来跟踪要引出的插槽数。喜欢

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
}


推荐阅读