Alexa Skill:配置ShouldEndSession维持会话并触发YesIntent
解决Alexa Skill维持会话并触发YesIntent的问题
我看了你的代码,发现两个关键问题导致会话无法维持、YesIntent也触发不了,咱们一步步来修复:
1. 先修正AMAZON.YesIntent的调用错误
你当前的YesIntent里写的是this.emit(':RandomQuoteIntent');,这里多了个冒号!emit方法需要传入的是handler的名称字符串,不是Alexa的指令格式,所以得把冒号去掉:
'AMAZON.YesIntent': function() { this.emit('RandomQuoteIntent'); // 去掉前面的冒号 },
2. 在RandomQuoteIntent中正确维持会话并添加询问提示
你的RandomQuoteIntent读完名言后直接结束了会话,没有告诉Alexa要保持会话开启并等待用户回应。之前尝试的response.shouldEndSession(false, "...")用法不对,正确的做法是用listen()方法添加后续提示,或者直接用:askWithCard指令(和你在Introduction里用的一样)。
方法一:用response.speak().listen()
修改RandomQuoteIntent如下:
'RandomQuoteIntent': function() { const quoteOfficeSpace = data; const quoteIndex = Math.floor(Math.random() * quoteOfficeSpace.length); const randomQuote = quoteOfficeSpace[quoteIndex]; const speechOutput = randomQuote; const repromptText = "Would you like to hear another quote?"; // 添加询问提示 this.response.cardRenderer(SKILL_NAME); this.response.speak(speechOutput).listen(repromptText); // 先读名言,再等待用户回应 this.emit(':responseReady'); },
方法二:直接用:askWithCard指令
如果你想同时保留卡片和维持会话,也可以用:askWithCard,这样更简洁:
'RandomQuoteIntent': function() { const quoteOfficeSpace = data; const quoteIndex = Math.floor(Math.random() * quoteOfficeSpace.length); const randomQuote = quoteOfficeSpace[quoteIndex]; const speechOutput = randomQuote; const repromptText = "Would you like to hear another quote?"; this.emit(':askWithCard', speechOutput, repromptText, SKILL_NAME, randomQuote); },
为什么之前的方法无效?
- 你之前在RandomQuoteIntent里没有添加
listen()或者用:ask类指令,Alexa读完名言后默认会结束会话,所以用户说"Yes"的时候已经没有会话在运行了,自然触发不了YesIntent。 - 直接设置
shouldEndSession(false)的同时,必须提供reprompt内容,而response.shouldEndSession()的第二个参数并不是用来设置reprompt的,正确的方式是通过listen()方法来添加提示语,这样Alexa才会保持会话开启状态。
把这两处修改完成后,你的Skill就能读完名言后保持会话,用户说"Yes"时也能正确触发RandomQuoteIntent获取新的名言了。
内容的提问来源于stack exchange,提问作者Neil
相关产品推荐
相关产品推荐

