无法让帮助函数在AWS的lambda nodejs中工作。

问题描述 投票:0回答:1

我是nodejs的新手,正在学习,但找不到为什么我的帮助函数不能工作.本质上,这是一个例子alexa lambda函数的一部分,一般都能工作.如果我把MQTTcode留在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')}
        });

    },

编辑了...

不,Tommy,这不是太基本的,谢谢你的帮助。实际上,我正试图让lambda接受来自两个AWS触发器的输入。 我不确定这两个触发器是否需要单独的处理函数,或者,如果像我猜测的那样,使用smarthome触发器使ask-api方法失效,以某种方式调用注册的Intent函数,到达的json显然与两种触发器类型的格式不同,我很感激它可以在lambda中手动完成所有alexa自定义技能解析。那么我的问题是......如果开始使用自定义技能,如果我再添加一个smarthome触发器,那么注册所有的request-api的函数调用就会变得无效,因为处理request-api事件的一个处理程序不能同时处理smarthome指令。

在解决这个问题后,我试图 "带出 "MQTT调用,在Intent函数中可以工作,就像最初编码的那样,但如果我试图把它们放到单独的函数调用中,就会失败。我知道我想做什么......只是对这门语言还不是很了解。

node.js aws-lambda alexa
1个回答
0
投票

我想你没有理解的是,你实际上是在覆盖同一个变量。

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%清楚你在问什么,但是如果可以的话,你需要把两个函数的内容合并起来(或者用一个处理程序来检查事件并调用一个单独的函数),或者把它们移到单独的lambdas中。

例如

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) 语句,希望能给你一个指示,让你知道什么是 EventType 财产应该是为 IF 语句。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.