Skip to content
Alexa Skill Template with clean code (eslint, sonar), testing (unit tests, e2e), multi-languge and Alexa Presentation Language (APL) support
JavaScript PowerShell Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.ask
.vscode
hooks
lambda/custom dynamola added. Jul 11, 2019
models
test
.eslintignore
.eslintrc
.gitignore
LICENSE
README.md e2e tests added. Jul 11, 2019
package.json e2e tests added. Jul 11, 2019
skill.json
sonar-project.properties
testing.json

README.md

alexa-skill-clean-code-template

https://github.com/javichur/alexa-skill-clean-code-template

Alexa Skill Template with clean code (eslint, sonar), testing (unit tests, e2e), multi-languge and Alexa Presentation Language (APL) support

Made with ❤️ by Javier Campos (https://javiercampos.es) Available on the [AWS Serverless Application Repository].

Initial Setup

  1. Install ASK CLI (npm install -g ask-cli)

  2. Install Visual Code (https://code.visualstudio.com/)

  3. Install Alexa Skill Kit (ASK) Toolkit for vscode (https://marketplace.visualstudio.com/items?itemName=ask-toolkit.alexa-skills-kit-toolkit)

  4. You must edit ~/.aws/credentials file to add a valid AWS profile (aws_access_key_id, aws_secret_access_key) with the following policy:

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": [
      "iam:CreateRole",
      "iam:GetRole",
      "iam:AttachRolePolicy",
      "iam:PassRole",
      "lambda:AddPermission",
      "lambda:CreateFunction",
      "lambda:GetFunction",
      "lambda:UpdateFunctionCode",
      "lambda:ListFunctions",
      "logs:FilterLogEvents",
      "logs:getLogEvents",
      "logs:describeLogStreams"
    ],
    "Resource": "*"
  }
}

More info:

How to create a new Alexa Skill using this template

  1. Configure ASK CLI with AWS profile (credentials):
