GadgetController Interface Reference
The Gadget Controller interface enables your skill to control Echo Buttons. This interface works with compatible Amazon Echo devices only. With the Gadget Controller interface, you can send animations to illuminate the buttons with different colors in a specific order.
- Configuring Your Skill to Use the Gadget Controller Interface
- Gadget Controller Directives
- Gadget Controller Requests
- Examples
- Service Interface Reference (JSON)
Configuring Your Skill to Use the Gadget Controller Interface
To use Gadget Controller directives, you must specify the Gadget Controller interface in your skill metadata.
Gadget Controller Directives
The Gadget Controller interface provides the following directives:
Directive | Description |
---|---|
GadgetController.SetLight |
Sends Alexa a command to modify the behavior of connected Echo Buttons. For examples of how to assemble this directive using the Alexa Skills Kit (ASK) SDKs, see Examples. |
When including a directive in your response, set the type
property to the directive that you want to send. Include directives in the directives
array in your response. The following is the format of a request with a GadgetController.SetLight
directive in it.
{
"version": "1.0",
"sessionAttributes": {},
"shouldEndSession": boolean,
"response": {
"outputSpeech": string,
"reprompt": string,
"directives": [
{
"type": "GadgetController.SetLight",
"version": 1,
"targetGadgets": [ "gadgetId1", "gadgetId2" ],
"parameters": {
"triggerEvent": string,
"triggerEventTimeMs": number,
"animations": [
{
"repeat": number,
"targetLights": ["1"],
"sequence": [
{
"durationMs": number,
"blend": boolean,
"color": string
}
]
}
]
}
}
]
}
}
For the full response format, see Response Format in the JSON Interface Reference for Custom Skills.
SetLight Directive
Sends a command to animate the LEDs of connected Echo Buttons. For examples of how to assemble this directive using the Alexa Skills Kit (ASK) SDKs, see Examples.
The following example shows the general form of the 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"
}
]
}
]
}
}
Parameter | Description | Type | Required |
---|---|---|---|
type |
Must be set to GadgetController.SetLight . |
string |
yes |
version |
The version of the directive. Must be set to 1 . |
number |
yes |
targetGadgets |
The gadget IDs that will receive the command. An empty array, or leaving this parameter out, signifies that all gadgets will receive the command. | array |
no |
parameters |
Arguments that pertain to animating the buttons. | object |
yes |
parameters object
This object contains instructions on how to animate the buttons.
Parameter | Description | Type | Required |
---|---|---|---|
|
The action that triggers the animation. 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. |
|
yes |
|
The delay in milliseconds to wait after the trigger event before playing the animation. Minimum: 0. Maximum: 65,535. |
|
yes |
|
One animation. An animation contains one or more sequences of instructions to be performed in a specific order. (An animation can contain multiple sequences, but there can be only one animation per |
|
yes |
animations object
This object contains a sequence of instructions to be performed in a specific order, along with the number of times to play the overall animation.
Parameter | Description | Type | Required |
---|---|---|---|
|
The number of times to play this animation. Minimum: 0. Maximum: 255. |
|
yes |
|
An array of strings that represent the light addresses on the target gadgets that this animation will be applied to. Because the Echo Button has one light only, use |
|
yes |
|
The animation steps to render in order. The maximum number of steps that you can define depends on the number of target gadgets that you specify. To calculate the maximum number of steps that you can include in the sequence array for a given trigger, use the following formula:
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. The minimum number of steps 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 must have the following fields, all of which are required:
|
|
yes |
Gadget Controller Requests
This section contains the types of requests that Alexa might send your skill when you use the Gadget Controller interface.
System.ExceptionEncountered Request
If a GadgetController
directive that you send fails (for example, you send an invalid animation sequence), then your skill will be invoked with a standard System.ExceptionEncountered
request. Any directives included in the response are ignored.
{
"type": "System.ExceptionEncountered",
"requestId": "string",
"timestamp": "string",
"locale": "string",
"error": {
"type": "string",
"message": "string"
},
"cause": {
"requestId": "string"
}
}
Parameters
Parameter | Description | Type |
---|---|---|
type |
System.ExceptionEncountered |
string |
requestId |
A unique identifier for the specific request. | string |
timestamp |
Provides the date and time when Alexa sent the request as an ISO 8601 formatted string. Used to verify the request when hosting your skill as a web service. | string |
locale |
A string indicating the user's locale. For example: en-US . |
string |
error |
An object with error information. | object |
error.type |
The specific type of error (INVALID_RESPONSE , DEVICE_COMMUNICATION_ERROR , INTERNAL_ERROR ). |
string |
error.message |
A description of the error the device has encountered. | string |
cause.requestId |
The requestId for the request that caused the error. |
string |
Valid Response Types
Your skill cannot return a response to System.ExceptionEncountered
.
Examples
This section provides examples of how to assemble a SetLight
directive using the ASK SDKs.
- Immediate Animation Example
- Button Press Animation Example
- Example of Setting Animations for All Three Triggers
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.
{
"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
}
]
}
]
}
}
]
}
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;
}
};
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;
}
}
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.
{
"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
}
]
}
]
}
}
]
}
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;
}
};
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;
}
}
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.
{
"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
}
]
}
]
}
}
]
}
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;
}
};
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;
}
}
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
Service Interface Reference (JSON)
Request Format and Standard Request Types:
- Request and Response JSON Reference
- Request Types Reference (LaunchRequest, IntentRequest, SessionEndedRequest)
Interfaces:
- AudioPlayer Interface
- Connections Interface
- Dialog Interface
- Display Interface
- GadgetController Interface (this document)
- GameEngine Interface
- PlaybackController Interface
- VideoApp Interface