ralyxa-lambda 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +16 -0
  5. data/.ruby-version +1 -0
  6. data/.tool-versions +1 -0
  7. data/.travis.yml +12 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/CONTRIBUTING.md +19 -0
  10. data/Gemfile +3 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +294 -0
  13. data/Rakefile +6 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/lib/ralyxa.rb +36 -0
  17. data/lib/ralyxa/configuration.rb +18 -0
  18. data/lib/ralyxa/errors.rb +4 -0
  19. data/lib/ralyxa/handler.rb +47 -0
  20. data/lib/ralyxa/register_intents.rb +45 -0
  21. data/lib/ralyxa/request_entities/request.rb +63 -0
  22. data/lib/ralyxa/request_entities/user.rb +25 -0
  23. data/lib/ralyxa/response_builder.rb +65 -0
  24. data/lib/ralyxa/response_entities/card.rb +89 -0
  25. data/lib/ralyxa/response_entities/directives.rb +9 -0
  26. data/lib/ralyxa/response_entities/directives/audio.rb +11 -0
  27. data/lib/ralyxa/response_entities/directives/audio/audio_item.rb +23 -0
  28. data/lib/ralyxa/response_entities/directives/audio/stream.rb +37 -0
  29. data/lib/ralyxa/response_entities/directives/audio_player.rb +46 -0
  30. data/lib/ralyxa/response_entities/directives/audio_player/clear_queue.rb +27 -0
  31. data/lib/ralyxa/response_entities/directives/audio_player/play.rb +32 -0
  32. data/lib/ralyxa/response_entities/directives/audio_player/stop.rb +19 -0
  33. data/lib/ralyxa/response_entities/output_speech.rb +35 -0
  34. data/lib/ralyxa/response_entities/reprompt.rb +20 -0
  35. data/lib/ralyxa/response_entities/response.rb +54 -0
  36. data/lib/ralyxa/skill.rb +53 -0
  37. data/lib/ralyxa/version.rb +3 -0
  38. data/pkg/ralyxa-1.0.0.gem +0 -0
  39. data/ralyxa.gemspec +32 -0
  40. metadata +195 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b587c2395aa13bbf2cec83953c02a902e87189b4918c9104fb252980889d13f0
