Alexa Skill:处理ShouldEndSession以保持会话开放

时间:2018-05-17 16:42:20

标签: javascript node.js amazon-web-services alexa aws-sdk-nodejs

拥有Alexa技能,可以读出随机引用。会话以“shouldEndSession”结尾:false。

如何保持会话开放并询问用户是否想要听到其他报价?触发YesIntent。我正在使用':askWithCard',它会使会话保持打开但不会触发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();
};

尝试了RandomQuoteIntent中的response.shouldEndSession(false, "would you like to hear another quote ");但会话在读出第一个引用后关闭了

1 个答案:

答案 0 :(得分:0)

有一种愚蠢的方法:在提示问题后使用静音音频流。由于每个音频不应超过90秒,因此最多可以有大约180秒的窗口等待用户的下一次输入。

唯一需要注意的是,用户必须在实际命令之前中断音频,因此需要额外的“Alexa”或您选择的任何调用名称。

Exact code and steps here