-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcouch-lights-lambda.py
executable file
·160 lines (135 loc) · 4.72 KB
/
couch-lights-lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from __future__ import print_function
import requests
import ujson
import os
import webcolors
apiurl = 'https://api.particle.io/v1/devices'
# --------------- Helpers that build all of the responses ----------------------
def build_speechlet_response(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
#'type': 'PlainText',
'type': 'SSML',
#'text': output
'ssml': '<speak>' + output + '</speak>'
},
'card': {
'type': 'Simple',
'title': "SessionSpeechlet - " + title,
'content': "SessionSpeechlet - " + output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
def build_response(session_attributes, speechlet_response):
return {
'version': '1.0',
'sessionAttributes': session_attributes,
'response': speechlet_response
}
# Basic function to fetch and parse
def fetchResults(url, args=None):
if args is not None:
data = {'access_token': os.environ['particleToken'], 'args': args}
else:
data = {'access_token': os.environ['particleToken']}
try:
response = requests.post(url, data = data)
response = ujson.loads(response.text)
except:
response = None
return response
def turnOn():
url = "%s/%s/On" % (apiurl, os.environ['particleID'])
print("Calling: %s" % url)
results = fetchResults(url)
print(results)
if not results:
response = "I'm sorry, something went wrong"
else:
try:
response = results['error']
except:
response = "OK"
speech_output = response
reprompt_text = "Please try again"
card_title = "Turn On"
session_attributes = {}
should_end_session = True
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def setColour(colour):
url = "%s/%s/setColour" % (apiurl, os.environ['particleID'])
results = fetchResults(url, args=colour)
if not results:
response = "I'm sorry, something went wrong"
else:
try:
response = results['error']
except:
response = "OK"
speech_output = response
reprompt_text = "Please try again"
card_title = "Set Colour"
session_attributes = {}
should_end_session = True
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def turnOff():
url = "%s/%s/Off" % (apiurl, os.environ['particleID'])
results = fetchResults(url)
if not results:
response = "I'm sorry, something went wrong"
else:
try:
response = results['error']
except:
response = "OK"
speech_output = response
reprompt_text = "Please try again"
card_title = "Turn Off"
session_attributes = {}
should_end_session = True
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def on_intent(intent_request, session):
""" Called when the user specifies an intent for this skill """
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
print(intent)
# Dispatch to your skill's intent handlers
if intent_name == "turnOn":
return turnOn()
elif intent_name == "turnOff":
return turnOff()
elif intent_name == "setColour":
colour = intent['slots']['colour']['value']
colour = colour.replace(' ', '')
try:
colour = webcolors.name_to_hex(colour)
except:
raise ValueError("Couldn't grok colour")
return setColour(colour)
else:
raise ValueError("Invalid intent")
# --------------- 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'] != "<APPLICATION_ID>"):
# raise ValueError("Invalid Application ID")
if event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])