-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlambda_function.py
More file actions
157 lines (138 loc) · 4.54 KB
/
lambda_function.py
File metadata and controls
157 lines (138 loc) · 4.54 KB
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
from __future__ import print_function
import json
import boto3
config_file = open('lambda_config.json', 'r')
config = json.loads(config_file.read())
sqs_url = config['sqs_url']
sqs = boto3.client('sqs')
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'])
if event['session']['new']:
on_session_started({'requestId': event['request']['requestId']},
event['session'])
if event['request']['type'] == "LaunchRequest":
return on_launch(event['request'], event['session'])
elif event['request']['type'] == "IntentRequest":
return on_intent(event['request'], event['session'])
elif event['request']['type'] == "SessionEndedRequest":
return on_session_ended(event['request'], event['session'])
def send_message(body, attributes):
response = sqs.send_message(
QueueUrl=sqs_url,
MessageBody=body,
MessageAttributes=attributes)
return response
def on_launch(request, session):
output = "Please tell me a song you would like to hear"
title = "Welcome"
reprompt = None
should_end_session = True
return build_response({}, build_speechlet_response(title, output, reprompt, should_end_session))
def on_intent(request, session):
# Defaults
should_end_session = True
reprompt_text = None
title = ''
output = ''
if request['intent']['name'] == 'sendAudioIntent':
query = request['intent']['slots']['Query']['value']
output = "Ok. You asked me to play %s" % query
title = "Chromecast Play"
send_message(
query,
{
'query': {
'StringValue': query,
'DataType': 'String'
},
'type': {
'StringValue': 'query',
'DataType': 'String'
}
})
if request['intent']['name'] == 'AMAZON.StopIntent':
title = "Chromecast Stop"
output = "Stopping"
send_message(
'Stop Request',
{
'type': {
'StringValue': 'stop_request',
'DataType': 'String'
}
})
if request['intent']['name'] == 'AMAZON.PauseIntent':
title = "Chromecast Pause"
output = "Pausing"
send_message(
'Pause Request',
{
'type': {
'StringValue': 'pause_request',
'DataType': 'String'
}
})
if request['intent']['name'] == 'AMAZON.ResumeIntent':
title = "Chromecast Resume"
output = "Resuming"
send_message(
'Resume Request',
{
'type': {
'StringValue': 'resume_request',
'DataType': 'String'
}
})
return build_response({}, build_speechlet_response(title, output, reprompt_text, should_end_session))
def on_session_started(request, session):
pass
def on_session_ended(request, session):
pass
def build_speechlet_response(title, output, reprompt_text, should_end_session):
if output == None:
return {
'shouldEndSession': should_end_session
}
elif title == None:
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'reprompt': {
'outputSpeech': {
'type': 'PlainText',
'text': reprompt_text
}
},
'shouldEndSession': should_end_session
}
else:
return {
'outputSpeech': {
'type': 'PlainText',
'text': output
},
'card': {
'type': 'Simple',
'title': title,
'content': 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
}