我不确定是否是因为我正在alexa开发人员控制台中进行测试,但看来会话在每次意图之后都重新启动。
在下面的代码中,如果我调用SetMyVarA
,它将写出正确的值给cloudwatch(或在使用无服务器时使用终端),但是如果我随后立即调用SetMyVarB
,则我将得到“嗯,我不知道那个”(在本地跑步只会给我undefined
作为值)。
我也尝试按照此问题中的建议进行操作,但似乎没有效果:"Usage with Passport"
/*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}`);
},
};
答案 0 :(得分:0)
在SetMyVar
中,如果您使用speak()
,然后emit
和responseReady
,则会话默认关闭,因此当您尝试呼叫时,您已经不在会话中您的第二个意图。
如果您想做与SetMyVarA
中相同的操作,但又不想立即关闭会话,则需要将this.response.shouldEndSession
设置为false
。 Alexa Dev Console可以毫无问题地处理技能课程,因此不必担心。
顺便说一句,您正在使用的ASK v1已过时。请像这样切换到v2代码: