Alexa 技能忘记了意图之间的会话
Alexa skill forgets session between intents
我不确定是不是因为我正在 alexa 开发者控制台中进行测试,但似乎会话在每个意图后都重新启动。
在下面的代码中,如果我调用 SetMyVarA
,它将向 cloudwatch(或使用无服务器时的终端)写出正确的值,但是如果我随后立即调用 SetMyVarB
,那么我会得到 "Hmm, I don't know that one"(运行 本地只会给我 undefined
作为值)。
我也尝试按照这个问题中的建议进行操作,但似乎没有效果:
/*jslint es6 */
"use strict";
const Alexa = require(`alexa-sdk`);
module.exports.handler = (event, context, callback) => {
console.log(`handler: ${JSON.stringify(event.request)}`);
const alexa = Alexa.handler(event, context, callback);
alexa.appId = process.env.APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
"LaunchRequest": function() {
console.log(`LaunchRequest`);
this.emit(`AMAZON.HelpIntent`);
},
"SetMyVarA": function() {
console.log(`Set MyVarA`);
var myVarA = this.event.session.attributes.myVarA = this.event.request.intent.slots.myVarA.value;
console.log(`MyVarA is ${myVarA}.`);
var speechOutput = `MyVarA has been set to ` + myVarA + `.`;
this.response.speak(speechOutput);
this.emit(`:responseReady`);
},
"SetMyVarB": function() {
console.log(`Set MyVarB`);
var myVarB = this.event.session.attributes.myVarB = this.event.request.intent.slots.myVarB.value;
console.log(`MyVarB is ${myVarB}.`);
var myVarA = this.event.session.attributes.myVarA
console.log(`MyVarA is ${myVarA}.`);
var speechOutput = {
"type": `SSML`,
"ssml": `<speak>MyVarB is ` + myVarB + `.</speak>`,
};
this.response.speak(speechOutput);
this.emit(`:responseReady`);
},
"AMAZON.HelpIntent": function() {
var speechOutput = `This is a skill.`;
var reprompt = `Help here.`;
speechOutput = speechOutput + reprompt;
this.response.speak(speechOutput)
.listen(reprompt);
this.emit(`:responseReady`);
},
"AMAZON.CancelIntent": function() {
},
"AMAZON.StopIntent": function() {
},
"AMAZON.RepeatIntent": function() {
console.log(`RepeatIntent`);
this.emit(`LaunchRequest`);
},
"Unhandled": function() {
// handle any intent in interaction model with no handler code
console.log(`Unhandled`);
this.emit(`LaunchRequest`);
},
"SessionEndedRequest": function() {
// "exit", timeout or error. Cannot send back a response
console.log(`Session ended: ${this.event.request.reason}`);
},
};
在 SetMyVar
中,如果您使用 speak()
然后 emit
一个 responseReady
会话默认关闭,所以当您尝试时已经退出了调用你的第二个意图。
如果您想做与在 SetMyVarA
中完全相同的事情但不立即关闭会话,您需要将 this.response.shouldEndSession
设置为 false
。 Alexa Dev Console 可以毫无问题地处理技能会话,所以不用担心。
顺便说一下,您使用的 ASK v1 已经过时了。请像这样切换到 v2 代码:
我不确定是不是因为我正在 alexa 开发者控制台中进行测试,但似乎会话在每个意图后都重新启动。
在下面的代码中,如果我调用 SetMyVarA
,它将向 cloudwatch(或使用无服务器时的终端)写出正确的值,但是如果我随后立即调用 SetMyVarB
,那么我会得到 "Hmm, I don't know that one"(运行 本地只会给我 undefined
作为值)。
我也尝试按照这个问题中的建议进行操作,但似乎没有效果:
/*jslint es6 */
"use strict";
const Alexa = require(`alexa-sdk`);
module.exports.handler = (event, context, callback) => {
console.log(`handler: ${JSON.stringify(event.request)}`);
const alexa = Alexa.handler(event, context, callback);
alexa.appId = process.env.APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
"LaunchRequest": function() {
console.log(`LaunchRequest`);
this.emit(`AMAZON.HelpIntent`);
},
"SetMyVarA": function() {
console.log(`Set MyVarA`);
var myVarA = this.event.session.attributes.myVarA = this.event.request.intent.slots.myVarA.value;
console.log(`MyVarA is ${myVarA}.`);
var speechOutput = `MyVarA has been set to ` + myVarA + `.`;
this.response.speak(speechOutput);
this.emit(`:responseReady`);
},
"SetMyVarB": function() {
console.log(`Set MyVarB`);
var myVarB = this.event.session.attributes.myVarB = this.event.request.intent.slots.myVarB.value;
console.log(`MyVarB is ${myVarB}.`);
var myVarA = this.event.session.attributes.myVarA
console.log(`MyVarA is ${myVarA}.`);
var speechOutput = {
"type": `SSML`,
"ssml": `<speak>MyVarB is ` + myVarB + `.</speak>`,
};
this.response.speak(speechOutput);
this.emit(`:responseReady`);
},
"AMAZON.HelpIntent": function() {
var speechOutput = `This is a skill.`;
var reprompt = `Help here.`;
speechOutput = speechOutput + reprompt;
this.response.speak(speechOutput)
.listen(reprompt);
this.emit(`:responseReady`);
},
"AMAZON.CancelIntent": function() {
},
"AMAZON.StopIntent": function() {
},
"AMAZON.RepeatIntent": function() {
console.log(`RepeatIntent`);
this.emit(`LaunchRequest`);
},
"Unhandled": function() {
// handle any intent in interaction model with no handler code
console.log(`Unhandled`);
this.emit(`LaunchRequest`);
},
"SessionEndedRequest": function() {
// "exit", timeout or error. Cannot send back a response
console.log(`Session ended: ${this.event.request.reason}`);
},
};
在 SetMyVar
中,如果您使用 speak()
然后 emit
一个 responseReady
会话默认关闭,所以当您尝试时已经退出了调用你的第二个意图。
如果您想做与在 SetMyVarA
中完全相同的事情但不立即关闭会话,您需要将 this.response.shouldEndSession
设置为 false
。 Alexa Dev Console 可以毫无问题地处理技能会话,所以不用担心。
顺便说一下,您使用的 ASK v1 已经过时了。请像这样切换到 v2 代码: