AWS Lambda NodeJS发布请求

时间:2018-01-30 18:49:05

标签: amazon-web-services lambda

我正在尝试构建一个简单的aws lambda函数。基本上我的alexa技能的意图使用了这个功能(我留下了一些空字符串,但一切都应该没问题):

'use strict';

const Alexa = require('alexa-sdk');
var http = require("https");


const APP_ID = undefined; // TODO replace with your app ID (OPTIONAL).

const languageStrings = {
    'en': {
        translation: {
            SKILL_NAME: '',
            STOP_MESSAGE: '',
        },
    },
    'en-US': {
        translation: {
            SKILL_NAME: ''        
            },
    },
    'en-GB': {
        translation: {
            SKILL_NAME: ''        
            },
    },
    'de': {
        translation: {
            SKILL_NAME: '',
            STOP_MESSAGE: '',

        },
    },
};

const handlers = {
    'LaunchRequest': function () {
        this.attributes.speechOutput = this.t('SKILL_NAME', this.t('SKILL_NAME'));
        this.emit(':ask', this.attributes.speechOutput);
    },
    'Test': function () {

      var command = this.event.request.intent.slots.commandType.value;
      var response = "";
      var self = this;
      if(command === "begin"){
        sendCommand("load", 0, function(res){

          self.emit(':tell', "Command executed");
        });
      }

    },
    'AMAZON.HelpIntent': function () {

    },
    'AMAZON.RepeatIntent': function () {

    },
    'AMAZON.StopIntent': function () {
        this.emit('SessionEndedRequest');
    },
    'AMAZON.CancelIntent': function () {
        this.emit('SessionEndedRequest');
    },
    'SessionEndedRequest': function () {
        //this.emit(':tell', this.t('STOP_MESSAGE'));
    },
    'Unhandled': function () {

    },
};

function sendCommand(commandName, scenarioID, callback){
  var options = {
    "method": "POST",
    "hostname": "someURL",
    "port": null,
    "path": "somePath",
    "headers": {
      "content-type": "application/json",
      "accept": "application/json",
      "cache-control": "no-cache"
    }
  };

  var req = http.request(options, function(res){
    var chunks = [];

    res.on("data", function (chunk) {
      chunks.push(chunk);
    });

    res.on("end", function () {
      var body = Buffer.concat(chunks);
      return callback(body.toString());
    });

  });
  req.write(JSON.stringify({ commandType: commandName, scenario: scenarioID }));
  req.end();
}

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.resources = languageStrings;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

这可以作为一个独立的脚本运行,但是当我尝试在AWS函数中使用sendCommand时它就不再起作用了。 有人可以帮我吗?

0 个答案:

没有答案
相关问题