Instantly share code, notes, and snippets.

What would you like to do?
AWS Lambda code for Alexa skill handler for WS2811 control via Octoblu
from __future__ import print_function
import urllib
import urllib2
octoblu_trigger = ""
# --------------- Helpers that build all of the responses ----------------------
def build_speechlet_response(output):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
def build_response(session_attributes, speechlet_response):
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
# --------------- Events ------------------
def on_intent(intent_request, session):
""" Called when the user specifies an intent for this skill """
print("on_intent requestId=" + intent_request['requestId'] +
", sessionId=" + session['sessionId'])
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
mode = None
color = None
text = None
session_attributes = {}
speech_output = None
# Dispatch to your skill's intent handlers
if intent_name == "ModeIntent":
if ('mode' in intent['slots']) and ('value' in intent['slots']['mode']):
mode = intent['slots']['mode']['value']
elif intent_name == "ColorIntent":
if ('color' in intent['slots']) and ('value' in intent['slots']['color']):
color = intent['slots']['color']['value']
mode = "solid"
elif intent_name == "TextIntent":
if ('text' in intent['slots']) and ('value' in intent['slots']['text']):
text = intent['slots']['text']['value']
mode = "dotmatrix"
elif intent_name == "AskOctobluIntent":
mode = "octoblu"
elif intent_name == "SorryIntent":
speech_output = "I'm sorry James, I can't do that"
raise ValueError("Invalid intent")
obmsg = {"debug":intent}
if mode:
obmsg["mode"] = mode
if color:
obmsg["color"] = color
if text:
obmsg["text"] = text
url = octoblu_trigger
data = urllib.urlencode(obmsg)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page =
if not speech_output:
speech_output = "OK"
return build_response(session_attributes, build_speechlet_response(speech_output))
def on_session_ended(session_ended_request, session):
""" Called when the user ends the session.
Is not called when the skill returns should_end_session=true
print("on_session_ended requestId=" + session_ended_request['requestId'] +
", sessionId=" + session['sessionId'])
# add cleanup logic here
# --------------- Main handler ------------------
def lambda_handler(event, context):
""" Route the incoming request based on type (LaunchRequest, IntentRequest,
etc.) The JSON body of the request is provided in the event parameter.
print("event.session.application.applicationId=" +
Uncomment this if statement and populate with your skill's application ID to
prevent someone else from configuring a skill that sends requests to this
if (event['session']['application']['applicationId'] !=
raise ValueError("Invalid Application ID")
if event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment