Grato por sua visita. Neste momento esta página está apenas disponível em inglês.

Light up Echo Buttons

Echo Buttons contain full-color LEDs that you can use to enhance the skill experience. For example, you can use the LEDs to differentiate users, queue timing-based gameplay, or just to set the mood. This topic describes how to light up the buttons using animation sequences.

Animating the Buttons

Your skill can modify the behavior of each connected Echo Button, or multiple Echo Buttons at once, by including the GadgetController.SetLight directive in response to any request from Alexa. To use this directive, your skill metadata must include the Gadget Controller interface.

To understand the SetLight directive, visualize that you are asking the Amazon Echo device to deliver commands to each individual Echo Button. Each SetLight directive wraps a single command to deliver, but the same message will be broadcast to each Echo Button that is specified in the directive. You can also send the command to all connected Echo Buttons at once by not specifying a specific button.

The following figure shows how your skill interacts with the Gadget Controller to send commands to Echo Buttons.

Controlling Echo Buttons

The following example shows the general structure of the SetLight directive.

{
   "type": "GadgetController.SetLight",
   "version": 1,
   "targetGadgets": [ "gadgetId1", "gadgetId2" ],
   "parameters": {
      "triggerEvent": "none",
      "triggerEventTimeMs": 0,
      "animations": [ 
       {
          "repeat": 1,
          "targetLights": ["1"],
          "sequence": [ 
           {
              "durationMs": 10000,
              "blend": false,
              "color": "0000FF"
           }
          ] 
        }
      ]
    }
 }

The following is a description of each field. For detailed API information, see the GadgetController Interface Reference.

  • type – The type of directive, which is GadgetController.SetLight.
  • version – The version of this directive. Set this to 1.
  • targetGadgets – The gadget IDs of the buttons that will receive the animations. You find the gadget IDs by using a roll call. You can broadcast the same command to all connected Echo Buttons by leaving this array empty or omitting the targetGadgets field altogether.
  • parameters – The animation-specific arguments, all of which are required:
    • triggerEvent – The action that triggers the animation. It has three possible values. You can send three directives to a single Echo Button simultaneously to populate each of the trigger events, but each subsequent command to the same Echo Button that addresses the same trigger event overwrites the one that you previously set. The possible values for triggerEvent are as follows:
      • buttonDown – Play the animation when the button is pressed. This replaces the current buttonDown animation. If a buttonDown animation is currently playing, the new animation replaces it immediately. Otherwise, the new animation will play on subsequent presses of the button.
      • buttonUp – Play the animation when the button is released. This replaces the current buttonUp animation. If a buttonUp animation is currently playing, the new animation replaces it immediately. Otherwise, the new animation will play on subsequent releases of the button.
      • none – Play the animation as soon as it arrives.
    • triggerEventTimeMs – The delay in milliseconds to wait after the trigger event before playing the animation.
    • animations – One animation. An animation is a sequence of steps to be sent in a specific order.

Defining Animations

Inside an animation, you provide a sequence of steps. Each step defines the duration that it will last, the color that will be displayed in that interval, and whether the interval displays the full color the whole time, or whether the color will be interpolated up to that key color over the course of the interval (that is, whether the color will be blended).

The following is the general structure of animations.

"animations": [ 
 {
     "repeat": someNumber,
     "targetLights": ["1"],
     "sequence": [ 
     {
       "durationMs": someNumber,
       "blend": true, // or false
       "color": "colorInRGBHex"
      },
     {
       "durationMs": someNumber,
       "blend": true, // or false
       "color": "colorInRGBHex"
      }
    ] 
  }
]

