Amazon Alexa Skill

拥有回忆 提交于 2019-12-02 12:10:12

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!