DEV Community

Cover image for Run your own Alexa Skill locally (and test it) [JS]
NOPR9D ☄️
NOPR9D ☄️

Posted on • Updated on

Run your own Alexa Skill locally (and test it) [JS]

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
Enter fullscreen mode Exit fullscreen mode

We will generate our skill starter with

ask new
Enter fullscreen mode Exit fullscreen mode

Choose NodeJs, and self-hosted

Ask new config

Next step we will add Virtual-Alexa dependencies

npm install virtual-alexa --save-dev
Enter fullscreen mode Exit fullscreen mode

Enter into the created folder and add a new node project with

npm init
Enter fullscreen mode Exit fullscreen mode

Add this necessary dependencies

npm i ask-sdk-core ask-sdk-model
npm i virtual-alexa --save-dev
Enter fullscreen mode Exit fullscreen mode

In your package.json file add this start script

 "scripts": {
    "start": "node index.js"
  }
Enter fullscreen mode Exit fullscreen mode

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);
});

Enter fullscreen mode Exit fullscreen mode

Run with

npm run start
Enter fullscreen mode Exit fullscreen mode

If everything is ok you should be able to read the alexa's output speech in your console

Like this

And that it, now you can develop your skill and test the output without send your code.

Nice !


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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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!"
    );
  });
});
Enter fullscreen mode Exit fullscreen mode

run with

npm run test
Enter fullscreen mode Exit fullscreen mode

If everything is ok you should be able to see our test result

Alt Text

And that's it ! Now you can write your skill logic, run and test it.

Here we go


You can also take a look to Ask Toolkit for vscode

See also : Secure the endpoint of my Skills Alexa application (futur post?)

Top comments (0)