-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
117 lines (97 loc) · 4 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
import boto3
import json
import urllib
import urllib2
def lambda_handler(event, context):
if event['session']['application']['applicationId'] != "amzn1.echo-sdk-ams.app.<your-alexa-skills-id>":
print "Invalid Application ID"
else:
sessionAttributes = {}
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 == "BrewIntent":
return brewIntent(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 brewIntent(intent):
repromptText = "You can say brew me a cup of coffee"
shouldEndSession = True
speechOutput = "Ok, I'm brewing your coffee"
cardTitle = speechOutput
# Make a call to AWS IoT to update the shadow. The Raspberry Pi
# connected to the Keurig watches the shadow topic for instructions
# to start the brew
client = boto3.client('iot-data')
response = client.update_thing_shadow(
thingName='coffee',
payload=b'{"state" : { "desired" : { "brew" : "start" }}}'
)
repromptText = "I didn't understand that."
shouldEndSession = True
return(buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))
def getWelcomeResponse():
sessionAttributes = {}
cardTitle = "Welcome"
speechOutput = """You can ask me to brew you a cup of coffee by saying brew me a cup of coffee"""
# 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 = speechOutput
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": "SessionSpeechlet - " + title,
"content": "SessionSpeechlet - " + output
},
"reprompt": {
"outputSpeech": {
"type": "PlainText",
"text": repromptText
}
},
"shouldEndSession": shouldEndSession
})
def buildResponse(sessionAttributes, speechletResponse):
return ({
"version": "1.0",
"sessionAttributes": sessionAttributes,
"response": speechletResponse
})