/
lambda.rb
264 lines (236 loc) · 7.21 KB
/
lambda.rb
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
require 'json'
require 'open-uri'
require 'rss'
REQUEST_TYPES = {
'LaunchRequest' => :launch,
'IntentRequest' => :intent,
'SessionEndedRequest' => :end
}
class AlexaResponse
def initialize(session)
@session = session
end
def to_json
@response.to_json
end
def none
build_response({ :end_session => true })
end
# quick helpers
def speak_text(text, end_sess = true, attribute_obj = {})
build_response(self.class.speech_options(text))
if !end_sess
@response['response']['shouldEndSession'] = false
end
attribute_obj.each do |k,v|
@response['sessionAttributes'][k] = v
end
self
end
def speak_ssml(ssml)
build_response(self.class.speech_options(nil, ssml))
self
end
def play_mp3(url)
build_response(self.class.mp3_options(url))
self
end
def with_card(card)
add_card({:card => card})
self
end
def with_reprompt(reprompt)
add_reprompt({:reprompt => reprompt})
self
end
# use these to build the options for reprompt arg or card arg
def self.speech_options(text, ssml = nil)
text ? { :text=>text } : { :ssml=>ssml }
end
def self.mp3_options(url)
{:ssml => "<speak><audio src=\"#{url}\" /></speak>"}
end
def self.card_options(card_title, card_content, image_small = nil, image_large = nil)
{ :card_title => card_title,
:card_content => card_content,
:image_small => image_small,
:image_large => image_large }
end
# details
def build_response(options)
@response = { 'version': '1.0' }
add_session(options)
add_response(options)
self
end
def add_session(options)
@response['sessionAttributes'] = @session.attributes || {}
end
def add_response(options)
@response['response'] = {
'shouldEndSession': options.nil? || options[:end_session].nil? ? true : !!options[:end_session]
}
add_output(options)
add_card(options)
add_reprompt(options)
end
def add_output(options)
@response['response']['outputSpeech'] = self.class.speech(options)
end
def add_card(options)
if options[:card]
@response['response']['card'] = self.class.card(options)
end
end
def add_reprompt(options)
if options[:reprompt]
@response['response']['shouldEndSession'] = false
@response['response']['reprompt'] = { 'outputSpeech': speech(options[:reprompt]) }
end
end
def self.speech(options)
if options[:ssml]
{
'type': 'SSML',
'ssml': options[:ssml]
}
else
{
'type': options[:type] || 'PlainText',
'text': options[:text]
}
end
end
def self.card(options)
card = {
'type': options[:card_type] || 'Simple',
'title': options[:card_title] || 'Card Title',
'content': options[:card_content] || 'Card Contents'
}
if options[:image_small] && options[:image_large]
card.merge!({
'type' => 'Standard',
'image' => {
'smallImageUrl' => options[:image_small],
'largeImageUrl' => options[:image_large],
}
})
end
card
end
end
class SiteRssParser
def get_titles(user_ag, start_val = 0, static = false)
rss_results = []
# Update with your feed
rss_url = "http://www.ruby-lang.org/en/feeds/news.rss"
rss = RSS::Parser.parse(open(rss_url, 'User-Agent' => user_ag).read, false).items[start_val..start_val+4].to_a
if static
rss = RSS::Parser.parse(open(rss_url, 'User-Agent' => user_ag).read, false).items[start_val..static-1].to_a
end
rss.each_with_index do |result, result_i|
res_ind = (start_val + result_i + 1).to_s
final_result = res_ind + ") " + result.title.to_s
rss_results.push(final_result)
end
return rss_results = rss_results.join(". ").gsub("<title>","").gsub("</title>","")
end
end
def lambda_handler(event:, context:)
def smart_hash(h)
h = Hash[h.map{|k,v| [k, v.is_a?(Hash) ? smart_hash(v) : v]}]
h.instance_eval do
def method_missing(name, *args, &block)
self[name.to_s]
end
end
h
end
# Update with your skill ID
@application_id_check = 'amzn1.ask.skill.[YOUR ID HERE]'
@validate_application_on_every_request = true
def handler(event)
@event = smart_hash(event)
@version = @event.version
parse_session
parse_request
dispatch_session
dispatch_response
end
def parse_session
@session = @event.session
@new_session = @session.new
@session_id = @session.sessionId
@application_id = @session.application.applicationId
@session_attributes = @session.attributes || {}
@user_id = @session.user.userId
@access_token = @session.user.accessToken
end
def parse_request
@request = @event.request
@request_type = REQUEST_TYPES[@request.type]
@request_id = @request.requestId
@intent = @request_type == :intent ? @request.intent.name.sub(/\./,'_') : @request_type
p "Intent: " + @intent.to_s
end
def dispatch_session
on_new_session if @new_session
end
def dispatch_response
validate_application_id if @validate_application_on_every_request
@response = AlexaResponse.new(@session)
send("on_#{@intent}", @response)
end
#INTENTS START HERE
def on_new_session(); end
def on_launch(response)
set_step = 0;
if @session_attributes['step']
set_step = @session_attributes['step'] + 5
end
drp = SiteRssParser.new
got_titles = drp.get_titles(@request_id, set_step)
response.speak_text(got_titles, false, {'step': set_step})
end
def on_next(response)
set_step = 0;
if @session_attributes['step']
set_step = @session_attributes['step'] + 5
end
drp = SiteRssParser.new
got_titles = drp.get_titles(@request_id, set_step)
response.speak_text(got_titles, false, {'step': set_step})
end
def on_static(response)
drp = SiteRssParser.new
vals = @request.intent.slots.number.value.to_i rescue 5
got_titles = drp.get_titles(@request_id, 0, vals)
response.speak_text(got_titles, true)
end
#YOUR INTENTS END HERE
#AMAZON INTENTS START
def on_exit(response)
response.speak_text("see ya!")
end
def on_end(response)
response.speak_text("see ya!")
end
def on_AMAZON_FallbackIntent(response)
response.speak_text("something went wrong please invoke again", true)
end
def on_AMAZON_CancelIntent(response)
response.speak_text("see ya!")
end
def on_AMAZON_HelpIntent(response)
text = "Welcome to Dota 2 RSS Feed. I will read off the top five headlines then you can say continue or next to get the next five. If you only want a certain amount of headlines you can add first and a number to the invoke. Say 'next' to get the first 5 articles or help to hear this again."
response.speak_text(text, false)
end
def on_AMAZON_StopIntent(response)
response.speak_text("see ya!")
end
#AMAZON INTENTS START
def validate_application_id
raise "AppID #{@application_id} does not match" if @application_id != @application_id_check
end
handler(event)
end