4
+ data.tar.gz: 798c7e3541fe24bf097733257c6c10a61c76a5253ebb9f07b51d3159912aa5de
5
+ SHA512:
6
+ metadata.gz: 3b0c06e79501105585eda22cfd8a87d82a88492a6af42879d27cfae1c1b712ac08cbddcbe2ddef93a9c48f82075ca33329d5382c704e9d224eb794954c68573b
7
+ data.tar.gz: c86ab796dbd4fd9f212376c938aa34659bcc46507b2846f9a7ab39bbe775eb630b7973e1fa62d00569ee41d498801c4de644bcf9e0dcff351639e935f785f916
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ Gemfile.lock
3
+ coverage
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ AllCops:
2
+ Exclude:
3
+ - spec/**/*.rb
4
+ TargetRubyVersion: 2.3.1
5
+
6
+ Metrics/LineLength:
7
+ Enabled: false
8
+
9
+ Metrics/ParameterLists:
10
+ Max: 6
11
+
12
+ Style/FrozenStringLiteralComment:
13
+ Enabled: false
14
+
15
+ Documentation:
16
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.5.0
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ - ruby-head
7
+
8
+ matrix:
9
+ allow_failures:
10
+ - rvm: ruby-head
11
+
12
+ script: bundle exec rspec spec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at s_morgan@me.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,19 @@
1
+ # Contributing to Ralyxa
2
+
3
+ 1. Have tests.
4
+
5
+ ## The basic idea
6
+
7
+ Ralyxa is in two parts:
8
+
9
+ 1. The user-facing Ruby interface which looks a bit like Sinatra.
10
+ 2. The under-the-hood Ruby-to-JSON translator which directly references Alexa structures.
11
+
12
+ This gives rise to some principles:
13
+
14
+ 1. The user-facing Ruby interface should try to provide thoughtful abstractions to the Alexa structures.
15
+ 2. The under-the-hood Ruby-to-JSON translator should contain objects which directly map onto Alexa structures.
16
+
17
+ Some other principles:
18
+
19
+ 1. Keep the user-facing Ruby interface as simple as possible. Imagine a brand-new Ruby developer using this.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Sam Morgan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,294 @@
1
+ # Ralyxa
2
+
3
+ [![Build Status](https://travis-ci.org/sjmog/ralyxa.svg?branch=master)](https://travis-ci.org/sjmog/ralyxa)
4
+
5
+ A Ruby framework for interacting with Amazon Alexa. Designed to work with Sinatra, although can be used with a few other web frameworks.
6
+
7
+ An example application implementing the gem can be played with [here](https://github.com/sjmog/ralyxa_example).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ralyxa'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ralyxa
24
+
25
+ ## Usage
26
+
27
+ ### Getting Started
28
+
29
+ First, you've gotta pass the request from Sinatra to Ralyxa. Add a single `POST` route to your Sinatra application, with the following:
30
+
31
+ ```ruby
32
+ require 'sinatra'
33
+ require 'ralyxa'
34
+
35
+ post '/' do
36
+ Ralyxa::Skill.handle(request)
37
+ end
38
+ ```
39
+
40
+ Second, you've gotta define your intents. To define a new intent, create a directory called 'intents'. Inside there, create a `.rb` file:
41
+
42
+ ```ruby
43
+ intent "IntentName" do
44
+ # Whatever logic you want to do
45
+ # fetching something for the response
46
+ # persisting something etc
47
+ # this is plain Ruby, so go wild
48
+
49
+ respond("This is what Alexa will say to the user")
50
+ end
51
+ ```
52
+
53
+ Third, define your Intent and Utterance on the Alexa Developer portal. You can then test your application in the Service Simulator (either by pushing the Sinatra app somewhere with HTTPS or, more easily, using ngrok to tunnel the application).
54
+
55
+ ### Being more pro
56
+
57
+ ##### `ask` and `tell`
58
+
59
+ There are two kinds of responses you can send to Alexa: `ask`s and `tell`s. An `ask` should ask the user a question, and expect them to reply. A `tell` should end the conversation.
60
+
61
+ When defining intents, you can use the `#ask` and `#tell` methods in place of `#respond` to keep the session open, or close it:
62
+
63
+ ```ruby
64
+ intent "AskMoreQuestions" do
65
+ ask("What next?")
66
+ end
67
+ ```
68
+
69
+ ```ruby
70
+ intent "SayGoodbye" do
71
+ tell("Goodbye.")
72
+ end
73
+ ```
74
+
75
+ > A `tell` is basically a `#respond` with `end_session: true`. You can use that instead if you prefer.
76
+
77
+ ##### Reading and setting session attributes
78
+
79
+ You can persist data to an Alexa session:
80
+
81
+ ```ruby
82
+ intent "PersistThis" do
83
+ ask("Got it. What now?", session_attributes: { persist: "this" })
84
+ end
85
+ ```
86
+
87
+ And, subsequently, read it:
88
+
89
+ ```ruby
90
+ intent "ReadFromSession" do
91
+ persisted_data = request.session_attribute("persist")
92
+ ask("You persisted: #{ persisted_data }")
93
+ end
94
+ ```
95
+
96
+ ##### Playing audio with the `AudioPlayer` directive
97
+
98
+ ###### Play
99
+
100
+ You can play an audio stream right away with:
101
+
102
+ ```ruby
103
+ intent "PlayAudio" do
104
+ audio_player.play(
105
+ 'https://s3.amazonaws.com/my-ssml-samples/Flourish.mp3',
106
+ 'flourish-token',
107
+ speech: 'Playing Audio'
108
+ )
109
+ end
110
+ ```
111
+
112
+ ###### Play Later (Enqueue)
113
+
114
+ You can queue a song to play next with:
115
+
116
+ ```ruby
117
+ intent "PlayAudioLater" do
118
+ audio_player.play_later(
119
+ 'https://s3.amazonaws.com/my-ssml-samples/Flourish.mp3',
120
+ 'flourish-token'
121
+ )
122
+ end
123
+ ```
124
+
125
+ ###### Stop
126
+
127
+ You can stop playing with:
128
+
129
+ ```ruby
130
+ intent "StopAudio" do
131
+ audio_player.stop
132
+ end
133
+ ```
134
+
135
+ ###### Clear Queued
136
+
137
+ You can clear enqueued audio with:
138
+
139
+ ```ruby
140
+ intent "ClearQueue" do
141
+ audio_player.clear_queue
142
+ end
143
+ ```
144
+
145
+ ##### Reading the session user
146
+
147
+ You can read the session user's `userId` and `accessToken`, and check that the `accessToken` exists:
148
+
149
+ ```ruby
150
+ request.user_id #=> returns the `userId` value from the request session
151
+ request.user_access_token # => returns the `accessToken` value from the request session
152
+ request.user_access_token_exists? # => true if the user has an access token, false if not
153
+ ```
154
+
155
+ > Go check out the `Alexa::Request` object to see what else you can do with the `request`.
156
+
157
+ ##### Ending sessions
158
+
159
+ If, for some reason, you want to end a session in some other way than with a `tell`, you can:
160
+
161
+ ```ruby
162
+ intent "ConfuseTheUser" do
163
+ respond("This actually ends the session.", end_session: true)
164
+ end
165
+ ```
166
+
167
+ ##### Starting over
168
+
169
+ You can start conversations over, which clears the session attributes:
170
+
171
+ ```ruby
172
+ intent "AMAZON.StartOverIntent" do
173
+ ask("Starting over. What next?", start_over: true)
174
+ end
175
+ ```
176
+
177
+ ##### Using SSML
178
+
179
+ You can use [Speech Synthesis Markup Language](https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/speech-synthesis-markup-language-ssml-reference) to directly control Alexa's pronunciation:
180
+
181
+ ```ruby
182
+ intent "SpellOut" do
183
+ ask("<speak><say-as interpret-as='spell-out'>Hello World</say-as></speak>", ssml: true)
184
+ end
185
+ ```
186
+
187
+ ##### Using Cards
188
+
189
+ You can send cards to the Alexa app. Ralyxa will automatically figure out if you're trying to send a 'Simple' or 'Standard' card type:
190
+
191
+ ```ruby
192
+ # Simple card
193
+ intent "SendSimpleCard" do
194
+ simple_card = card("Hello World", "I'm alive!")
195
+ ask("What do you think of the Simple card I just sent?", card: simple_card)
196
+ end
197
+
198
+ # Standard card
199
+ intent "SendStandardCard" do
200
+ standard_card = card("Hello World", "I'm alive!", "http://placehold.it/720x480", "http://placehold.it/1200x800")
201
+
202
+ ask("What do you think of the Standard card I just sent?", card: standard_card)
203
+ end
204
+ ```
205
+
206
+ > Card images must be under 2MB and available at an SSL-enabled (HTTPS) endpoint.
207
+
208
+ ##### Account Linking
209
+
210
+ You can ask Alexa to send a [`LinkAccount`](https://developer.amazon.com/blogs/post/Tx3CX1ETRZZ2NPC/Alexa-Account-Linking-5-Steps-to-Seamlessly-Link-Your-Alexa-Skill-with-Login-wit) card for the user to authenticate via OAuth:
211
+
212
+ ```ruby
213
+ intent "SendAccountLinkingCard" do
214
+ tell("Please authorize via the Alexa app.", card: link_account_card)
215
+ end
216
+ ```
217
+
218
+ After completing authentication, the user's access token is available via `request.user_access_token`. You can check for its existence with `request.user_access_token_exists?`.
219
+
220
+ If, for example, you wanted to require authorization for an intent called `SecretIntent`:
221
+
222
+ ```ruby
223
+ intent "SecretIntent" do
224
+ return tell("Please authorize via the Alexa app", card: link_account_card) unless request.user_access_token_exists?
225
+ ask("Welcome to the secret zone. What's next?")
226
+ end
227
+ ```
228
+
229
+ ## Ephemera
230
+
231
+ > Alexa says there's a problem if I just fail to reply to a prompt!
232
+
233
+ This is probably because your application is not handling the `SessionEndedRequest` intent. That's a built-in intent that kicks in after the user says 'exit', or nothing at all, in response to an ask. You'll probably see a warning in your server logs. To resolve it, implement the following intent:
234
+
235
+ ```ruby
236
+ intent "SessionEndedRequest" do
237
+ respond
238
+ end
239
+ ```
240
+
241
+ > You can't actually respond to a `SessionEndedRequest`, but you might want to do some tidying in this action.
242
+
243
+
244
+ ### I want to serve card images, audio stream etc. over HTTP not HTTPS
245
+
246
+ In some special cases, you may be allowed to serve content over HTTP instead of HTTPS. To allow this within Ralyxa, you need to set the `require_secure_urls` configuration option to false.
247
+
248
+ > **NOTE:** In order to use HTTP sources, you must be given special approval directly from Amazon. If you use HTTP sources without getting advanced approval, your skill will not work correctly.
249
+
250
+ ```ruby
251
+ Ralyxa.configure do |config|
252
+ config.require_secure_urls = false
253
+ end
254
+ ```
255
+
256
+
257
+ ## Testing
258
+ Part of Amazon's requirements for Alexa skills is that they have to ensure requests are sent from Amazon. This is done in a number of ways documented [here](https://developer.amazon.com/docs/custom-skills/host-a-custom-skill-as-a-web-service.html). This verification is built into Ralyxa and can cause issues when testing your skills with stubbed data.
259
+
260
+ ### Disabling verification
261
+ Inside of your spec_helper files, include the following to disable verification:
262
+
263
+ #### RSpec
264
+ ```ruby
265
+ require 'ralyxa'
266
+
267
+ RSpec.configure do |config|
268
+ config.before :each do
269
+ Ralyxa.configure do |config|
270
+ config.validate_requests = false
271
+ end
272
+ end
273
+ end
274
+ ```
275
+
276
+ ## Development
277
+
278
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rspec` to run the tests. You can also run `irb` for an interactive prompt that will allow you to experiment.
279
+
280
+ ## Contributing
281
+
282
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sjmog/ralyxa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
283
+
284
+ The main areas of focus are:
285
+
286
+ - Reprompts :construction:
287
+ - Dialogue :construction:
288
+ - Generators of built-in Intents e.g. `SessionEndedRequest`
289
+ - Automation with the `AVS` command line tool
290
+
291
+ ## License
292
+
293
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
294
+