The following is a description of each field. For detailed API information, see the GadgetController Interface Reference.

  • repeat – The number of times to play this animation. Minimum: 0. Maximum: 255.
  • targetLights – A list of light addresses on the target gadgets that this animation will be applied to. Because the Echo Button has one light only, set this to 1 to signify that this animation should be sent to light one.
  • sequence – The animation steps to render in order. The maximum number of steps that you can define is 38. The minimum is 0 (though a zero-step animation sets the animation for that trigger to blank, clearing any animation that is currently set for that trigger). Each step has the following fields:
    • durationMs – the duration in milliseconds to render this step. The minimum is 1. The maximum is 65,535.
    • color – The color in RGB hexadecimal values to render. The format is RRGGBB. There are a number of Node.js libraries available for working with color. Remember to remove the # after you convert the value to hexadecimal.

      Due to the limitations of the hardware, some colors do not look as expected when rendered on Echo Buttons. You may need to experiment to find colors that work well with your skill. For examples of how colors might appear when rendered on Echo Buttons, see Echo Button Animations.
    • blendtrue to interpolate from the previous color into this one over the course of this directive's durationMs.

Supported Animation Sizes

Animation sizes for the LEDs on Echo Buttons are constrained by a single, binary-packed SetLight directive that fits into a single Bluetooth packet. This includes the space occupied by the addresses of target gadgets.

To calculate the maximum number of steps that you can include in the sequence array for a given trigger, use the following formula:

maxStepsPerSequence = 38 - numberOfTargetGadgetsSpecified * 3

For example, if you want to send animations to all buttons, you can send a maximum of 38 steps. If you specify one target button, then you can send at most 35 steps, and so on.

How Animations Interact

The following rules explain how the animations for the different trigger eventsnone (immediate), buttonDown, and buttonUp — interact with each other.

buttonDown and buttonUp vs. none

buttonDown and buttonUp animations play over the none animation. The none animation is not canceled or paused while the buttonDown and buttonUp animations play.

Example:

  1. The none animation is playing.
  2. A user presses a button. The buttonDown animation plays.
  3. The user releases the button. The buttonUp animation plays.
  4. The none animation continues to play as if it was playing all along.

Also note that a new none animation does not stop or replace any existing buttonDown or buttonUp animations. It just replaces the existing none animation, if any.

buttonDown vs. buttonUp

buttonDown and buttonUp animations replace each other.

Example:

  1. The buttonDown animation is set to 5 seconds and the buttonUp animation is set to 1 second.
  2. A user presses a button. The buttonDown animation starts to play.
  3. After 1 second, the user releases the button. The buttonUp animation plays.
  4. After the buttonUp animation finishes, the none animation (if any) continues playing. The buttonDown animation does not continue.

New vs. Existing none

A new none animation replaces the existing none animation. As such, to cancel a none animation that is already playing, send another none animation. This could be a reset-style animation that has a short duration (100ms) and a color of 0x000000.

New vs. Existing buttonDown

A new buttonDown animation replaces the existing buttonDown animation. As such, to stop or replace the buttonDown animation, send a new buttonDown animation. Note that you don't need to wait for a button press to replace a buttonDown animation that is already playing. The moment the Echo Button receives the new buttonDown animation, the new animation takes effect immediately.

New vs. Existing buttonUp

This is the same behavior as New vs. Existing buttonDown.

Animations to Avoid

When you program your animations, avoid using the predefined system animations described in Best Practices.

Examples

This section provides examples of SetLight directives and how you can assemble them using the ASK SDKs.

For complete sample skills, see Hello Buttons and Color Changer in GitHub.

Immediate Animation Example

The following directive plays the animation as soon as it arrives at the specified buttons. It initially sets the color to red, blends up to blue in one second, holds on blue for half a second, and then quickly blends back down to red. It repeats this sequence three times.

Copied to clipboard.

