Skip to content

Instantly share code, notes, and snippets.

@jamesbulpin
Created December 22, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesbulpin/b189b97f96dadd5ced9337b622073caa to your computer and use it in GitHub Desktop.
Save jamesbulpin/b189b97f96dadd5ced9337b622073caa to your computer and use it in GitHub Desktop.
AWS Lambda code for Alexa skill handler for WS2811 control via Octoblu
from __future__ import print_function
import urllib
import urllib2
octoblu_trigger = "https://triggers.octoblu.com/v2/flows/3791898c-6234-4c57-a860-92a8e6c616fc/triggers/6b5c0082-37ab-459b-9912-8228ccb43a15"
# --------------- 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"
else:
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 = response.read()
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=" +
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
function.
"""
if (event['session']['application']['applicationId'] !=
"amzn1.ask.skill.fb304a19-98ab-4a2b-9cc5-7608e933cf1c"):
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