在本地运行Amazon Alexa Skill而不是AWS Lambda(Javascript)

时间:2017-04-25 22:10:51

标签: amazon-web-services aws-lambda alexa alexa-skills-kit alexa-skill

是否可以在本地使用ngrok而不是AWS运行alexa技能?我在AWS Lambda中构建了一项技能,但我宁愿使用自己的服务器。如何在本地运行Alexa?

我尝试了https://github.com/alexa-js/alexa-app-server,但它有任何意义,因为我需要重写我的整个代码:(更好的解决方案是http://docs.bespoken.tools/en/latest/tutorials/tutorial_lambda_nodejs/,但它不是最好的。它只适用于一个好的意图和冻结之后:(

终端日志来自bespken命令:

    BST: v0.9.35  Node: v7.8.0

Your URL for Alexa Skill configuration:
https://proxy.bespoken.tools?node-id=33efccba-2246-477f-bbb8-2e1e510cce9d

INFO  2017-04-25T20:27:20.628Z Connected - proxy.bespoken.tools:5000
INFO  2017-04-25T20:27:26.812Z RequestReceived: POST /?node-id=33efccba-2246-477f-bbb8-2e1e510cce9d ID: 1493152039146
INFO  2017-04-25T20:27:26.815Z Forwarding localhost:10000
Current hour: 24
Warning: Application ID is not set
INFO  2017-04-25T20:27:27.939Z ResponseReceived ID: 1493152039146
INFO  2017-04-25T20:28:10.755Z RequestReceived: POST /?node-id=33efccba-2246-477f-bbb8-2e1e510cce9d ID: 1493152078963
INFO  2017-04-25T20:28:10.756Z Forwarding localhost:10000
Warning: Application ID is not set
INFO  2017-04-25T20:28:11.157Z ResponseReceived ID: 1493152078963
INFO  2017-04-25T20:28:51.073Z RequestReceived: POST /?node-id=33efccba-2246-477f-bbb8-2e1e510cce9d ID: 1493152113739
INFO  2017-04-25T20:28:51.073Z Forwarding localhost:10000
Warning: Application ID is not set
INFO  2017-04-25T20:28:51.995Z ResponseReceived ID: 1493152113739

3 个答案:

答案 0 :(得分:4)

是的,有几种解决方案可以在本地运行你的节点lambda。例如,我一直在使用node-lambda。与大多数解决方案一样,它面向希望在本地进行测试然后轻松部署到AWS Lambda的用户。

如果你想自己运行它们,我会注意到MS和IBM已经实现了lambda开源的实现(这里是MS'sIBM's)。我自己并没有真正尝试过,我会注意到AWS,GCP和Azure都为节点提供Lambda服务,市场对这些服务是健康的,锁定很少,所以我觉得不太需要能够运行我自己就像Dynamo那样。

但我也建议你继续追求BST。我正在使用我自己的一些作品来测试我的技能,因为我在听到他们的东西之前就已经开始了,但我尝试过他们的(BSTAlexa)是非常有用的,我发现他们提供了你需要的其他部分轻松有效地测试您的技能。

答案 1 :(得分:1)

以下是一些可用于在本地轻松运行Lambda的示例代码,请调用此文件AlexaLambda.js:

const log = require('console');
var AWS = require('aws-sdk');

AWS.config.region = "us-east-1";
AWS.config.update({
    accessKeyId: "----",
    secretAccessKey: "----",
});

/**
 * Wraps the actual underlying Alexa lambda initialization in a 
 * Promise. Injects test mocks where appropriate.
 */
var initializerPromise = new Promise(function(fulfill, reject) {
    // Mock out certain imports here if you want but not necessary
    /*
  var Module = require('module');
  var originalRequire = Module.prototype.require;
  Module.prototype.require = function() {
    if ((arguments[0] == 'S3FeedService') ||
        (arguments[0] == './lib/S3FeedService')) {
      return MockS3Service;
    } else if ((arguments[0] == 'WebsocketService') ||
               (arguments[0] == './lib/WebsocketService')) {
      return WSMockService;
    } else if ((arguments[0] == 'SQSService') ||
               (arguments[0] == './lib/SQSService')) {
      return SQSMockService;
    } else {
      return originalRequire.apply(this, arguments);
    }
  };*/
  // Import your actual lambda here.
  var lambda = require('../src/index.js');
  fulfill(lambda);

});

/**
 * The Alexa Lambda context object which is called upon completion
 * of lambda execution.  Also wraps the callback which contains the 
 * test assertion code of the caller.
 * @param callback - must be of the form function(error, result) {};
 * @returns
 */
function Context(callback) {
    this.clientContext = {"env": {}}; 
    this.callback = callback;
}

Context.prototype.done = function(error, result) {
    if (typeof error != "undefined" && error) {
        this.callback(error, null);
    } else {
        this.callback(null, result);
    }
}

Context.prototype.succeed = function(result) {
    this.callback(null, result);
}

Context.prototype.fail = function(error) {
    this.callback(error, null);
}

/**
 * The AlexaLambda object that's exposed for test cases.
 * @returns
 */
function AlexaLambda() {
}

/**
 * Executes the lambda function, provided an inputEvent and a 
 * callback.
 * @param inputEvent - the input event that includes the intent.
 * @param callback - called upon completion of lambda execution.
 */
AlexaLambda.prototype.execute = function(inputEvent, callback) {
    initializerPromise.then(function(lambda) {
        var context = new Context(callback);
        lambda.handler(inputEvent, context);
    });
}

/**
 * Export the lambda class, importers instantiate via new AlexaLambda();
 */
module.exports = AlexaLambda;

然后你可以在你的测试中使用这个'AlexaLambda'(在我的例子中,我使用的是Mocha):

var AlexaLambda = require('./AlexaLambda');
var Event = require('./Event');  // My 'fake' Event class

describe("Guest User Test", function() {
  var alexaLambda = new AlexaLambda();      

  it("Alexa, open/launch 60db", function(done) {
    var event = Event.createLaunchEvent();
    alexaLambda.execute(event, function(error, result) {
      validateYourResultHere();
      done();
    })
  });

然后,只需通过您正在使用的任何框架运行测试。

答案 2 :(得分:1)

您可以按照以下教程在本地测试您的alexa技能:

How to test Alexa locally