【问题标题】:How do you completely end and exit a skill?你如何完全结束和退出技能?
【发布时间】:2018-09-22 11:08:37
【问题描述】:
我有一项 Alexa 技能,可以根据请求播放声音文件,然后播放消息并在用户说停止时停止播放。我在停止意图中使用了结束会话语句。但是,在说停止一次之后,如果您再次说 Alexa 停止,它会再次播放消息,告诉我该技能仍然处于活动状态。怎么下命令完全退出技能?
这是我目前的停止意图:
'AMAZON.StopIntent': function() {
//output to available screen
makeTemplate.call(this, 'stop');
this.response.speak('Ok. I sent a practice tip to your Alexa app.').audioPlayerStop();
this.emit(':responseReady');
this.response.shouldEndSession(true);
},
【问题讨论】:
标签:
alexa
alexa-skills-kit
alexa-skill
【解决方案1】:
您可以利用 Alexa 中的状态机,我建议您使用不同的状态,除了默认的常规状态外,还有 StopIntent。在这种情况下,您可以在播放声音时切换到此状态,然后您的特定停止行为才会起作用,您可以返回到常规状态,该状态不会对您的技能有默认行为,因此它将运行默认来自 Alexa 本身的一个关闭你的技能。
在这段代码中,您可以对它的工作原理有一个基本的了解,尽管可以肯定缺少一些东西,但重要的是 this.handler.state 控制当前会话处于什么状态,以及 @987654322 @ 它获取特定状态的名称和该状态意图的特定行为作为参数。
const Alexa = require('alexa-sdk');
const defaultHandlers = {
PlayIntent: function() {
// move to state 'PLAY'
this.handler.state = 'PLAY'
// Code to play
}
}
const playingHanders = Alexa.CreateStateHandler('PLAY', {
PlayIntent: function() {
// Code to play
},
'AMAZON.StopIntent': function() {
//output to available screen
makeTemplate.call(this, 'stop');
// move to default state
this.handler.state = ''
this.response.speak('Ok. I sent a practice tip to your Alexa app.').audioPlayerStop();
this.emit(':responseReady');
this.response.shouldEndSession(true);
}
})
module.exports.skill = (event, context, callback) => {
const alexa = Alexa.handler(event, context, callback);
alexa.appId = APP_ID
alexa.registerHandlers(defaultHandlers, playingHanders)
alexa.execute();
}
互联网上有很多关于此的教程,因此您可以找到更好的想法来了解如何利用它。