-
Notifications
You must be signed in to change notification settings - Fork 0
/
LambdaFunction_Multipass.py
86 lines (80 loc) · 2.99 KB
/
LambdaFunction_Multipass.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
def lambda_handler(event, context):
import boto3
should_end_session = True
card_title = 'Multipass'
topiccmdPath = "$aws/things/Multipass/shadow/command"
topicstatusPath = "$aws/things/Multipass/shadow/update"
#AlexaCmds = ["Power","Channel","Volume","Mute"]
#AlexaDirs = ["Up","Down"]
#IRCmds = ["Power","ChannelUp","ChannelDown","VolumeUp","VolumeDown","Mute"]
#Check Request Type
SendToShadow = ""
AlexaResp = ""
if event['request']['type'] == "IntentRequest":
#Check and convert Alexa to IRCommands
KeyStr = event['request']['intent']['slots']['Keys']['value'].decode('utf-8').upper()
if KeyStr == "CHANNEL":
try:
DirStr = event['request']['intent']['slots']['Direction']['value'].decode('utf-8').upper()
except:
AlexaResp = "Dort"
DirStr = ""
if DirStr == "UP":
KeyStr = "CHANNELUP"
elif DirStr == "DOWN":
KeyStr = "CHANNELDOWN"
else:
AlexaResp = "Dort"
elif KeyStr == "VOLUME":
try:
DirStr = event['request']['intent']['slots']['Direction']['value'].decode('utf-8').upper()
except:
AlexaResp = "Dort"
DirStr = ""
if DirStr == "UP":
KeyStr = "VOLUMEUP"
elif DirStr == "DOWN":
KeyStr = "VOLUMEDOWN"
else:
AlexaResp = "Dort"
elif KeyStr == "POWER":
KeyStr = "POWER"
elif KeyStr == "MUTE":
KeyStr = "MUTE"
else:
# Stanard "Bad" response
AlexaResp = "Dort"
# Update the Device Shadow if not bad response
if AlexaResp != "Dort":
client = boto3.client('iot-data', region_name='us-east-1')
#Error Trap this as extra credit
#response = client.get_thing_shadow(thingName='Multipass')
SendToShadow = '{"message": "' + SendToShadow + '" }'
response = client.publish(topic=topiccmdPath,qos=1,payload=SendToShadow)
AlexaResp = "Ba-da-boom"
return (build_response({}, build_speechlet_response(card_title, AlexaResp, None, should_end_session)))
def build_speechlet_response(title, output, reprompt_text, should_end_session):
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'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
}