Skip to content

Instantly share code, notes, and snippets.

@mlopezr
Last active March 14, 2019 14:11
Show Gist options
  • Save mlopezr/85423d8f1192e535d77ba5827a153d9a to your computer and use it in GitHub Desktop.
Save mlopezr/85423d8f1192e535d77ba5827a153d9a to your computer and use it in GitHub Desktop.
Running Alexa skills from your local machine

Running Alexa skills from your local machine

Talk to Alexa skills running on your local machine. Stop waiting for Lambda deployments. Debug step-by-step.

Setup

  • Find the index.js file containing your ASK SDK NodeJS skill handler code (usually in skill-name/lambda/custom)
  • Add server.js to the same folder where index.js is located

Use

  • Run node lambda/custom/server.js (or wherever it is located)
  • In a separate terminal, run ngrok http 8080. This will create a public HTTPS endpoint that forwards requests to your local HTTP server listening on port 8080.
  • In your Alexa Developer Console go to the Build tab, and in the Endpoint section update the HTTPS endpoint with the one provided by ngrok, e.g. https://abc123.ngrok.io and check the option My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority.
  • Check that the skill is enabled for your account in the Test section of your Alexa Developer Console. You can also enable it using ask api enable-skill -s SKILL_ID
  • Invoke the skill as usual. You should see each request on ngrok. You can open http://127.0.0.1:4040 to inspect the requests and responses.
  • You can run server.js in debug mode and use breakpoints. On VS Code, open server.js and click Debug > Start Debugging

Limitations

This is an extremely simple piece of code meant only for initial testing purposes.

const express = require('express');
const bodyParser = require('body-parser');
const handler = require('./index').handler;
function CreateHandler(handler) {
return (req, res) => {
handler(req.body, null, (err, result) => {
if (err) {
return res.status(500).send(err);
}
return res.status(200).json(result);
});
};
}
// create server
const server = express();
const listener = server.listen(process.env.port || process.env.PORT || 8080, function () {
const { address, port } = listener.address();
console.log('%s listening to %s%s', server.name, address, port);
});
// parse json
server.use(bodyParser.json());
// connect the lambda functions to http
server.post("/", CreateHandler(handler));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment