Amazon Alexa Skill

2020-05-06 11:31发布

问题:

I want to ask alexa different sorts of questions and then at the end I want it should ask "Is there anything else you would like to know?" and when I say yes (where yes is working suggestion) it should suggest me according to the intent I am in. Like if I am in

IncityIntent:

    'InCityIntent': function () {
        speechOutput = '';


speechOutput = "The atmosphere in the city is beautiful. Is there anything else you would like to know";
        this.emit(":ask", speechOutput, speechOutput);


'YesIntent': function () {
        speechOutput = '';
/*when the user say yes, he should get this output*/  
            speechOutput = You can learn more about city by trying, alexa what are the best places in the city";
            this.emit(":tell",speechOutput, speechOutput);

FoodIntent:

    'FoodIntent': function () {
        speechOutput = '';


speechOutput = "Food in the city is delicious. Is there anything else you would like to know";
        this.emit(":ask", speechOutput, speechOutput);

'YesIntent': function () {
        speechOutput = '';
/*change in response here*/
            speechOutput = You can learn more about food by trying, alexa what are the best restaurants in the city";
            this.emit(":tell",speechOutput, speechOutput);

回答1:

First thing, do not create custom YesIntent and NoIntent, instead use AMAZON.YesIntent and AMAZON.NoIntent. You can always add utterences to these predefined intents if you want to.

Your issues can be solved in a couple of ways.

Using sessionAttributes:
Add a previousIntent attribute or something to keep a track of the converstation in the sessionAttributes when you receive the initial request, say InCityIntent. And in your AMAZON.YesIntent or AMAZON.NoIntent handler check for the previous intent and reply accordingly.

 'InCityIntent': function () {
       const speechOutput = "The atmosphere in the city is beautiful. Is there anything else you would like to know";
       const reprompt = "Is there anything else you would like to know";
       this.attributes['previousIntent'] = "InCityIntent";
       this.emit(":ask", speechOutput, reprompt);
  }

 'Amazon.YesIntent': function () {
     var speechOutput =  "";
     var reprompt = "";
     if (this.attributes 
        && this.attributes.previousIntent
        && this.attributes.previousIntent === 'InCityIntent' ) {
        speechOutput = "You can learn more about city by trying, Alexa what are the best places in the city";
        reprompt = "your reprompt";
     } else if ( //check for FoodIntent ) {

       // do accordingly
     }
     this.attributes['previousIntent'] = "Amazon.YesIntent";
     this.emit(":ask", speechOutput, reprompt);
  }

Using STATE handlers
ask-nodejs-sdk v1 has state handlers to generate response based on states. The idea is similar, the sdk will add the sessionAttribute parameter for you and the when will automatically map the handler with respect to that state.

 'InCityIntent': function () {
       const speechOutput = "The atmosphere in the city is beautiful. Is there anything else you would like to know";
       const reprompt = "Is there anything else you would like to know";
       this.handler.state = "ANYTHING_ELSE";
       this.emit(":ask", speechOutput, reprompt);
  }

const stateHandlers = Alexa.CreateStateHandler("ANYTHING_ELSE", {
    "AMAZON.YesIntent": function () {
       var speechOutput =  "You can learn more about city by trying, Alexa what are the best places in the city";
       var reprompt = "your reprompt";
       this.emit(":ask", speechOutput, reprompt);
    },

Once a state is set, then next time the the intent handlers defined in that particular state handler will be triggered. Change your state accordingly and once done, remove it.