-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.py
184 lines (157 loc) · 6.53 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import requests
import os
import json
import time
import myq
USERNAME = "<MYQ_LOGIN_USERNAME>"
PASSWORD = "<MYQ_LOGIN_PASSWORD>"
mq = myq.MyQ(USERNAME,PASSWORD)
def lambda_handler(event, context):
if event['session']['application']['applicationId'] != "amzn1.ask.skill.<your-alexa-skills-id>":
print "Invalid Application ID"
raise
else:
# Not using sessions for now
sessionAttributes = {}
mq.login()
mq.get_device_id()
if event['session']['new']:
onSessionStarted(event['request']['requestId'], event['session'])
if event['request']['type'] == "LaunchRequest":
speechlet = onLaunch(event['request'], event['session'])
response = buildResponse(sessionAttributes, speechlet)
elif event['request']['type'] == "IntentRequest":
speechlet = onIntent(event['request'], event['session'])
response = buildResponse(sessionAttributes, speechlet)
elif event['request']['type'] == "SessionEndedRequest":
speechlet = onSessionEnded(event['request'], event['session'])
response = buildResponse(sessionAttributes, speechlet)
# Return a response for speech output
return(response)
# Called when the session starts
def onSessionStarted(requestId, session):
print("onSessionStarted requestId=" + requestId + ", sessionId=" + session['sessionId'])
# Called when the user launches the skill without specifying what they want.
def onLaunch(launchRequest, session):
# Dispatch to your skill's launch.
getWelcomeResponse()
# Called when the user specifies an intent for this skill.
def onIntent(intentRequest, session):
intent = intentRequest['intent']
intentName = intentRequest['intent']['name']
# Dispatch to your skill's intent handlers
if intentName == "StateIntent":
return stateResponse(intent)
elif intentName == "MoveIntent":
return moveIntent(intent)
elif intentName == "HelpIntent":
return getWelcomeResponse()
else:
print "Invalid Intent (" + intentName + ")"
raise
# Called when the user ends the session.
# Is not called when the skill returns shouldEndSession=true.
def onSessionEnded(sessionEndedRequest, session):
# Add cleanup logic here
print "Session ended"
def getWelcomeResponse():
cardTitle = "Welcome"
speechOutput = """You can open or close your garage door by saying, ask my garage door to open."""
# If the user either does not reply to the welcome message or says something that is not
# understood, they will be prompted again with this text.
repromptText = 'Ask me to close your garage door by saying ask my garge door to close'
shouldEndSession = True
return (buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))
def moveIntent(intent):
"""
Ask my garage to {open|close|shut|go up|go down}
"intent": {
"name": "StateIntent",
"slots": {
"doorstate": {
"name": "doorstate",
"value": "close"
}
}
}
"""
if (intent['slots']['doorstate']['value'] == "close") or (intent['slots']['doorstate']['value'] == "shut") or (intent['slots']['doorstate']['value'] == "go down"):
mq.close()
speechOutput = "Ok, I'm closing your garage door"
cardTitle = "Closing your garage door"
elif (intent['slots']['doorstate']['value'] == "open") or (intent['slots']['doorstate']['value'] == "go up"):
mq.open()
speechOutput = "Ok, I'm opening your garage door"
cardTitle = speechOutput
else:
speechOutput = "I didn't understand that. You can say ask the garage door to open or close"
cardTitle = "Try again"
repromptText = "I didn't understand that. You can say ask the garage door if it's open, or tell it to open or close"
shouldEndSession = True
return(buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))
def stateResponse(intent):
"""
Ask my garage door if it's {open|closed}
"intent": {
"name": "StateIntent",
"slots": {
"doorstate": {
"name": "doorstate",
"value": "closed"
}
}
}
"""
doorstate = mq.status()
if (intent['slots']['doorstate']['value'] == "open") or (intent['slots']['doorstate']['value'] == "up"):
if doorstate == "open":
speechOutput = "Yes, your garage door is open"
cardTitle = "Yes, your garage door is open"
elif doorstate == "closed":
speechOutput = "No, your garage door is closed"
cardTitle = "No, your garage door is closed"
else:
speechOutput = "Your garage door is " + doorstate
cardTitle = "Your garage door is " + doorstate
elif (intent['slots']['doorstate']['value'] == "closed") or (intent['slots']['doorstate']['value'] == "shut") or (intent['slots']['doorstate']['value'] == "down"):
if doorstate == "closed":
speechOutput = "Yes, your garage door is closed"
cardTitle = "Yes, your garage door is closed"
elif doorstate == "open":
speechOutput = "No, your garage door is open"
cardTitle = "No, your garage door is open"
else:
speechOutput = "Your garage door is " + doorstate
cardTitle = "Your garage door is " + doorstate
else:
speechOutput = "I didn't understand that. You can say ask the garage door if it's open"
cardTitle = "Try again"
repromptText = "I didn't understand that. You can say ask the garage door if it's open"
shouldEndSession = True
return(buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))
# --------------- Helpers that build all of the responses -----------------------
def buildSpeechletResponse(title, output, repromptText, shouldEndSession):
return ({
"outputSpeech": {
"type": "PlainText",
"text": output
},
"card": {
"type": "Simple",
"title": "MyQ - " + title,
"content": "MyQ - " + output
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": repromptText
}
},
"shouldEndSession": shouldEndSession
})
def buildResponse(sessionAttributes, speechletResponse):
return ({
"version": "1.0",
"sessionAttributes": sessionAttributes,
"response": speechletResponse
})