1
votes

This Alexa skill provides information about paperback or online books.This is just an example, not the real thing. So, here's an example User: "Alexa, find the best book with one author." Alexa: "OK!, here's the book blah blah." Alexa: "Would you like to hear this again?" User: "Yes!" Alexa: repeat the same information. I want to know how I can implement the code to make Alexa answer to that yes/no question and repeat what was said. I've done an extensive research about this but I still can't understand. Btw I'm new to this.

'booksIntent': function () {
        var speechOutput = '';
        var speechReprompt = '';

        var sourceSlot = resolveCanonical(this.event.request.intent.slots.source);
        console.log('User requested source: ' + sourceSlot);

        var authorSlot = resolveCanonical(this.event.request.intent.slots.author);
        console.log('User requested author: ' + authorSlot);

        var sources = {
            'basic book' : {
                'one author' : "Blah blah one author",

                'two authors' : "Blah blah two authors",

                'multiple authors' : "blah blah multiple authors"
            },

            'basic electronic' : {
                'one author' : "Blah blah...some electronic information"
            },

        }

         var authors = [
            'one author',
            'two authors',
            'multiple authors'
            ];

        var source =sourceSlot.toLowerCase();

        if(sourceSlot && sources[source]){
            var standardSource = '';
            var author = 'one author'; //default author choice

            if(authorSlot && author.indexOf(authorSlot) - 1){
                author = authorSlot;
            }

            var getSource = sources[source][author];

                speechOutput = 'Ok! ' + getSource;
                speechReprompt= 'Would you like to hear this again?'; //I want the user to answer with a yes or no and the program to respond accordingly. 


        }
            else{
                speechOutput = 'Sorry, the information you asked is not supported yet'
                speechReprompt = 'I support: this and that!'
            }
            this.emit(":ask", speechOutput, speechReprompt)

    },
1
How did you solve this in the end?icelic

1 Answers

1
votes

First, you should update the version of Alexa SDK.

You can solve Yes, No intent by using state.

For Example, before return speech out, you can store the state where to go next.

Using attributes to store states and everything.

attributes.state ="YES_MODE"

In Yes Intent,

you should check, is that state matching or not then allow to do the operation.

Here is the code:

attributes.data = "You can store the data. What you want to repeat"
attributes.state = "YES_MODE"

return handlerInput.responseBuilder
  .speak(speechText)
  .reprompt(repromptText)
  .getResponse();

In Yes Intent:

 const YesIntentHandler = {
 canHandle(handlerInput) {
const attributes = handlerInput.attributesManager.getSessionAttributes();
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
  && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.YesIntent'
  && attributes.state === "YES_MODE";
},
handle(handlerInput) {
const attributes = handlerInput.attributesManager.getSessionAttributes();
let data = attributes.data

//write your logic 

return handlerInput.responseBuilder
  .speak(speechText)
  .reprompt(repromptText)
  .getResponse();
},
};