{
    "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak>This animation is red, then blue, then red. It repeats three times.</speak>"
    },
    "shouldEndSession": false,
    "directives": [
        {
            "type": "GadgetController.SetLight",
            "version": 1,
            "targetGadgets": [],
            "parameters": {
                "triggerEvent": "none",
                "animations": [
                    {
                        "repeat": 3,
                        "targetLights": [
                            "1"
                        ],
                        "sequence": [
                            {
                                "durationMs": 500,
                                "color": "FF0000",
                                "blend": false
                            },
                            {
                                "durationMs": 1000,
                                "color": "0000FF",
                                "blend": true
                            },
                            {
                                "durationMs": 500,
                                "color": "0000FF",
                                "blend": false
                            },
                            {
                                "durationMs": 200,
                                "color": "FF0000",
                                "blend": true
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Node.js (v2).

const AnimateButtonsIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'AnimateButtonsIntent';
  },
  handle(handlerInput) {

    const response = handlerInput.responseBuilder
      .speak('This animation is red, then blue, then red. It repeats three times.')
      .withShouldEndSession(false)
      .addDirective
      (
            {
                'type': 'GadgetController.SetLight',
                'version': 1,
                'targetGadgets': [],
                'parameters': {
                    'triggerEvent': 'none',
                    'animations': [
                        {
                            'repeat': 3,
                            'targetLights': [
                                '1'
                            ],
                           'sequence': [
                               {
                                 'durationMs': 500,
                                 'color': 'FF0000',  // Red
                                 'blend': false
                               },
                               {
                                 'durationMs': 1000,
                                 'color': '0000FF',  // Fade to blue
                                 'blend': true
                               },
                               {
                                 'durationMs': 500,
                                 'color': "0000FF",  // Hold on blue
                                 'blend': false
                               },
                               {
                                 'durationMs': 200,
                                 'color': "FF0000",  // Red
                                 'blend': true
                               }
                           ]
                        }
                    ]
                }
            }
       )
      .getResponse();

      console.log('===RESPONSE=== '+ JSON.stringify(response));
      return response;

  }
};

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Java.

package com.amazon.ask.buttonsamples.handlers;

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.interfaces.gadgetController.*;
import com.amazon.ask.model.services.gadgetController.*;
import java.util.Optional;
import java.util.Arrays;
import java.util.List;
import static com.amazon.ask.request.Predicates.intentName;


public class AnimateButtonsIntentHandler implements RequestHandler {

    @Override
    public boolean canHandle(HandlerInput handlerInput) {
        return handlerInput.matches(intentName("AnimateButtonsIntent"));
    }

    @Override
    public Optional<Response> handle(HandlerInput handlerInput) {

      // Set the light to red.
      AnimationStep animationStep1 = AnimationStep.builder()
        .withDurationMs(500)
        .withColor("FF0000")
        .withBlend(false)
        .build();

      // Fade to blue.
      AnimationStep animationStep2 = AnimationStep.builder()
        .withDurationMs(1000)
        .withColor("0000FF")
        .withBlend(true)
        .build();

      // Hold on blue.
      AnimationStep animationStep3 = AnimationStep.builder()
        .withDurationMs(500)
        .withColor("0000FF")
        .withBlend(false)
        .build();

      // Fade back to red.
      AnimationStep animationStep4 = AnimationStep.builder()
        .withDurationMs(200)
        .withColor("FF0000")
        .withBlend(true)
        .build();

      // Build an animation out of the steps defined previously.
      LightAnimation animation = LightAnimation.builder()
        .withRepeat(3)
        .withTargetLights((List<String>)Arrays.asList("1"))
        .withSequence((List<AnimationStep>)Arrays.asList(animationStep1,
                       animationStep2, animationStep3, animationStep4))
        .build();

      // Using the animation information defined previously, build
      // the parameter list.
      SetLightParameters animationParameters = SetLightParameters.builder()
        .withAnimations((List<LightAnimation>)Arrays.asList(animation))
        .withTriggerEvent(TriggerEventType.NONE)
        .build();

      // Assemble the final SetLight directive.
      SetLightDirective directive = SetLightDirective.builder()
        .withParameters(animationParameters)
        .withVersion(1)
        .build();

      // Assemble the response.
      Optional<Response> response = handlerInput.getResponseBuilder()
        .withSpeech("This animation is red, then blue, then red. It repeats three times.")
        .withShouldEndSession(false)
        .addDirective(directive)
        .build();

      // Write the response to CloudWatch.
      String responseLog = response.toString();
      responseLog = responseLog.replace("\n", " ").replace("\r", " ");
      System.out.println("===RESPONSE=== " + responseLog);

      // Return the response.
      return response;

   }
}

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Python.

from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_core.utils import is_intent_name
from ask_sdk_model import Response
from ask_sdk_model.interfaces.gadget_controller import SetLightDirective
from ask_sdk_model.services.gadget_controller import (
    AnimationStep, LightAnimation, SetLightParameters)
 
 
class AnimateButtonsIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return is_intent_name("AnimateButtonsIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
 
        # Set the light to red
        animation_step_1 = AnimationStep(
            duration_ms=500, color="FF0000", blend=False)
 
        # Fade to blue
        animation_step_2 = AnimationStep(
            duration_ms=1000, color="0000FF", blend=True)
 
        # Hold on blue
        animation_step_3 = AnimationStep(
            duration_ms=500, color="0000FF", blend=False)

        # Set the light to red
        animation_step_4 = AnimationStep(
            duration_ms=200, color="FF0000", blend=True)
 
        # Build an animation out of the steps defined previously
        animation = LightAnimation(
            repeat=3, target_lights=["1"],
            sequence=[animation_step_1, animation_step_2, animation_step_3,
                      animation_step_4])
 
        # Using the animation information defined previously,
        # build the parameter list
        animation_parameters = SetLightParameters(animations=[animation])
 
        response = handler_input.response_builder.speak(
            speech="This animation is red, then blue, then red. "
            "It repeats three times.").set_should_end_session(False)\
            .add_directive(SetLightDirective(
                version=1, parameters=animation_parameters)).response

        # Write the response to CloudWatch.
        print("===RESPONSE===\n {}".format(response))

        # Return the response.
        return response

Button Press Animation Example

The following directive produces a white flash that fades to pink when you press and hold a button. Note that when you release the button, the animation goes away because the button up animation (which we didn't set here) replaces it.

Copied to clipboard.

{
    "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak>When you press and hold a button, this animation flashes white and then fades to pink.</speak>"
    },
    "shouldEndSession": false,
    "directives": [
        {
            "type": "GadgetController.SetLight",
            "version": 1,
            "targetGadgets": [],
            "parameters": {
                "triggerEvent": "buttonDown",
                "animations": [
                    {
                        "repeat": 1,
                        "targetLights": [
                            "1"
                        ],
                        "sequence": [
                            {
                                "durationMs": 10,
                                "color": "FFFFFF",
                                "blend": false
                            },
                            {
                                "durationMs": 200,
                                "color": "FFFFFF",
                                "blend": true
                            },
                            {
                                "durationMs": 1000,
                                "color": "FC0031",
                                "blend": true
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Node.js (v2).

const AnimateButtonsIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'AnimateButtonsIntent';
  },
  handle(handlerInput) {

    const response = handlerInput.responseBuilder
      .speak('When you press and hold a button, this animation flashes white '
              + 'and then fades to pink.')
      .withShouldEndSession(false)
      .addDirective
      (
            {
                'type': 'GadgetController.SetLight',
                'version': 1,
                'targetGadgets': [],
                'parameters': {
                    'triggerEvent': 'buttonDown',
                    'animations': [
                        {
                            'repeat': 1,
                            'targetLights': [
                                '1'
                            ],
                           'sequence': [
                               {
                                 'durationMs': 10,
                                 'color': 'FFFFFF', // White
                                 'blend': false
                               },
                               {
                                 'durationMs': 200,
                                 'color': 'FFFFFF', // Hold on white
                                 'blend': true
                               },
                               {
                                 'durationMs': 1000,
                                 'color': 'FC0031', // Fade to pink
                                 'blend': true
                               }
                           ]
                        }
                    ]
                }
            }
       )
      .getResponse();

      console.log('===RESPONSE=== '+ JSON.stringify(response));
      return response;

  }
};

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Java.

package com.amazon.ask.buttonsamples.handlers;

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.interfaces.gadgetController.*;
import com.amazon.ask.model.services.gadgetController.*;
import java.util.Optional;
import java.util.Arrays;
import java.util.List;
import static com.amazon.ask.request.Predicates.intentName;


public class AnimateButtonsIntentHandler implements RequestHandler {

    @Override
    public boolean canHandle(HandlerInput handlerInput) {
        return handlerInput.matches(intentName("AnimateButtonsIntent"));
    }

    @Override
    public Optional<Response> handle(HandlerInput handlerInput) {

      // Set the light to white.
      AnimationStep animationStep1 = AnimationStep.builder()
        .withDurationMs(10)
        .withColor("FFFFFF")
        .withBlend(false)
        .build();

      // Hold on white.
      AnimationStep animationStep2 = AnimationStep.builder()
        .withDurationMs(200)
        .withColor("FFFFFF")
        .withBlend(true)
        .build();

      // Fade to pink.
      AnimationStep animationStep3 = AnimationStep.builder()
        .withDurationMs(1000)
        .withColor("FC0031")
        .withBlend(true)
        .build();

      // Build an animation out of the steps defined previously.
      LightAnimation animation = LightAnimation.builder()
        .withRepeat(1)
        .withTargetLights((List<String>)Arrays.asList("1"))
        .withSequence((List<AnimationStep>)Arrays.asList(animationStep1,
                       animationStep2, animationStep3))
        .build();

      // Using the animation information defined previously, build
      // the parameter list.
      SetLightParameters animationParameters = SetLightParameters.builder()
        .withAnimations((List<LightAnimation>)Arrays.asList(animation))
        .withTriggerEvent(TriggerEventType.BUTTONDOWN)
        .build();

      // Assemble the final SetLight directive.
      SetLightDirective directive = SetLightDirective.builder()
        .withParameters(animationParameters)
        .withVersion(1)
        .build();

      // Assemble the response.
      Optional<Response> response = handlerInput.getResponseBuilder()
        .withSpeech("When you press and hold a button, this animation "
                     + "flashes white and then fades to pink.")
        .withShouldEndSession(false)
        .addDirective(directive)
        .build();

      // Write the response to CloudWatch.
      String responseLog = response.toString();
      responseLog = responseLog.replace("\n", " ").replace("\r", " ");
      System.out.println("===RESPONSE=== " + responseLog);

      // Return the response.
      return response;

   }
}

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Python.

from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_core.utils import is_intent_name
from ask_sdk_model import Response
from ask_sdk_model.interfaces.gadget_controller import SetLightDirective
from ask_sdk_model.services.gadget_controller import (
    AnimationStep, LightAnimation, SetLightParameters, TriggerEventType)


class AnimateButtonsIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return is_intent_name("AnimateButtonsIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        # Set the light to white
        animation_step_1 = AnimationStep(
            duration_ms=10, color="FFFFFF", blend=False)

        # Hold on white
        animation_step_2 = AnimationStep(
            duration_ms=200, color="FFFFFF", blend=True)

        # Fade to Pink
        animation_step_3 = AnimationStep(
            duration_ms=1000, color="FC0031", blend=True)

        # Build an animation out of the steps defined previously
        animation = LightAnimation(
            repeat=1, target_lights=["1"],
            sequence=[animation_step_1, animation_step_2, animation_step_3])

        # Using the animation information defined previously,
        # build the parameter list
        animation_parameters = SetLightParameters(
            trigger_event=TriggerEventType.buttonDown, animations=[animation])

        response = handler_input.response_builder.speak(
            speech="When you press and hold a button, "
                   "this animation flashes white and then fades to "
                   "pink").set_should_end_session(False).add_directive(
            SetLightDirective(
                version=1, parameters=animation_parameters)).response

        # Write the response to CloudWatch.
        print("===RESPONSE===\n {}".format(response))

        # Return the response.
        return response

Example of Setting Animations for All Three Triggers

The following directive sets the idle animation to red, the down animation to green, and the up animation to yellow.

Copied to clipboard.

{
    "outputSpeech": {
        "type": "SSML",
        "ssml": "<speak>Setting the idle animation to red, the down animation to green, and the up animation to yellow.</speak>"
    },
    "shouldEndSession": false,
    "directives": [
        {
            "type": "GadgetController.SetLight",
            "version": 1,
            "targetGadgets": [],
            "parameters": {
                "triggerEvent": "none",
                "animations": [
                    {
                        "repeat": 1,
                        "targetLights": [
                            "1"
                        ],
                        "sequence": [
                            {
                                "durationMs": 60000,
                                "color": "FF0000",
                                "blend": false
                            }
                        ]
                    }
                ]
            }
        },
        {
            "type": "GadgetController.SetLight",
            "version": 1,
            "targetGadgets": [],
            "parameters": {
                "triggerEvent": "buttonDown",
                "animations": [
                    {
                        "repeat": 1,
                        "targetLights": [
                            "1"
                        ],
                        "sequence": [
                            {
                                "durationMs": 1000,
                                "color": "00CC00",
                                "blend": true
                            }
                        ]
                    }
                ]
            }
        },
        {
            "type": "GadgetController.SetLight",
            "version": 1,
            "targetGadgets": [],
            "parameters": {
                "triggerEvent": "buttonUp",
                "animations": [
                    {
                        "repeat": 1,
                        "targetLights": [
                            "1"
                        ],
                        "sequence": [
                            {
                                "durationMs": 500,
                                "color": "FFFF00",
                                "blend": true
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Node.js (v2).

const AnimateButtonsIntentHandler = {
   canHandle(handlerInput) {
      return handlerInput.requestEnvelope.request.type === 'IntentRequest'
      && handlerInput.requestEnvelope.request.intent.name === 'AnimateButtonsIntent';
   },

   handle(handlerInput) {

    const response = handlerInput.responseBuilder
      .speak('Setting the idle animation to red, the down animation to green, '
              + ' and the up animation to yellow.')
      .withShouldEndSession(false)
      .addDirective
      (
            {
                'type': 'GadgetController.SetLight',
                'version': 1,
                'targetGadgets': [],
                'parameters': {
                    'triggerEvent': 'none',
                    'animations': [
                        {
                            'repeat': 1,
                            'targetLights': [
                                '1'
                            ],
                            'sequence': [
                                {
                                    'durationMs': 60000,
                                    'color': 'FF0000',
                                    'blend': false
                                }
                            ]
                        }
                    ]
                }
            }
       )
      .addDirective
      (
            {
                'type': 'GadgetController.SetLight',
                'version': 1,
                'targetGadgets': [],
                'parameters': {
                    'triggerEvent': 'buttonDown',
                    'animations': [
                        {
                            'repeat': 1,
                            'targetLights': [
                                '1'
                            ],
                            'sequence': [
                                {
                                    'durationMs': 1000,
                                    'color': '00CC00',
                                    'blend': true
                                }
                            ]
                        }
                    ]
                }
            }
       )
      .addDirective
      (
            {
                'type': 'GadgetController.SetLight',
                'version': 1,
                'targetGadgets': [],
                'parameters': {
                    'triggerEvent': 'buttonUp',
                    'animations': [
                        {
                            'repeat': 1,
                            'targetLights': [
                                '1'
                            ],
                            'sequence': [
                                {
                                    'durationMs': 500,
                                    'color': 'FFFF00',
                                    'blend': true
                                }
                            ]
                        }
                    ]
                }
            }
       )
      .getResponse();

      console.log('===RESPONSE=== '+ JSON.stringify(response));
      return response;

   }
};

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Java.

package com.amazon.ask.buttonsamples.handlers;

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.interfaces.gadgetController.*;
import com.amazon.ask.model.services.gadgetController.*;
import java.util.Optional;
import java.util.Arrays;
import java.util.List;
import static com.amazon.ask.request.Predicates.intentName;


public class AnimateButtonsIntentHandler implements RequestHandler {

    @Override
    public boolean canHandle(HandlerInput handlerInput) {
        return handlerInput.matches(intentName("AnimateButtonsIntent"));
    }

    @Override
    public Optional<Response> handle(HandlerInput handlerInput) {

      // Build the animation step that we will use for idling
      // (i.e., the "none" trigger): red.
      AnimationStep animationStepIdle = AnimationStep.builder()
        .withDurationMs(60000)
        .withColor("FF0000")
        .withBlend(false)
        .build();

      // Build the animation step that we will use for button down:
      // 1000 ms of green.
      AnimationStep animationStepButtonDown = AnimationStep.builder()
        .withDurationMs(1000)
        .withColor("00CC00")
        .withBlend(true)
        .build();

      // Build the animation step that we will use for button up:
      // 500 ms of yellow.
      AnimationStep animationStepButtonUp = AnimationStep.builder()
        .withDurationMs(500)
        .withColor("FFFF00")
        .withBlend(true)
        .build();

      // Build the animation that we will use for idling
      // (i.e., the "none" trigger). The animation consists
      // of one step, defined previously.
      LightAnimation animationIdle = LightAnimation.builder()
        .withRepeat(1)
        .withTargetLights((List<String>)Arrays.asList("1"))
        .withSequence((List<AnimationStep>)Arrays.asList(animationStepIdle))
        .build();

      // Build the animation that we will use for button down.
      // The animation consists of one step, defined previously.
      LightAnimation animationButtonDown = LightAnimation.builder()
        .withRepeat(1)
        .withTargetLights((List<String>)Arrays.asList("1"))
        .withSequence((List<AnimationStep>)Arrays.asList(animationStepButtonDown))
        .build();

      // Build the animation that we will use for button up.
      // The animation consists of one step, defined previously.
      LightAnimation animationButtonUp = LightAnimation.builder()
        .withRepeat(1)
        .withTargetLights((List<String>)Arrays.asList("1"))
        .withSequence((List<AnimationStep>)Arrays.asList(animationStepButtonUp))
        .build();

      // Using the animation information defined previously, build
      // the parameter list for idling (i.e., the "none" trigger).
      SetLightParameters animationParametersIdle = SetLightParameters.builder()
        .withAnimations((List<LightAnimation>)Arrays.asList(animationIdle))
        .withTriggerEvent(TriggerEventType.NONE)
        .build();

      // Using the animation information defined previously,
      // build the parameter list for button down.
      SetLightParameters animationParametersButtonDown = SetLightParameters.builder()
        .withAnimations((List<LightAnimation>)Arrays.asList(animationButtonDown))
        .withTriggerEvent(TriggerEventType.BUTTONDOWN)
        .build();

      // Using the animation information defined previously,
      // build the parameter list for button down.
      SetLightParameters animationParametersButtonUp = SetLightParameters.builder()
        .withAnimations((List<LightAnimation>)Arrays.asList(animationButtonUp))
        .withTriggerEvent(TriggerEventType.BUTTONUP)
        .build();

      // Assemble the final SetLight directive for idling.
      SetLightDirective directiveIdle = SetLightDirective.builder()
        .withParameters(animationParametersIdle)
        .withVersion(1)
        .build();

      // Assemble the final SetLight directive for button down.
      SetLightDirective directiveButtonDown = SetLightDirective.builder()
        .withParameters(animationParametersButtonDown)
        .withVersion(1)
        .build();

      // Assemble the final SetLight directive for button up.
      SetLightDirective directiveButtonUp = SetLightDirective.builder()
        .withParameters(animationParametersButtonUp)
        .withVersion(1)
        .build();

      // Assemble the response.
      Optional<Response> response = handlerInput.getResponseBuilder()
        .withSpeech("Setting the idle animation to red, the down animation to green, "
                     + "and the up animation to yellow.")
        .withShouldEndSession(false)
        .addDirective(directiveIdle)
        .addDirective(directiveButtonUp)
        .addDirective(directiveButtonDown)
        .build();

      // Write the response to CloudWatch.
      String responseLog = response.toString();
      responseLog = responseLog.replace("\n", " ").replace("\r", " ");
      System.out.println("===RESPONSE=== " + responseLog);

      // Return the response.
      return response;

   }
}

Copied to clipboard.

This sample code uses the Alexa Skills Kit SDK for Python.

from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_core.utils import is_intent_name
from ask_sdk_model import Response
from ask_sdk_model.interfaces.gadget_controller import SetLightDirective
from ask_sdk_model.services.gadget_controller import (
    AnimationStep, LightAnimation, SetLightParameters, TriggerEventType)


class AnimateButtonsIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return is_intent_name("AnimateButtonsIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        #   Build the animation step that we will use for idling
        #   (i.e., the "none" trigger): red.
        animation_step_idle = AnimationStep(
            duration_ms=60000, color="FF0000", blend=False)

        #   Build the animation step that we will use for button down:
        #   1000 ms of green.
        animation_step_button_down = AnimationStep(
            duration_ms=1000, color="00CC00", blend=True)

        #  Build the animation step that we will use for button up:
        #  500 ms of yellow.
        animation_step_button_up = AnimationStep(
            duration_ms=500, color="FFFF00", blend=True)

        # Build the animation that we will use for idling
        # (i.e., the "none" trigger). The animation consists
        # of one step, defined previously.
        animation_idle = LightAnimation(
            repeat=1, target_lights=["1"], sequence=[animation_step_idle])

        # Build the animation that we will use for button down.
        # The animation consists of one step, defined previously.
        animation_button_down = LightAnimation(
            repeat=1, target_lights=["1"],
            sequence=[animation_step_button_down])

        # Build the animation that we will use for button up.
        # The animation consists of one step, defined previously.
        animation_button_up = LightAnimation(
            repeat=1, target_lights=["1"], sequence=[animation_step_button_up])

        # Using the animation information defined previously, build
        # the parameter list for idling (i.e., the "none" trigger).
        animation_parameters_idle = SetLightParameters(
            trigger_event=TriggerEventType.none, animations=[animation_idle])

        # Using the animation information defined previously,
        # build the parameter list for button down.
        animation_parameters_button_down = SetLightParameters(
            trigger_event=TriggerEventType.none,
            animations=[animation_button_down])

        # Using the animation information defined previously,
        # build the parameter list for button down.
        animation_parameters_button_up = SetLightParameters(
            trigger_event=TriggerEventType.none,
            animations=[animation_button_up])

        #  Assemble the final SetLight directive for idling.
        directive_idle = SetLightDirective(
            version=1, parameters=animation_parameters_idle)

        # Assemble the final SetLight directive for button down.
        directive_button_down = SetLightDirective(
            version=1, parameters=animation_parameters_button_down)

        # Assemble the final SetLight directive for button up.
        directive_button_up = SetLightDirective(
            version=1, parameters=animation_parameters_button_up)

        # Assemble the response.
        response = handler_input.response_builder.speak(
            speech="Setting the idle animation to red, the down "
                   "animation to green and the up animation to "
                   "yellow.").set_should_end_session(
            False).add_directive(
            directive_idle).add_directive(
            directive_button_up).add_directive(
            directive_button_down).response

        # Write the response to CloudWatch.
        print("===RESPONSE===\n {}".format(response))

        # Return the response.
        return response