1
votes

Is it possible to run alexa skill locally with ngrok instead AWS? I built a skill in AWS Lambda but I would rather use my own server. What can I do to run Alexa locally?

I tried https://github.com/alexa-js/alexa-app-server but it makes any sense because I would need to rewrite my whole code :( The better solution is http://docs.bespoken.tools/en/latest/tutorials/tutorial_lambda_nodejs/ but it isn't the best. It just works only for a wellcome intent and freezes after that :(

Terminal Logs from bespken command:

    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
Hi Anna, I am one of the creators of Bespoken Tools - thanks for mentioning us. What are you trying to do that we cannot support? Our recommended flow is to developer your Lambdas locally using our bst proxy, and then deploy the lambda out to AWS using our bst deploy command. But would like to make sure I understand your use case, so please tell me more. Thanks, JohnJohn Kelvie
And do you mean that you want to run your lamba locally for testing, or for production? There are solutions for both.Tom
Hi John, thank you for your help. I edited my question. bst proxy seems to be great solution but in my case it doesn't work ;( Could you help me please? It just freezes after first intent and alexa doesn't react. Is there any log file? from logs in terminal I don't know what is going wrongAnna K
@Tom I would like to get to know both best solutions, for developing and production but I dont need to rewrite my code. I used Alexa SDK to write my app npmjs.com/package/alexa-sdkAnna K
@AnnaK As for logs you can use Logless or your own logger (bunyan or winston). If you're still having issues with bst, I suggest you start a new question.spicypumpkin

3 Answers

4
votes

Yes, there are several solutions for running your node lambda locally. I've been using node-lambda, for example. Like most solutions it is oriented to users who want to test locally and then easily deploy to AWS Lambda.

If you want to run them yourself, I would note that MS and IBM have made their implementations of lambda open-source (here's MS's and IBM's). I haven't actually tried it myself, and I would note that with AWS, GCP, and Azure all providing Lambda services for node the market for these is healthy and the lock-in is minimal so I feel less need to be able to run it myself than for something like Dynamo.

But I also recommend that you continue to pursue BST. I'm using some of my own pieces for testing my skills because I got started before I heard of their stuff, but what I have tried of their's (BSTAlexa) is very useful and I see that they provide some of the other pieces you need for easy and effective testing of your skill.

1
votes

Here's some sample code that you can use to easily run a Lambda locally, call this file 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;

Then you can use this 'AlexaLambda' in your tests like so (in my case, I'm using 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();
    })
  });

Then it's just a matter of running your test via whatever framework you're using.

1
votes

You can test your alexa skill locally by following the following tutorial:

How to test Alexa locally