ask init
  1. Create a new skill from this template (https://github.com/javichur/alexa-skill-clean-code-template):
ask new --url https://github.com/javichur/alexa-skill-clean-code-template.git
? Please type in your new skill name, alphanumeric only: my-new-skill
  1. Install dependencies:
cd my-new-skill/lambda/custom
npm install
  1. Develop your awesome Alexa Skill! Take advantage of this template!

  2. Deploy the skill easily with cli:

ask deploy

How to take advantage of this template

  1. Control your Code Style using Airbnb eslint rules:
npm run eslint
  1. Control the quality of your code using SonarQube (I assume Sonar running on localhost:9000):
npm run sonar
start http://localhost:9000/dashboard?id=my-new-skill
  1. Add text strings for all languages in strings folder.

  2. Use vscode and press F5 to start Unit Tests and debug with breakpoints in your code.

Or use npm run unit-test

  1. Deploy the skill easily with cli:
ask deploy
  1. Run e2e tests. Use npm run e2e-test. You must get a free access token for a virtual device in https://apps.bespoken.io/dashboard/virtualdevice and paste it in testing.json file.

How this template was created from Amazon-provided "Hello world" template

  1. Configure ASK CLI with AWS profile (credentials):
ask init

You must edit ~/.aws/credentials file to add a valid AWS profile (aws_access_key_id, aws_secret_access_key, region). More info: https://docs.aws.amazon.com/es_es/cli/latest/userguide/cli-chap-configure.html

  1. Create a new skill from Hello World template (https://github.com/alexa/skill-sample-nodejs-hello-world):
ask new
? Please select the runtime Node.js V8
? List of templates you can choose Hello World
? Please type in your skill name:  alexa-skill-clean-code-template

The .ask/config file will be contain:

{
  "deploy_settings": {
    "default": {
      "skill_id": "",
      "was_cloned": false,
      "merge": {}
    }
  }
}
  1. Edit skill.json to deploy Lambda function in Europe:
"apis": {
  "custom": {
    "endpoint": {
      "sourceDir": "lambda/custom",
      "uri": "ask-custom-clean-code-skill-template-default"
    },
    "regions": {
      "EU": {
        "endpoint": {
          "sourceDir": "lambda/custom",
          "uri": "ask-custom-clean-code-skill-template-default"
        }
      }
    }
  }
}
  1. Deploy
ask deploy

Or deploy only Lambda function

ask deploy lambda

The ask deploy command invokes hooks in order to install dependencies.

  1. Test interaction model using Utterance Profiler (https://developer.amazon.com/alexa/console/ask)

  2. Test your skill using interactive ask dialog (beta)

ask dialog --locale "en-US"
User  >  start hello world
Alexa >  Welcome to the Alexa Skills Kit, you can say hello!
User  >  help
Alexa >  You can say hello to me!
User  >  hello
Alexa >  Hello World!
  1. Test your skill using simulator (https://developer.amazon.com/alexa/console/ask)

  2. Test JSON request/response using ask simulate

ask simulate --locale "en-US" --text "start hello world"
  1. Automating Unit Tests using Bespoken Tools. .vscode/launch.json edited.
npm install bespoken-tools --save-dev

And press F5 to start Unit Tests. Or use npm run unit-test

  1. Clean code. ESLINT (with airbnb rules) + Sonar added. See root package.json
npm run eslint
npm run sonar-eslint
  1. Locale. Add addRequestInterceptors() interceptor.

  2. Add text strings for all languages in strings folder.

  3. Add es-ES language in skill.json.

  4. Refactor handlers. See /handlers/globalHandlers.js.

  5. IntentReflectorHandler added.

  6. Add APL support in Skill Manifest (https://developer.amazon.com/es/docs/alexa-presentation-language/apl-select-the-viewport-profiles-your-skill-supports.html).

"interfaces": [
  {
    "type": "ALEXA_PRESENTATION_APL",
    "supportedViewports": [
      {
        "mode": "HUB",
        "shape": "ROUND",
        "minWidth": 480,
        "maxWidth": 480,
        "minHeight": 480,
        "maxHeight": 480
      },
      {
        "mode": "HUB",
        "shape": "RECTANGLE",
        "minWidth": 1024,
        "maxWidth": 1024,
        "minHeight": 600,
        "maxHeight": 600
      },
      {
        "mode": "HUB",
        "shape": "RECTANGLE",
        "minWidth": 1280,
        "maxWidth": 1280,
        "minHeight": 800,
        "maxHeight": 800
      },
      {
        "mode": "TV",
        "shape": "RECTANGLE",
        "minWidth": 960,
        "maxWidth": 960,
        "minHeight": 540,
        "maxHeight": 540
      },
      {
        "mode": "HUB",
        "shape": "RECTANGLE",
        "minWidth": 960,
        "maxWidth": 960,
        "minHeight": 480,
        "maxHeight": 480
      }
    ]
  }
],
  1. Add APL templates /apl.

  2. Use APL with custom layoyts (VerticalListItem in documentListado.json).

  3. Use APL events (headerNavigationAction and TouchWrapper.onPress in documentListado.json).

  4. Access to DynamoDB

npm install dynamola

Use it:

const Dynamola = require('dynamola');
let myDb = new Dynamola("nombre-tabla-en-dynamodb", "nombre-primary-key-en-dynamodb", null);

myDb.getItem(userID).then((data) => {
  if(!data){
      // item no existe
  }
  else {
    // item devuelto OK
  }
})
.catch((err) => {
  // error al acceder a dynamodb
});

// TODO

  1. Work with session. See /data/sessionState.js.

  2. Access to external APIs. Check /data/api.js and UseApiRequestHandler (index.js).

  3. Mock info /data/mocks.

  4. Tests e2e added. Use npm run e2e-test. You must get a free access token for a virtual device in https://apps.bespoken.io/dashboard/virtualdevice and paste it in testing.json file.

  5. Deploy using Alias and Lambda versions. // TODO

Known problems

  1. Error invoking ask deploy: "No se puede cargar el archivo \hooks\pre_deploy_hook.ps1 porque la ejecución de scripts está deshabilitada en este sistema". Solution:
Set-ExecutionPolicy -Scope CurrentUser
$> unrestricted

IMPORTANT: If you create a new skill project from a skill template that contains hook scripts, ASK CLI will run them. You should only use skill templates from sources that you trust.

Useful commands

  1. Download a skill. it allows you to download a complete skill (including skill.json) created in your Alexa developer account.
ask clone --skill-id xxxxxxxxxxxxxxx
You can’t perform that action at this time.