简体   繁体   中英

Alexa Skill: Handling ShouldEndSession to keep session open

Have an Alexa skill, that reads out a random quote. The session ends with "shouldEndSession": false.

How do I keep the session open and ask the user if they want to hear another quote? which triggers the YesIntent. I am using the ':askWithCard'which leaves the session open but does not trigger the YesIntent

const handlers = {
  'LaunchRequest': function () {
      this.emit('Introduction');

  },
  'Introduction': function() {
    console.log("Introduction Handler called.");
    var speechOutput =  "Just say give me an office space quote";
    var repromptText = "I did not recieve any input. Thank you and good bye";
    this.emit(':askWithCard', speechOutput, repromptText);
  },

  'RandomQuoteIntent': function() {
    const quoteOfficeSpace = data;
    const quoteIndex = Math.floor(Math.random() * quoteOfficeSpace.length);
    const randomQuote = quoteOfficeSpace[quoteIndex];
    const speechOutput = randomQuote;

    this.response.cardRenderer(SKILL_NAME);

    this.response.speak(speechOutput);
    this.emit(':responseReady');


  },
   'AMAZON.YesIntent': function() {
    this.emit(':RandomQuoteIntent');
  },
  'AMAZON.HelpIntent': function () {
    const speechOutput = HELP_MESSAGE;
    const reprompt = HELP_REPROMPT;
    this.response.speak(speechOutput).listen(reprompt);
    this.emit(':responseReady');
  },
  'AMAZON.CancelIntent': function () {
      this.emit(':tell', "Okay! Goodbye, just don't forget to fill out your TPS reports");
  },
  'AMAZON.StopIntent': function () {
      this.emit(':tell', "Goodbye ");
  },
  'Unhandled': function () {
    console.log("Unhandled Intent Handler Called.");
    this.emit(':tell', "Sorry, I am unable to understand what you said. just say give me an office space quote");
  },

};


exports.handler = function(event, context, callback) {
  var alexa = Alexa.handler(event, context);
  alexa.appId = APP_ID;
  alexa.registerHandlers(handlers);
  alexa.execute();
};

tried the response.shouldEndSession(false, "would you like to hear another quote "); in the RandomQuoteIntent but the session was closed after it read out the first quote

There is a hacky way to do it: use silent audio streaming after prompting the question. Since each audio should no longer than 90 seconds, you could have a maximum of roughly 180 seconds window to wait for user's next input.

The only caveats is that the user will have to interrupt the audio, thus an extra "Alexa" or any invocation name you choose, before the actual command.

Exact code and steps here

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