Run your own Alexa Skill locally (and test it) [JS]
NOPR9D ☄️
Posted on December 5, 2020
So you had an Alexa device and you wanna make you own skill, here we will learn how we can make our custom skill and test it locally without depending Aws Lambda
First step install the ASK-CLI
npm install -g ask-cli
We will generate our skill starter with
ask new
Choose NodeJs, and self-hosted
Next step we will add Virtual-Alexa dependencies
npm install virtual-alexa --save-dev
Enter into the created folder and add a new node project with
npm init
Add this necessary dependencies
npm i ask-sdk-core ask-sdk-model
npm i virtual-alexa --save-dev
In your package.json file add this start script
"scripts": {
"start": "node index.js"
}
And for the last step create a new file called index.js and we will :
- Init an instance of Virtual alexa
- Handle our lamdba function to it and add our options like locales, interaction models, ...
- Start an intents
- Print the result (alexa speech)
const va = require("virtual-alexa");
const _defaultHandler = va.VirtualAlexa.Builder()
.handler("./lambda/index.js") // Lambda file
.interactionModelFile("./skill-package/interactionModels/custom/en-US.json") // Interaction file
.locale("en-US")
.create();
_defaultHandler.intend("HelloWorldIntent").then((payload) => {
// Print speech to console
console.log("OutputSpeech: " + payload.response.outputSpeech.ssml);
});
Run with
npm run start
If everything is ok you should be able to read the alexa's output speech in your console
And that it, now you can develop your skill and test the output without send your code.
So if we recap, we used the 'hello-world' starter generated by ASK-CLI, we added a node application next to it using the 'virtual-alexa' dependency
As long as we are there we can take the opportunity to add tests, no?
I promise it will take a minute.
We begin with dev dependencies, so wo can add mocha, chai and nyc
npm i mocha chai nyc --save-dev
Create a test folder, and a new "index-test.js" file, in your package.json add this test script :
"test": "nyc mocha test/*-test.js"
So now we can add the usual structure of a test file into 'index-test.js'.
const expect = require("chai").expect;
const VirtualAlexa = require("virtual-alexa").VirtualAlexa;
describe("Test hello world intent", async function() {
const alexa = VirtualAlexa.Builder()
.handler("./lambda/index.js") // Lambda function file and name
.interactionModelFile("./skill-package/interactionModels/custom/en-US.json") // Path to interaction model file
.create();
it("Should be Hello World!", async function() {
const helloWorldResponse = await alexa.intend("HelloWorldIntent");
expect(helloWorldResponse.response.outputSpeech.ssml).to.include(
"Hello World!"
);
});
});
run with
npm run test
If everything is ok you should be able to see our test result
And that's it ! Now you can write your skill logic, run and test it.
You can also take a look to Ask Toolkit for vscode
See also : Secure the endpoint of my Skills Alexa application (futur post?)
Posted on December 5, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.