无法使助手函数在AWS lambda nodejs中工作



我是nodejs和学习的新手,但不知道为什么我的助手函数不起作用。本质上,这是通常工作的示例alexa lambda函数的一部分。如果我将MQTT代码留在Intent处理程序中,MQTT操作就可以工作,但我需要将它移到代码主体中,这样我就可以从其他代码函数调用MQTT操作。

这个代码段中有几个"测试"函数无法工作,可能是因为我不知道将代码从Intent函数中移出的正确方法。

我对处理人员也很不清楚。。(实际上是多个处理程序(代码片段中有两个处理程序。。这不会造成问题,但我希望有两个lambda触发器(ask-sdk&smart-home(,每个触发器都调用自己的处理程序——不确定这是否可能。


var APP_ID = "amzn1.ask.skill.xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // input the axela skill ID
var AWS = require('aws-sdk');
var Alexa = require("alexa-sdk");
AWS.config.region = "us-east-1";
var iotData = new AWS.IotData({endpoint: "xxxxxxxxxxx.iot.us-east-1.amazonaws.com"}); // input the AWS thing end point
var topic = "esp32/sub"; //input the topic that the device is subscribed to
// Handler for Generic Event handling accepts both SmartHomeand ask-sdk events
// But only works when the handler below is removed.
exports.handler = async function (event, context) {
// Dump the request for logging - check the CloudWatch logs
console.log("index.handler request  -----");
console.log(JSON.stringify(event));
if (context !== undefined) {
console.log("index.handler context  -----");
console.log(JSON.stringify(context));
}
switchon(); // test call of standalone MQTTfunction ( doesn't work)
};
// Remove this function and the Smarthome Test works.
// But is needed for the ask-sdk events ( Smarthome events fail )
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
console.log("index.handler comment  -----");   
};

//*********************************
//   Helper code examples to functionalise the MQTT switch on
//   NONE OF THESE WORK WHEN CALLED
function switchon3(){
var dataObj = {
topic: topic,
payload: "on",
qos:0
};
iotData.publish(dataObj, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else     console.log(data);           // successful response
});
}
function switchon (error, data){
var params = {
topic: topic,
payload: "on",
qos:0
};
iotData.publish(params, (error, data)=>{
if (!error){this.emit(':tell', 'Robert, well done its Switched On');
}else{this.emit(':tell', 'Oh dear MQTT returned a switch on error')}
});
}
// End of helper examples
//*********************************
//********* THE PROPER CODE ************************ 
var handlers = {

'LaunchRequest': function () {
this.emit(':tell', 'Hello. Skill four here. How may I help you?');
},
'SwitchOnIntent': function () {
//    None of the example function calls work here       
//        switchon3();
//        this.emit(':tell', 'Test Switch On');   // needs this line to work     

// The following original example code DOES work      
var params = {
topic: topic,
payload: "on",
qos:0
};
iotData.publish(params, (error, data)=>{
if (!error){this.emit(':tell', 'Robert, well done its Switched On');
}else{this.emit(':tell', 'Oh dear MQTT returned a switch on error')}
});
},

已编辑。。。

不,汤米,这不太基本,谢谢你的帮助。实际上,我正试图让lambda接受来自两个AWS触发器的输入。1.来自定制技能的ASK-API2.智能家居触发器。我不确定这两个触发器是否需要单独的处理程序函数,或者,如果正如我所怀疑的那样,使用智能家居触发器可以避免使用以某种方式调用已注册Intent函数的ask-api方法,到达的json的格式显然与这两种触发器类型不同,我很感激可以在lambda中手动完成所有alexa自定义技能解析。那么我的问题是。。如果从自定义技能开始,那么如果我添加了一个智能家居触发器,那么用ask-api注册所有函数调用将变为无效,因为处理ask-api事件的一个处理程序不能同时处理智能家居指令。

在对其进行排序之后,尝试"调出"MQTT调用,该调用在最初编码的Intent函数中工作,但如果我尝试将它们放入单独的函数调用中,则会失败。请耐心等待。。。我知道我想做什么…只是还不太懂这种语言。

我认为您在这里没有理解的是您实际上覆盖了同一个变量。

exports是一个对象(变量(,它可以具有多个属性。如果这太基本了,请原谅,但一个属性基本上是一个附加到另一个变量的变量。

在代码中,首先将此属性的值赋给一个函数。

exports.handler = async function (event, context) {
// Dump the request for logging - check the CloudWatch logs
console.log("index.handler request  -----");
console.log(JSON.stringify(event));
if (context !== undefined) {
console.log("index.handler context  -----");
console.log(JSON.stringify(context));
}
switchon(); // test call of standalone MQTTfunction ( doesn't work)
};

所以如果你运行exports.handler(),它就会运行这个函数。但是,您可以将此变量向下重新分配几行。

所以现在是这样的:

exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
console.log("index.handler comment  -----");   
};

您正在用第二个函数替换第一个函数,这就是为什么注释掉exports.handler的第二个赋值会导致第一个位工作的原因。我不是100%清楚你在问什么,但如果可以的话,你要么需要将两个函数的内容组合起来(或者有一个处理程序来检查事件并调用一个单独的函数(,要么将它们移动到单独的lambda中。

例如:

exports.handler = function(event,context,callback) {
if(event.EventType === "YourGenericEvent") { // replace YourGenericEvent with whatever the eventName is for the first function
genericEvent(event,context)
} else if(event.EventType === "SecondEvent") { // again replace "SecondEvent" with whatever the event is for your second function
secondEvent(event,context,callback)
}
}
function genericEvent (event, context) {
// Dump the request for logging - check the CloudWatch logs
console.log("index.handler request  -----");
console.log(JSON.stringify(event));
if (context !== undefined) {
console.log("index.handler context  -----");
console.log(JSON.stringify(context));
}
switchon(); // test call of standalone MQTTfunction ( doesn't work)
};
function secondEvent(event,context,callback) {
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
console.log("index.handler comment  -----");   
}

希望您的console.log(event)语句能够指示IF语句的EventType属性的值。

你可以在这里看到另一篇与Python相关的文章

如何在AWS Lambda函数中拥有多个处理程序?

最新更新