我正在寻找一个选项,如何将我的Alexa请求处理程序引入另一个模块。这是我的方法:
//index.js
var Alexa = require('ask-sdk-core');
var tests = require('./secondModule');
var LaunchRequestHandler = tests('LaunchRequest','Hello, is this working?')
var skillBuilder = Alexa.SkillBuilders.custom();
exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler
)
.lambda();
第二个模块如下:
//secondModule.js
var Alexa = require('ask-sdk-core');
function Test(requestName, speechText){
var request = requestName+"Handler"
console.log("log: " +request)
request = {
canHandle(handlerInput) {
console.log("log: "+request)
return handlerInput.requestEnvelope.request.type === requestName;
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
},
};
}
module.exports = Test
但是,如果我这样尝试,错误'“ errorMessage”:“无法读取未定义的属性'canHandle'”。您可能有一个想法,我怎么做?我对Node和JavaScript相当陌生
这是我的package.json:
{
"name": "hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"actions-on-google": "^2.5.0",
"ask-sdk-core": "^2.0.0",
"ask-sdk-model": "^1.0.0"
}
}
答案 0 :(得分:0)
您没有返回request
。将您的Test
函数更改为此:
function Test(requestName, speechText){
var request = requestName+"Handler"
console.log("log: " +request)
return request = {
canHandle(handlerInput) {
console.log("log: "+request)
return handlerInput.requestEnvelope.request.type === requestName;
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Hello World', speechText)
.getResponse();
},
};
}
现在应该可以使用!