SKY CrewkossanY SShigeyosh Kamoda
Created January 1, 2020 © CC BY

Cereal Maker

This robot gives you wonderful breakfast. You can choose two cereals in 3 sizes. It can add toppings later and remember your usual recipe.

AdvancedWork in progress3 days53
Cereal Maker

Things used in this project

Hardware components

Mindstorms EV3 Programming Brick / Kit
LEGO Mindstorms EV3 Programming Brick / Kit
×1

Software apps and online services

Fusion 360
Autodesk Fusion 360
VS Code
Microsoft VS Code

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

BOX

hinge_L

hinge_R

handle

box_assembly

compatible with LEGO block (PLA is recommended for printing)

Code

cereal-maker.py

Python
Original Source Code
#!/usr/bin/env python3

import os
import sys
import time
from time import sleep
import logging
import json
import random
import threading

from enum import Enum
from agt import AlexaGadget

from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.motor import OUTPUT_B, OUTPUT_C, SpeedPercent, MediumMotor
from ev3dev2.sensor import INPUT_1, INPUT_4
from ev3dev2.sensor.lego import TouchSensor

# Set the logging level to INFO to see messages from AlexaGadget
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)

DRIVE_POWER = 30
ARM_POWER = - 70
CAN_MOVE = True

class MindstormsGadget(AlexaGadget):

    def __init__(self):
        # Ev3dev initialization
        self.leds = Leds()
        self.sound = Sound()
        self.drive = MediumMotor(OUTPUT_B)
        self.arm = MediumMotor(OUTPUT_C)
        self.arm_touch = TouchSensor(INPUT_1)
        self.drive_touch = TouchSensor(INPUT_4)

        self.recipe = {}
        self.recipe['cereal'] = 0
        self.recipe['size'] = 5
        self.recipe['ingredient'] = []
        self.recipe['add'] = []

        if CAN_MOVE:
            # initiate the positon of the arm
            while not self.arm_touch.is_pressed:
                self.arm.on(- ARM_POWER)
            self.arm.stop()
            self.arm.on_for_degrees(ARM_POWER, 960)

            # initiate the positon of the machine
            while not self.drive_touch.is_pressed:
                self.drive.on(DRIVE_POWER)
            self.drive.stop()
            self.drive.on_for_degrees(- DRIVE_POWER, 1160)

        super().__init__()



    def on_connected(self, device_addr):
        """
        Gadget connected to the paired Echo device.
        :param device_addr: the address of the device we connected to
        """
        self.leds.set_color("LEFT", "GREEN")
        self.leds.set_color("RIGHT", "GREEN")
        logger.info("{} connected to Echo device".format(self.friendly_name))

    def on_disconnected(self, device_addr):
        """
        Gadget disconnected from the paired Echo device.
        :param device_addr: the address of the device we disconnected from
        """
        self.leds.set_color("LEFT", "BLACK")
        self.leds.set_color("RIGHT", "BLACK")
        logger.info("{} disconnected from Echo device".format(self.friendly_name))

    def on_custom_mindstorms_gadget_order(self, directive):
        """
        Handles the Custom.Mindstorms.Gadget order directive.
        :param directive: the custom directive with the matching namespace and name
        """
        try:
            payload = json.loads(directive.payload.decode("utf-8"))
            command = payload['command']
            if command == 'order':
                self.recipe['cereal'] = payload['cereal']
                self.recipe['size'] = payload['size'] + 4
                self.recipe['ingredient'] = payload['ingredient']
            elif command == 'add':
                self.recipe['add'] = payload['ingredient']
                self.recipe['ingredient'] = \
                    list(set(self.recipe['ingredient']) | set(self.recipe['add']))
                self.recipe['ingredient'].sort()
            elif command == 'set':
                with open('recipe.json', 'w') as f:
                    json.dump(self.recipe, f, indent=4)
            elif command == 'usual':
                try:
                    with open('recipe.json', 'r') as f:
                        self.recipe = json.load(f)
                except OSError:
                    pass

            if command != 'set':
                if command == 'add':
                    for ingredient in self.recipe['add']:
                        self.move(ingredient, 2)
                else:
                    self.move(self.recipe['cereal'], self.recipe['size'])
                    for ingredient in self.recipe['ingredient']:
                        self.move(ingredient, 2)
                self.sound.play_song((('C4', 'e'),  ('E5', 'q')))

        except KeyError:
            print("Missing expected parameters: {}".format(directive), file=sys.stderr)

    def move(self, container, size):
        if CAN_MOVE:
            drive_power = DRIVE_POWER * (1 if container % 2 == 0 else -1)
            deg = 410 + 365 * (container // 2)
            self.drive.on_for_degrees(drive_power, deg)
            while (self.drive.is_running):
                pass
            sleep(0.1)
            self.arm.on_for_rotations(ARM_POWER, 16 * size)
            while (self.arm.is_running):
                pass
            sleep(0.4)
            self.drive.on_for_degrees(- drive_power, deg)
            while (self.drive.is_running):
                pass
            sleep(1.0)



if __name__ == '__main__':

    gadget = MindstormsGadget()

    # Set LCD font and turn off blinking LEDs
    os.system('setfont Lat7-Terminus12x6')
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

    # Startup sequence
    gadget.sound.play_song((('C4', 'e'), ('D4', 'e'), ('E5', 'q')))
    gadget.leds.set_color("LEFT", "GREEN")
    gadget.leds.set_color("RIGHT", "GREEN")

    # Gadget main entry point
    gadget.main()

    # Shutdown sequence
    gadget.sound.play_song((('E5', 'e'), ('C4', 'e')))
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

cereal-maker.ini

INI
[GadgetSettings]
amazonId = YOUR_GADGET_AMAZON_ID
alexaGadgetSecret = YOUR_GADGET_SECRET

[GadgetCapabilities]
Custom.Mindstorms.Gadget = 1.0

model.json

JSON
Original Source Code
{
  "interactionModel": {
    "languageModel": {
      "invocationName": "cereal maker",
      "intents": [
        {
          "name": "AMAZON.CancelIntent",
          "samples": []
        },
        {
          "name": "AMAZON.HelpIntent",
          "samples": []
        },
        {
          "name": "AMAZON.StopIntent",
          "samples": []
        },
        {
          "name": "AMAZON.NavigateHomeIntent",
          "samples": []
        },
        {
          "name": "OrderIntent",
          "slots": [
            {
              "name": "Cereal",
              "type": "CerealType"
            },
            {
              "name": "Ingredient",
              "type": "IngredientType"
            },
            {
              "name": "Ingredient_",
              "type": "IngredientType"
            },
            {
              "name": "Ingredient__",
              "type": "IngredientType"
            },
            {
              "name": "Ingredient___",
              "type": "IngredientType"
            }
          ],
          "samples": [
            "Make {Cereal} alone",
            "Make {Cereal} without topping",
            "Make {Cereal} without toppings",
            "Make {Cereal} with {Ingredient}",
            "Make {Cereal} with {Ingredient} and {Ingredient_}",
            "Make {Cereal} with {Ingredient} {Ingredient_} and {Ingredient__}",
            "Make {Cereal} with {Ingredient} and {Ingredient_} and {Ingredient__}",
            "Make {Cereal} with {Ingredient} {Ingredient_} {Ingredient__} and {Ingredient___}",
            "Make {Cereal} with {Ingredient} and {Ingredient_} and {Ingredient__} and {Ingredient___}",
            "{Cereal} alone",
            "{Cereal} without topping",
            "{Cereal} without toppings",
            "{Cereal} with {Ingredient}",
            "{Cereal} with {Ingredient} and {Ingredient_}",
            "{Cereal} with {Ingredient} {Ingredient_} and {Ingredient__}",
            "{Cereal} with {Ingredient} and {Ingredient_} and {Ingredient__}",
            "{Cereal} with {Ingredient} {Ingredient_} {Ingredient__} and {Ingredient___}",
            "{Cereal} with {Ingredient} and {Ingredient_} and {Ingredient__} and {Ingredient___}"
          ]
        },
        {
          "name": "SimpleOrderIntent",
          "slots": [
            {
              "name": "Cereal",
              "type": "CerealType"
            },
            {
              "name": "Size",
              "type": "SizeType",
              "samples": [
                "{Size}",
                "{Size} size",
                "{Size} please",
                "{Size} size please"
              ]
            },
            {
              "name": "Bool",
              "type": "BoolType",
              "samples": [
                "{Bool}"
              ]
            },
            {
              "name": "Bool_",
              "type": "BoolType",
              "samples": [
                "{Bool_}"
              ]
            },
            {
              "name": "Bool__",
              "type": "BoolType",
              "samples": [
                "{Bool__}"
              ]
            },
            {
              "name": "Bool___",
              "type": "BoolType",
              "samples": [
                "{Bool___}"
              ]
            }
          ],
          "samples": [
            "Make {Cereal}",
            "{Cereal}"
          ]
        },
        {
          "name": "AddIntent",
          "slots": [
            {
              "name": "Ingredient",
              "type": "IngredientType"
            }
          ],
          "samples": [
            "Add {Ingredient}",
            "Add some {Ingredient}",
            "Add more {Ingredient}",
            "{Ingredient}",
            "{Ingredient} please",
            "Some {Ingredient}",
            "Some {Ingredient} please",
            "More {Ingredient}",
            "More {Ingredient} please",
            "I want some {Ingredient}",
            "I want more {Ingredient}",
            "I want to have some {Ingredient}",
            "I want to have more {Ingredient}",
            "I would like to have some {Ingredient}",
            "I would like to have more {Ingredient}"
          ]
        },
        {
          "name": "UsualOrderIntent",
          "slots": [
            {
              "name": "Default",
              "type": "DefaultType"
            }
          ],
          "samples": [
            "{Default}",
            "{Default} please",
            "With {Default}",
            "The same as {Default}",
            "The same as {Default} please",
            "Same as {Default}",
            "Same as {Default} please",
            "I will have {Default}",
            "I'll have {Default}",
            "Make Cereal with {Default}",
            "Make Cereal as {Default}",
            "Cereal with {Default}",
            "Cereal as {Default}"
          ]
        },
        {
          "name": "SetDefaultIntent",
          "slots": [
            {
              "name": "Default",
              "type": "DefaultType"
            },
            {
              "name": "Recipe",
              "type": "RecipeType"
            }
          ],
          "samples": [
            "Set {Recipe} as {Default}",
            "Remember {Recipe} as {Default}",
            "Remember that {Recipe} is {Default}",
            "Remember that {Recipe} are {Default}",
            "Remember {Recipe} is {Default}",
            "Remember {Recipe} are {Default}"
          ]
        }
      ],
      "types": [
        {
          "name": "CerealType",
          "values": [
            {
              "id": "0",
              "name": {
                "value": "cornflakes",
                "synonyms": [
                  "corn flakes",
                  "cereal",
                  "cereals",
                  "breakfast cereal",
                  "breakfast cereals"
                ]
              }
            },
            {
              "id": "1",
              "name": {
                "value": "granola",
                "synonyms": [
                  "muesli"
                ]
              }
            }
          ]
        },
        {
          "name": "IngredientType",
          "values": [
            {
              "id": "2",
              "name": {
                "value": "almonds",
                "synonyms": [
                  "almond",
                  "almond nut",
                  "almond nuts",
                  "roasted almonds"
                ]
              }
            },
            {
              "id": "3",
              "name": {
                "value": "walnuts",
                "synonyms": [
                  "walnut",
                  "black walnut",
                  "black walnuts",
                  "akhrot"
                ]
              }
            },
            {
              "id": "4",
              "name": {
                "value": "cashews",
                "synonyms": [
                  "cashew",
                  "cashew nut",
                  "cashew nuts",
                  "roasted cashew",
                  "roasted cashews",
                  "roasted cashew nut",
                  "roasted cashew nuts"
                ]
              }
            },
            {
              "id": "5",
              "name": {
                "value": "dried fruits",
                "synonyms": [
                  "dried fruit",
                  "dry fruit",
                  "dry fruits",
                  "mixed dried fruit",
                  "mixed dried fruits",
                  "dried mixed  fruit",
                  "dried mixed  fruits",
                  "dried fruit mix",
                  "dried fruits mix"
                ]
              }
            }
          ]
        },
        {
          "name": "BoolType",
          "values": [
            {
              "id": "0",
              "name": {
                "value": "False",
                "synonyms": [
                  "no",
                  "no, thank you",
                  "nay",
                  "nope",
                  "nyet",
                  "nah",
                  "naw"
                ]
              }
            },
            {
              "id": "1",
              "name": {
                "value": "True",
                "synonyms": [
                  "yes",
                  "yes, please",
                  "yeah",
                  "yep",
                  "yup"
                ]
              }
            }
          ]
        },
        {
          "name": "SizeType",
          "values": [
            {
              "id": "0",
              "name": {
                "value": "small",
                "synonyms": [
                  "smaller",
                  "smallish"
                ]
              }
            },
            {
              "id": "1",
              "name": {
                "value": "medium",
                "synonyms": [
                  "medium-sized"
                ]
              }
            },
            {
              "id": "2",
              "name": {
                "value": "large",
                "synonyms": [
                  "larger",
                  "largish"
                ]
              }
            }
          ]
        },
        {
          "name": "DefaultType",
          "values": [
            {
              "name": {
                "value": "default",
                "synonyms": [
                  "the default",
                  "defaults",
                  "usual",
                  "the usual",
                  "the usual one",
                  "the usual recipe",
                  "my usual",
                  "my usual one",
                  "my usual recipe"
                ]
              }
            }
          ]
        },
        {
          "name": "RecipeType",
          "values": [
            {
              "name": {
                "value": "the recipe",
                "synonyms": [
                  "this recipe",
                  "that recipe",
                  "the order",
                  "this order",
                  "that order",
                  "this one",
                  "that one",
                  "it",
                  "this",
                  "that",
                  "them",
                  "these",
                  "those"
                ]
              }
            }
          ]
        }
      ]
    },
    "dialog": {
      "intents": [
        {
          "name": "SimpleOrderIntent",
          "confirmationRequired": false,
          "prompts": {},
          "slots": [
            {
              "name": "Cereal",
              "type": "CerealType",
              "confirmationRequired": false,
              "elicitationRequired": false,
              "prompts": {}
            },
            {
              "name": "Size",
              "type": "SizeType",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.1563072020057.65106764934"
              }
            },
            {
              "name": "Bool",
              "type": "BoolType",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.480765851385.739109665850"
              }
            },
            {
              "name": "Bool_",
              "type": "BoolType",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.480765851385.1436186343034"
              }
            },
            {
              "name": "Bool__",
              "type": "BoolType",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.480765851385.170079424503"
              }
            },
            {
              "name": "Bool___",
              "type": "BoolType",
              "confirmationRequired": false,
              "elicitationRequired": true,
              "prompts": {
                "elicitation": "Elicit.Slot.480765851385.562950547601"
              }
            }
          ]
        }
      ],
      "delegationStrategy": "ALWAYS"
    },
    "prompts": [
      {
        "id": "Elicit.Slot.480765851385.739109665850",
        "variations": [
          {
            "type": "PlainText",
            "value": "Would you like some almonds?"
          },
          {
            "type": "PlainText",
            "value": "Would you like to add some almonds?"
          }
        ]
      },
      {
        "id": "Elicit.Slot.480765851385.1436186343034",
        "variations": [
          {
            "type": "PlainText",
            "value": "Would you like some walnuts?"
          },
          {
            "type": "PlainText",
            "value": "Would you like to add some walnuts?"
          },
          {
            "type": "PlainText",
            "value": "How about walnuts?"
          }
        ]
      },
      {
        "id": "Elicit.Slot.480765851385.170079424503",
        "variations": [
          {
            "type": "PlainText",
            "value": "Would you like some cashews?"
          },
          {
            "type": "PlainText",
            "value": "Would you like to add some cashews?"
          },
          {
            "type": "PlainText",
            "value": "How about cashews?"
          }
        ]
      },
      {
        "id": "Elicit.Slot.480765851385.562950547601",
        "variations": [
          {
            "type": "PlainText",
            "value": "Would you like some dried fruits?"
          },
          {
            "type": "PlainText",
            "value": "Would you like to add some dried fruits?"
          },
          {
            "type": "PlainText",
            "value": "How about dried fruits?"
          }
        ]
      },
      {
        "id": "Elicit.Slot.1563072020057.65106764934",
        "variations": [
          {
            "type": "PlainText",
            "value": "What size?"
          },
          {
            "type": "PlainText",
            "value": "What size would you like?"
          },
          {
            "type": "PlainText",
            "value": "What size would you like to have?"
          }
        ]
      }
    ]
  }
}

index.js

JavaScript
Original Source Code
const Alexa = require('ask-sdk-core');
const Util = require('./util');
const Common = require('./common');

const NAMESPACE = 'Custom.Mindstorms.Gadget';

const NAME_ORDER = 'order';

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle: async function (handlerInput) {

        let request = handlerInput.requestEnvelope;
        let { apiEndpoint, apiAccessToken } = request.context.System;
        let apiResponse = await Util.getConnectedEndpoints(apiEndpoint, apiAccessToken);
        if ((apiResponse.endpoints || []).length === 0) {
            return handlerInput.responseBuilder
                .speak(`I couldn't find an EV3 Brick connected to this Echo device. Please check to make sure your EV3 Brick is connected, and try again.`)
                .getResponse();
        }

        // Store the gadget endpointId to be used in this skill session
        let endpointId = apiResponse.endpoints[0].endpointId || [];
        Util.putSessionAttribute(handlerInput, 'endpointId', endpointId);

        return handlerInput.responseBuilder
            .speak("Good Morning! May I take your order?")
            .reprompt("What would you like to eat?")
            .getResponse();
    }
};

const OrderIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'OrderIntent';
    },
    handle: function (handlerInput) {

        let cereal = Alexa.getSlot(handlerInput.requestEnvelope, 'Cereal');
        if (!cereal.value || !isValid(cereal)) {
            return handlerInput.responseBuilder
                .speak("Can you repeat that?")
                .reprompt("What was that again?").getResponse();
        }

        var ingredient = [];
        let elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Ingredient');
        if (isValid(elem)) { ingredient.push(getId(elem)); }
        elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Ingredient_');
        if (isValid(elem)) { ingredient.push(getId(elem)); }
        elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Ingredient__');
        if (isValid(elem)) { ingredient.push(getId(elem)); }
        elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Ingredient___');
        if (isValid(elem)) { ingredient.push(getId(elem)); }

        var ingredientSet = new Set(ingredient);
        ingredient = Array.from(ingredientSet);
        ingredient.sort((a, b) => a - b);

        const attributesManager = handlerInput.attributesManager;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];

        let directive = Util.build(endpointId, NAMESPACE, NAME_ORDER,
            {
                command: `order`,
                cereal: getId(cereal),
                ingredient: ingredient,
                size: 1
            });

        return handlerInput.responseBuilder
            .speak(`Wait a moment please.`)
            .reprompt()
            .addDirective(directive)
            .getResponse();
    }
};

const SimpleOrderIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'SimpleOrderIntent';
    },
    handle: function (handlerInput) {

        let cereal = Alexa.getSlot(handlerInput.requestEnvelope, 'Cereal');
        if (!isValid(cereal)) {
            return handlerInput.responseBuilder
                .speak("Can you repeat that?")
                .reprompt("What was that again?").getResponse();
        }

        let size = Alexa.getSlot(handlerInput.requestEnvelope, 'Size');
        if (isValid(size)) {
            size = getId(size);
        } else {
            size = 1;
        }

        var ingredient = [];
        let elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Bool');
        if (isValid(elem) && getId(elem) === 1) { ingredient.push(2); }
        elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Bool_');
        if (isValid(elem) && getId(elem) === 1) { ingredient.push(3); }
        elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Bool__');
        if (isValid(elem) && getId(elem) === 1) { ingredient.push(4); }
        elem = Alexa.getSlot(handlerInput.requestEnvelope, 'Bool___');
        if (isValid(elem) && getId(elem) === 1) { ingredient.push(5); }

        var ingredientSet = new Set(ingredient);
        ingredient = Array.from(ingredientSet);
        ingredient.sort((a, b) => a - b);

        const attributesManager = handlerInput.attributesManager;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];

        let directive = Util.build(endpointId, NAMESPACE, NAME_ORDER,
            {
                command: `order`,
                cereal: getId(cereal),
                ingredient: ingredient,
                size: size
            });

        return handlerInput.responseBuilder
            .speak(`Wait a moment please.`)
            .reprompt()
            .addDirective(directive)
            .getResponse();
    }
};

const AddIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AddIntent';
    },
    handle: function (handlerInput) {

        let ingredient = Alexa.getSlot(handlerInput.requestEnvelope, 'Ingredient');
        if (!isValid(ingredient)) {
            return handlerInput.responseBuilder
                .speak("Can you repeat that?")
                .reprompt("What was that again?").getResponse();
        }
        ingredient = [getId(ingredient)]

        const attributesManager = handlerInput.attributesManager;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];

        let directive = Util.build(endpointId, NAMESPACE, NAME_ORDER,
            {
                command: 'add',
                ingredient: ingredient
            });

        return handlerInput.responseBuilder
            .speak(`Wait a moment please.`)
            .reprompt()
            .addDirective(directive)
            .getResponse();
    }
};

const UsualOrderIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'UsualOrderIntent';
    },
    handle: function (handlerInput) {

        let def = Alexa.getSlot(handlerInput.requestEnvelope, 'Default');
        if (!isValid(def)) {
            return handlerInput.responseBuilder
                .speak("Can you repeat that?")
                .reprompt("What was that again?").getResponse();
        }

        const attributesManager = handlerInput.attributesManager;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];

        let directive = Util.build(endpointId, NAMESPACE, NAME_ORDER,
            {
                command: 'usual'
            });

        return handlerInput.responseBuilder
            .speak(`Wait a moment please.`)
            .reprompt()
            .addDirective(directive)
            .getResponse();
    }
};

const SetDefaultIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'SetDefaultIntent';
    },
    handle: function (handlerInput) {

        let recipe = Alexa.getSlot(handlerInput.requestEnvelope, 'Recipe');
        let def = Alexa.getSlot(handlerInput.requestEnvelope, 'Default');
        if (!isValid(recipe) || !isValid(def)) {
            return handlerInput.responseBuilder
                .speak("Can you repeat that?")
                .reprompt("What was that again?").getResponse();
        }

        const attributesManager = handlerInput.attributesManager;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];

        let directive = Util.build(endpointId, NAMESPACE, NAME_ORDER,
            {
                command: 'set'
            });

        return handlerInput.responseBuilder
            .speak('I remembered the recipe as the usual')
            .reprompt()
            .addDirective(directive)
            .getResponse();
    }
};


function isValid(slot) {
    return slot.value && slot['resolutions']['resolutionsPerAuthority'][0]['status']['code'] === 'ER_SUCCESS_MATCH';
}

function getId(slot) {
    return !Object.keys(slot).length ? -1 : Number(slot['resolutions']['resolutionsPerAuthority'][0]['values'][0]['value']['id']);
}

exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        OrderIntentHandler,
        SimpleOrderIntentHandler,
        AddIntentHandler,
        UsualOrderIntentHandler,
        SetDefaultIntentHandler,
        Common.HelpIntentHandler,
        Common.CancelAndStopIntentHandler,
        Common.SessionEndedRequestHandler,
        Common.IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addRequestInterceptors(Common.RequestInterceptor)
    .addErrorHandlers(
        Common.ErrorHandler,
    )
    .lambda();

