我对这种方法的有用性有些怀疑,下面还有一些优化的潜力,但实现这一点的一种方法是声明
exports.handler
作为一个简单的包装器,它根据您可以在请求中测试的条件调用之前声明的正确处理程序函数。
// set up handler for alexa
const skillBuilder = Alexa.SkillBuilders.standard();
const alexa_handler = skillBuilder
.addRequestHandlers(
LaunchRequest,
HelpIntent,
// .... various other intents
UnhandledIntent
)
.addErrorHandlers(ErrorHandler)
.lambda();
// set up handler for API Gateway
const api_gateway_handler = function(event, context, callback) {
let name = "you";
let city = 'World';
// etc ... more code ...
callback(null, response);
};
// for each invocation, choose which of the above to invoke
exports.handler = function(event, context, callback) {
if(/* some test that is true only for API Gateway */)
{
api_gateway_handler(event, context, callback);
}
else
{
alexa_handler(event, context, callback);
}
};
线路
if(/* some test that is true only for API Gateway */)
if(event.input && event.input.requestContext && event.input.requestContext.apiId)
API网关文档
suggest
这个值总是存在的。