简体   繁体   中英

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. 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).

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.

Yes it is possible to skip 1 or more slot values.

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.

 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. 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 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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