【问题标题】:Run Amazon Alexa Skill locally instead of AWS Lambda (Javascript)在本地运行 Amazon Alexa Skill 而不是 AWS Lambda (Javascript)
【发布时间】:2017-09-23 03:03:37
【问题描述】:

是否可以使用 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

【问题讨论】:

  • 嗨 Anna,我是 Bespoken Tools 的创建者之一 - 感谢您提及我们。你想做什么我们不能支持?我们推荐的流程是使用我们的 bst 代理在本地开发您的 Lambda,然后使用我们的 bst deploy 命令将 lambda 部署到 AWS。但想确保我了解您的用例,所以请告诉我更多信息。谢谢,约翰
  • 你的意思是你想在本地运行你的lamba进行测试,还是生产?两者都有解决方案。
  • 您好约翰,感谢您的帮助。我编辑了我的问题。 bst 代理似乎是一个很好的解决方案,但在我的情况下它不起作用;(你能帮我吗?它只是在第一次意图后冻结,alexa 没有反应。有任何日志文件吗?来自终端的日志我没有'不知道怎么回事
  • @Tom 我想了解开发和生产的最佳解决方案,但我不需要重写我的代码。我使用 Alexa SDK 编写我的应用程序npmjs.com/package/alexa-sdk
  • @AnnaK 至于日志,您可以使用Logless 或您自己的记录器(bunyanwinston)。如果您对bst 仍有疑问,我建议您提出一个新问题。

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


【解决方案1】:

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

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

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

【讨论】:

  • Tom 但你能帮我配置 node-lambda 以与 alexa 一起工作吗?我收到该错误:未捕获的异常:TypeError:无法读取未定义的属性“区域设置” TypeError:运行 $ node-lambda run 时无法读取未定义的属性“区域设置”
  • 副手我不知道那里出了什么问题,但实际上您应该为此创建一个新的单独问题。或者,您可以将问题(包含更多详细信息)作为问题提交到 node-lambda 的 github。
【解决方案2】:

这里有一些示例代码,您可以使用它们轻松地在本地运行 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();
    })
  });

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

【讨论】:

    【解决方案3】:

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

    How to test Alexa locally

    【讨论】:

      猜你喜欢
      • 2018-01-21
      • 2019-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2016-08-26
      • 2016-12-17
      相关资源
      最近更新 更多