package.json

JSON
{
  "name": "agt-mindstorms",
  "version": "1.1.0",
  "description": "A sample skill demonstrating how to use AGT with Lego Mindstorms",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Amazon Alexa",
  "license": "ISC",
  "dependencies": {
    "ask-sdk-core": "^2.6.0",
    "ask-sdk-model": "^1.18.0",
    "aws-sdk": "^2.326.0",
    "request": "^2.81.0"
  }
}

util.js

JavaScript
/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * You may not use this file except in compliance with the terms and conditions 
 * set forth in the accompanying LICENSE.TXT file.
 *
 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY DISCLAIMS, WITH 
 * RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
*/

'use strict';

const Https = require('https');

/**
 * Build a custom directive payload to the gadget with the specified endpointId
 * @param {string} endpointId - the gadget endpoint Id
 * @param {string} namespace - the namespace of the skill
 * @param {string} name - the name of the skill within the scope of this namespace
 * @param {object} payload - the payload data
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/send-gadget-custom-directive-from-skill.html#respond}
 */
exports.build = function (endpointId, namespace, name, payload) {
    // Construct the custom directive that needs to be sent
    // Gadget should declare the capabilities in the discovery response to
    // receive the directives under the following namespace.
    return {
        type: 'CustomInterfaceController.SendDirective',
        header: {
            name: name,
            namespace: namespace
        },
        endpoint: {
            endpointId: endpointId
        },
        payload
    };
};

