Lukasz Krysiewicz
Published © GPL3+

Invisible Dice Plus

Second part of tutorial on how to build an Alexa Amazon Skill.

BeginnerFull instructions provided2 hours714
Invisible Dice Plus

Things used in this project

Story

Read more

Schematics

schematic

Code

invisible dice plus

JavaScript
var Alexa = require('alexa-sdk');

// Used when a user asks for help.
const helpMessage    = "If you want run a skill you should say: Throw a dice. Give me a result. Roll a dice. " +
                       "If you want close a skill you should say: Stop or Cancel. " +
                       "For repeat please say help me.";

// Used when a user opens skill.
const welcomeMessage  = "Welcome to Invisible Dice Plus.  " +
                        "Tell a command or please say help me.";

// Used when a user says cancel.
const cancelMessage = "Ok, see you next time!";

// Used when a user says stop.
const stopMessage   = "Bye, bye, see you next time!";



// Used to randomise numbers
const t_numbers = [
      "one",
      "two",
      "three",
      "four",
      "five",
      "six"];

// Used to show images
  const t_images = [
   { imageSmall: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/1-small.png", 
     imageLarge: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/1-large.png" },
   { imageSmall: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/2-small.png", 
     imageLarge: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/2-large.png" },
   { imageSmall: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/3-small.png", 
     imageLarge: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/3-large.png" },
   { imageSmall: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/4-small.png", 
     imageLarge: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/4-large.png" },
   { imageSmall: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/5-small.png", 
     imageLarge: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/5-large.png" },
   { imageSmall: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/6-small.png", 
     imageLarge: "https://s3-eu-west-1.amazonaws.com/invisiblediceplus/6-large.png" },
 ];

// Used to play audio
 const qMark       = "\"";
 const sAudio      = "https://s3.eu-west-1.amazonaws.com/invisiblediceplus/throw.mp3";
 const audioSource = qMark + sAudio + qMark;

// Attaching handlers
// Here we have added the following handlers:
// HelpIntent: envoked when a user asks for help.
// StopIntent/CancelIntent: envoked when a user asks to end the skill.
// ThrowADiceIntent: envoked when a user ask about number.


const handlers = {
/*
  Launch Request ( no intent )

  <invocation name>
  Ask <invocation name>
  Begin <invocation name>
  Do <invocation name>
  Launch <invocation name>
  Load <invocation name>
  Open <invocation name>
  Play <invocation name>
  Play the game <invocation name>
  Resume <invocation name>
  Run <invocation name>
  Start <invocation name>
  Start playing <invocation name>
  Start playing the game <invocation name>
  Talk to <invocation name>
  Tell <invocation name>
  Use <invocation name>
*/
   'LaunchRequest': function () {
        this.emit(':ask', welcomeMessage, welcomeMessage);
   },
/*
<some action> <connecting word> <invocation name>
where the <connecting word> is one of the following: by, from, in, using, with

Ask <invocation name> <connecting word> <some action>
where the <connecting word> is one of the following: to, about, for

Tell <invocation name> <connecting word> <some action>
Where the <connecting word> is one of the following: to, that

Use <invocation name> <connecting word> <some action>
Where the <connecting word> is one of the following: and, to

Ask <invocation name> <some action>
Tell <invocation name> <some action>
Search <invocation name> for <some action>
Open <invocation name> for <some action>
Talk to <invocation name> and <some action>
Open <invocation name> and <some action>
Launch <invocation name> and <some action>
Start <invocation name> and <some action>
Resume <invocation name> and <some action>
Run <invocation name> and <some action>
Load <invocation name> and <some action>
Begin <invocation name> and <some action>
*/
   'ThrowADiceIntent': function () {
        var number = Math.floor(Math.random() * t_numbers.length);

        var sAudio   ="<audio src=" +  audioSource + " />";
        var sOutput  = sAudio + "The result is:" + "  "+ t_numbers[number];

        var cardTitle = "Result of the throw";
        var cardDescription = "Thanks for using.\n" + "The result is: " + t_numbers[number];
        var imageObj  = {
                    smallImageUrl: t_images[number].imageSmall,
                    largeImageUrl: t_images[number].imageLarge
        };


        this.emit(':tellWithCard', sOutput,cardTitle,cardDescription,imageObj);
   },

    'AMAZON.HelpIntent': function () {
        this.emit(':ask',helpMessage, helpMessage);
    },

    'AMAZON.StopIntent': function () {
        this.emit(':tell', stopMessage, stopMessage);
    },

    'AMAZON.CancelIntent': function () {
        this.emit(':tell', cancelMessage, cancelMessage);
    }

}

exports.handler = function (event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.registerHandlers(handlers);
    alexa.execute();
};

vui

JSON
{
  "intents": [
    {
      "name": "AMAZON.CancelIntent",
      "samples": [
        "Cancel"
      ]
    },
    {
      "name": "AMAZON.HelpIntent",
      "samples": []
    },
    {
      "name": "AMAZON.StopIntent",
      "samples": [
        "Stop"
      ]
    },
    {
      "name": "ThrowADiceIntent",
      "samples": [
        "Ask invisible dice {Action}",
        "Tell invisible dice {Action}",
        "Search invisible dice for {Action}",
        "Talk to invisible dice and {Action}",
        "Open invisible dice and {Action}",
        "Launch invisible dice and {Action}",
        "Start invisible dice and {Action}",
        "Resume invisible dice and {Action}",
        "Run invisible dice and {Action}",
        "Load invisible dice and {Action}",
        "Begin invisible dice and {Action}",
        "Ask invisible dice to {Action}",
        "Ask invisible dice about {Action}",
        "Ask invisible dice for {Action}",
        "Tell invisible dice to {Action}",
        "Tell invisible dice that {Action}",
        "Use invisible dice to {Action}",
        "Use invisible dice and {Action}",
        "{Action} by invisible dice",
        "{Action} from invisible dice",
        "{Action} in invisible dice",
        "{Action} using invisible dice",
        "{Action} with invisible dice",
        "{Action}"
      ],
      "slots": [
        {
          "name": "Action",
          "type": "LIST_OF_ACTIONS",
          "samples": []
        }
      ]
    }
  ],
  "types": [
    {
      "name": "LIST_OF_ACTIONS",
      "values": [
        {
          "id": null,
          "name": {
            "value": "throw a dice",
            "synonyms": []
          }
        },
        {
          "id": null,
          "name": {
            "value": "give me a result",
            "synonyms": []
          }
        },
        {
          "id": null,
          "name": {
            "value": "roll a dice",
            "synonyms": []
          }
        }
      ]
    }
  ]
}

cors

XML
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://ask-ifr-download.s3.amazonaws.com</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
    </CORSRule>
</CORSConfiguration>

project folder on the github

Credits

Lukasz Krysiewicz

Lukasz Krysiewicz

8 projects • 21 followers
Embedded Software Engineer

Comments