/**
 * A convenience routine to add the a key-value pair to the session attribute.
 * @param handlerInput - the context from Alexa Service
 * @param key - the key to be added
 * @param value - the value be added
 */
exports.putSessionAttribute = function(handlerInput, key, value) {
    const attributesManager = handlerInput.attributesManager;
    let sessionAttributes = attributesManager.getSessionAttributes();
    sessionAttributes[key] = value;
    attributesManager.setSessionAttributes(sessionAttributes);
};

/**
 * To get a list of all the gadgets that meet these conditions,
 * Call the Endpoint Enumeration API with the apiEndpoint and apiAccessToken to
 * retrieve the list of all connected gadgets.
 *
 * @param {string} apiEndpoint - the Endpoint API url
 * @param {string} apiAccessToken  - the token from the session object in the Alexa request
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/send-gadget-custom-directive-from-skill.html#call-endpoint-enumeration-api}
 */
exports.getConnectedEndpoints = function(apiEndpoint, apiAccessToken) {

    // The preceding https:// need to be stripped off before making the call
    apiEndpoint = (apiEndpoint || '').replace('https://', '');
    return new Promise(((resolve, reject) => {

        const options = {
            host: apiEndpoint,
            path: '/v1/endpoints',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + apiAccessToken
            }
        };

        const request = Https.request(options, (response) => {
            response.setEncoding('utf8');
            let returnData = '';
            response.on('data', (chunk) => {
                returnData += chunk;
            });

            response.on('end', () => {
                resolve(JSON.parse(returnData));
            });

            response.on('error', (error) => {
                reject(error);
            });
        });
        request.end();
    }));
};

common.js

JavaScript
/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * You may not use this file except in compliance with the terms and conditions 
 * set forth in the accompanying LICENSE.TXT file.
 *
 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY DISCLAIMS, WITH 
 * RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
*/

'use strict'

const Alexa = require('ask-sdk-core');

const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'You can say hello to me! How can I help?';

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        const speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.getResponse();
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt("I don't understand this command, try again")
            .getResponse();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

// The request interceptor is used for request handling testing and debugging.
// It will simply log the request in raw json format before any processing is performed.
const RequestInterceptor = {
    process(handlerInput) {
        let { attributesManager, requestEnvelope } = handlerInput;
        let sessionAttributes = attributesManager.getSessionAttributes();

        // Log the request for debug purposes.
        console.log(`=====Request==${JSON.stringify(requestEnvelope)}`);
        console.log(`=========SessionAttributes==${JSON.stringify(sessionAttributes, null, 2)}`);
    }
};

module.exports = {
    HelpIntentHandler,
    CancelAndStopIntentHandler,
    SessionEndedRequestHandler,
    IntentReflectorHandler,
    ErrorHandler,
    RequestInterceptor
    };

Credits

SKY Crew

SKY Crew

1 project • 0 followers
kossan

kossan

0 projects • 0 followers
Y S

Y S

0 projects • 0 followers
Shigeyosh Kamoda

Shigeyosh Kamoda

0 projects • 0 followers

Comments