ralyxa 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +35 -0
- data/LICENSE.txt +21 -0
- data/README.md +161 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/ralyxa/card.rb +51 -0
- data/lib/ralyxa/handler.rb +32 -0
- data/lib/ralyxa/output_speech.rb +33 -0
- data/lib/ralyxa/register_intents.rb +44 -0
- data/lib/ralyxa/request.rb +25 -0
- data/lib/ralyxa/response.rb +44 -0
- data/lib/ralyxa/response_builder.rb +45 -0
- data/lib/ralyxa/skill.rb +32 -0
- data/lib/ralyxa/version.rb +3 -0
- data/lib/ralyxa.rb +8 -0
- data/ralyxa.gemspec +26 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6e3f8442dcb51d6e780ebc37bc846b2929351af0
|
4
|
+
data.tar.gz: 5278a2bada22ba5f69207780e4114a8890793de0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ef36564dafddabaae15059ff9218d44bd3b17b28394a52596ebe3ac6665643f54c55a05c35b8aa7da1eb908087791367960fde26f560de1c437dc22eec49ee47
|
7
|
+
data.tar.gz: 1fd77d6e2e8d7f1a45e5bb3a789d04f833f5ddb9443d649c4825bcf0b722bb038ba63e04465a3c9ddeed1754f645a2ab0313c0c13993cb4d0b45d5c8e93edd7e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -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/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ralyxa (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.6.0)
|
12
|
+
rspec-core (~> 3.6.0)
|
13
|
+
rspec-expectations (~> 3.6.0)
|
14
|
+
rspec-mocks (~> 3.6.0)
|
15
|
+
rspec-core (3.6.0)
|
16
|
+
rspec-support (~> 3.6.0)
|
17
|
+
rspec-expectations (3.6.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.6.0)
|
20
|
+
rspec-mocks (3.6.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.6.0)
|
23
|
+
rspec-support (3.6.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.14)
|
30
|
+
rake (~> 10.0)
|
31
|
+
ralyxa!
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.14.6
|
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,161 @@
|
|
1
|
+
# Ralyxa
|
2
|
+
|
3
|
+
A Ruby framework for interacting with Amazon Alexa. Designed to work with Sinatra, although can be used with a few other web frameworks.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ralyxa'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ralyxa
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Getting Started
|
24
|
+
|
25
|
+
First, you've gotta pass the request from Sinatra to Ralyxa. Add a single `POST` route to your Sinatra application, with the following:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'sinatra'
|
29
|
+
require './lib/alexa'
|
30
|
+
|
31
|
+
post '/' do
|
32
|
+
Alexa::Skill.handle(request)
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Second, you've gotta define your intents. To define a new intent, create a directory called 'intents'. Inside there, create a `.rb` file:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
intent "IntentName" do
|
40
|
+
# Whatever logic you want to do
|
41
|
+
# fetching something for the response
|
42
|
+
# persisting something etc
|
43
|
+
# this is plain Ruby, so go wild
|
44
|
+
|
45
|
+
respond("This is what Alexa will say to the user")
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
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).
|
50
|
+
|
51
|
+
### Being more pro
|
52
|
+
|
53
|
+
##### `ask` and `tell`
|
54
|
+
|
55
|
+
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.
|
56
|
+
|
57
|
+
When defining intents, you can use the `#ask` and `#tell` methods in place of `#respond` to keep the session open, or close it:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
intent "AskMoreQuestions" do
|
61
|
+
ask("What next?")
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
intent "SayGoodbye" do
|
67
|
+
tell("Goodbye.")
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
> A `tell` is basically a `#respond` with `end_session: true`. You can use that instead if you prefer.
|
72
|
+
|
73
|
+
##### Reading and setting session attributes
|
74
|
+
|
75
|
+
You can persist data to an Alexa session:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
intent "PersistThis" do
|
79
|
+
ask("Got it. What now?", session_attributes: { persist: "this" })
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
And, subsequently, read it:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
intent "ReadFromSession" do
|
87
|
+
persisted_data = request.session_attribute("persist")
|
88
|
+
ask("You persisted: #{ persisted_data }")
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
> Go check out the `Alexa::Request` object to see what else you can do with the `request`.
|
93
|
+
|
94
|
+
##### Ending sessions
|
95
|
+
|
96
|
+
If, for some reason, you want to end a session in some other way than with a `tell`, you can:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
intent "ConfuseTheUser" do
|
100
|
+
respond("This actually ends the session.", end_session: true)
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
##### Starting over
|
105
|
+
|
106
|
+
You can start conversations over, which clears the session attributes:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
intent "AMAZON.StartOverIntent" do
|
110
|
+
ask("Starting over. What next?", start_over: true)
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
##### Using SSML
|
115
|
+
|
116
|
+
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:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
intent "SpellOut" do
|
120
|
+
ask("<speak><say-as interpret-as='spell-out'>Hello World</say-as></speak>", ssml: true)
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
##### Using Cards
|
125
|
+
|
126
|
+
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:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
# Simple card
|
130
|
+
intent "SendSimpleCard" do
|
131
|
+
simple_card = card("Hello World", "I'm alive!")
|
132
|
+
ask("What do you think of the Simple card I just sent?", card: simple_card)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Standard card
|
136
|
+
intent "SendStandardCard" do
|
137
|
+
standard_card = card("Hello World", "I'm alive!", "http://placehold.it/200")
|
138
|
+
ask("What do you think of the Standard card I just sent?", card: standard_card)
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
##### TODO
|
143
|
+
|
144
|
+
- Account linking :construction:
|
145
|
+
- Audio directives :construction:
|
146
|
+
- Reprompts :construction:
|
147
|
+
- Generator?
|
148
|
+
|
149
|
+
## Development
|
150
|
+
|
151
|
+
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.
|
152
|
+
|
153
|
+
## Contributing
|
154
|
+
|
155
|
+
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.
|
156
|
+
|
157
|
+
|
158
|
+
## License
|
159
|
+
|
160
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
161
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ralyxa"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/ralyxa/card.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Ralyxa
|
2
|
+
class Card
|
3
|
+
SIMPLE_CARD_TYPE = "Simple"
|
4
|
+
STANDARD_CARD_TYPE = "Standard"
|
5
|
+
|
6
|
+
def initialize(title, body, image_url)
|
7
|
+
@title = title
|
8
|
+
@body = body
|
9
|
+
@image_url = image_url
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.as_hash(title, body, image_url = nil)
|
13
|
+
new(title, body, image_url).to_h
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_h
|
17
|
+
Hash.new.tap do |card|
|
18
|
+
set_type(card)
|
19
|
+
card[:title] = @title
|
20
|
+
set_body(card)
|
21
|
+
set_image(card) if @image_url
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def set_type(card)
|
28
|
+
card[:type] = SIMPLE_CARD_TYPE if simple?
|
29
|
+
card[:type] = STANDARD_CARD_TYPE if standard?
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_body(card)
|
33
|
+
card[:content] = @body if simple?
|
34
|
+
card[:text] = @body if standard?
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_image(card)
|
38
|
+
card[:image] = Hash.new
|
39
|
+
card[:image][:smallImageUrl] = @image_url
|
40
|
+
card[:image][:largeImageUrl] = @image_url
|
41
|
+
end
|
42
|
+
|
43
|
+
def simple?
|
44
|
+
!@image_url
|
45
|
+
end
|
46
|
+
|
47
|
+
def standard?
|
48
|
+
!!@image_url
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative './response_builder'
|
2
|
+
require_relative './card'
|
3
|
+
|
4
|
+
module Ralyxa
|
5
|
+
class Handler
|
6
|
+
def initialize(request, intent_proc)
|
7
|
+
@request = request
|
8
|
+
@intent_proc = intent_proc
|
9
|
+
end
|
10
|
+
|
11
|
+
def handle
|
12
|
+
instance_eval &@intent_proc
|
13
|
+
end
|
14
|
+
|
15
|
+
def respond(response_text, response_details = {}, response_builder = Ralyxa::ResponseBuilder)
|
16
|
+
response_builder.build(response_details.merge(response_text: response_text))
|
17
|
+
end
|
18
|
+
|
19
|
+
def tell(response_text, response_details = {})
|
20
|
+
respond(response_text, response_details.merge(end_session: true))
|
21
|
+
end
|
22
|
+
|
23
|
+
def card(title, body, image_url = nil, card_class = Ralyxa::Card)
|
24
|
+
card_class.as_hash(title, body, image_url)
|
25
|
+
end
|
26
|
+
|
27
|
+
alias ask respond
|
28
|
+
|
29
|
+
attr_reader :request
|
30
|
+
private :request, :respond, :tell, :ask
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Ralyxa
|
2
|
+
class OutputSpeech
|
3
|
+
DEFAULT_RESPONSE_TEXT = "Hello World"
|
4
|
+
DEFAULT_RESPONSE_SSML = "<speak>Hello World</speak>"
|
5
|
+
|
6
|
+
def initialize(speech, ssml)
|
7
|
+
@speech = speech
|
8
|
+
@ssml = ssml
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_h
|
12
|
+
Hash.new.tap do |output_speech|
|
13
|
+
@ssml ? set_ssml(output_speech) : set_plaintext(output_speech)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.as_hash(speech: nil, ssml: false)
|
18
|
+
new(speech, ssml).to_h
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def set_ssml(output_speech)
|
24
|
+
output_speech[:type] = "SSML"
|
25
|
+
output_speech[:ssml] = (@speech || DEFAULT_RESPONSE_SSML).slice(0, 140)
|
26
|
+
end
|
27
|
+
|
28
|
+
def set_plaintext(output_speech)
|
29
|
+
output_speech[:type] = "PlainText"
|
30
|
+
output_speech[:text] = (@speech || DEFAULT_RESPONSE_TEXT).slice(0, 140)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative './skill'
|
2
|
+
|
3
|
+
module Ralyxa
|
4
|
+
class RegisterIntents
|
5
|
+
DEFAULT_INTENTS_DIRECTORY = "./intents".freeze
|
6
|
+
|
7
|
+
def initialize(intents_directory, alexa_skill_class)
|
8
|
+
@intents_directory = intents_directory
|
9
|
+
@alexa_skill_class = alexa_skill_class
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.run(intents_directory = DEFAULT_INTENTS_DIRECTORY, alexa_skill_class = Ralyxa::Skill)
|
13
|
+
new(intents_directory, alexa_skill_class).run
|
14
|
+
end
|
15
|
+
|
16
|
+
def run
|
17
|
+
warn NO_INTENT_DECLARATIONS_FOUND if intent_declarations.empty?
|
18
|
+
|
19
|
+
intent_declarations.each do |intent_declaration|
|
20
|
+
alexa_skill_class.class_eval intent_declaration
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
attr_reader :intents_directory, :alexa_skill_class
|
26
|
+
|
27
|
+
def intent_declarations
|
28
|
+
@intent_declarations ||=
|
29
|
+
Dir.glob("#{ intents_directory }/*.rb")
|
30
|
+
.map { |relative_path| File.expand_path(relative_path) }
|
31
|
+
.map { |intent_declaration_path| File.open(intent_declaration_path).read }
|
32
|
+
end
|
33
|
+
|
34
|
+
NO_INTENT_DECLARATIONS_FOUND = <<~HEREDOC
|
35
|
+
\e[33m
|
36
|
+
WARNING: You haven't defined any intents.
|
37
|
+
|
38
|
+
Please define intents inside a directory called 'intents',
|
39
|
+
on the same directory level as the runfile for your server application,
|
40
|
+
or change the default intents directory in alexa/register_intents.rb.
|
41
|
+
\e[0m
|
42
|
+
HEREDOC
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Ralyxa
|
4
|
+
class Request
|
5
|
+
def initialize(original_request)
|
6
|
+
@request = JSON.parse(original_request.body.read)
|
7
|
+
end
|
8
|
+
|
9
|
+
def intent_name
|
10
|
+
@request["request"]["intent"]["name"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def slot_value(slot_name)
|
14
|
+
@request["request"]["intent"]["slots"][slot_name]["value"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def new_session?
|
18
|
+
@request["session"]["new"]
|
19
|
+
end
|
20
|
+
|
21
|
+
def session_attribute(attribute_name)
|
22
|
+
@request["session"]["attributes"][attribute_name]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative './output_speech'
|
2
|
+
|
3
|
+
module Ralyxa
|
4
|
+
class Response
|
5
|
+
def initialize(output_speech, session_attributes, end_session, start_over, card)
|
6
|
+
@output_speech = output_speech
|
7
|
+
@session_attributes = session_attributes
|
8
|
+
@end_session = end_session
|
9
|
+
@start_over = start_over
|
10
|
+
@card = card
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_h
|
14
|
+
Hash.new.tap do |response|
|
15
|
+
set_version(response)
|
16
|
+
set_session_attributes(response)
|
17
|
+
set_response(response)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.as_hash(output_speech: Ralyxa::OutputSpeech.as_hash, session_attributes: {}, end_session: false, start_over: false, card: false)
|
22
|
+
new(output_speech, session_attributes, end_session, start_over, card).to_h
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
attr_reader :response
|
27
|
+
|
28
|
+
def set_version(response)
|
29
|
+
response[:version] = "1.0"
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_session_attributes(response)
|
33
|
+
return response[:sessionAttributes] = {} if @start_over
|
34
|
+
response[:sessionAttributes] = @session_attributes unless @session_attributes.empty?
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_response(response)
|
38
|
+
response[:response] = Hash.new
|
39
|
+
response[:response][:outputSpeech] = @output_speech
|
40
|
+
response[:response][:card] = @card if @card
|
41
|
+
response[:response][:shouldEndSession] = @end_session if @end_session
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'json'
|
2
|
+
require_relative './response'
|
3
|
+
require_relative './output_speech'
|
4
|
+
|
5
|
+
module Ralyxa
|
6
|
+
class ResponseBuilder
|
7
|
+
def initialize(response_class, output_speech_class, options)
|
8
|
+
@response_class = response_class
|
9
|
+
@output_speech_class = output_speech_class
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.build(options = {}, response_class = Ralyxa::Response, output_speech_class = Ralyxa::OutputSpeech)
|
14
|
+
new(response_class, output_speech_class, options).build
|
15
|
+
end
|
16
|
+
|
17
|
+
def build
|
18
|
+
merge_output_speech
|
19
|
+
merge_card if card_exists?
|
20
|
+
|
21
|
+
@response_class.as_hash(@options).to_json
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def merge_output_speech
|
27
|
+
@options.merge!(output_speech: output_speech)
|
28
|
+
end
|
29
|
+
|
30
|
+
def merge_card
|
31
|
+
@options[:card] = @options[:card].to_h
|
32
|
+
end
|
33
|
+
|
34
|
+
def card_exists?
|
35
|
+
@options[:card]
|
36
|
+
end
|
37
|
+
|
38
|
+
def output_speech
|
39
|
+
output_speech_params = { speech: @options.delete(:response_text) }
|
40
|
+
output_speech_params.merge!(ssml: @options.delete(:ssml)) if @options[:ssml]
|
41
|
+
|
42
|
+
@output_speech_class.as_hash(output_speech_params)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/ralyxa/skill.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require_relative './request'
|
2
|
+
require_relative './response'
|
3
|
+
require_relative './handler'
|
4
|
+
|
5
|
+
module Ralyxa
|
6
|
+
class Skill
|
7
|
+
@@intents = {}
|
8
|
+
|
9
|
+
def initialize(request)
|
10
|
+
@request = request
|
11
|
+
end
|
12
|
+
|
13
|
+
def handle(handler = Ralyxa::Handler)
|
14
|
+
intent_proc = self.class.registered_intents[@request.intent_name]
|
15
|
+
handler.new(@request, intent_proc).handle
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def intent(intent_name, &block)
|
20
|
+
@@intents[intent_name] = block
|
21
|
+
end
|
22
|
+
|
23
|
+
def registered_intents
|
24
|
+
@@intents.dup
|
25
|
+
end
|
26
|
+
|
27
|
+
def handle(request, alexa_request_wrapper = Ralyxa::Request)
|
28
|
+
new(alexa_request_wrapper.new(request)).handle
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/ralyxa.rb
ADDED
data/ralyxa.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ralyxa/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ralyxa"
|
8
|
+
spec.version = Ralyxa::VERSION
|
9
|
+
spec.authors = ["Sam Morgan"]
|
10
|
+
spec.email = ["samm@makersacademy.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby framework for interacting with Amazon Alexa.}
|
13
|
+
spec.description = %q{A Ruby framework for interacting with Amazon Alexa. Designed to work with Sinatra, although can be used with a few other web frameworks.}
|
14
|
+
spec.homepage = "https://github.com/sjmog/ralyxa"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
spec.required_ruby_version = '>= 2.3.1'
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ralyxa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Morgan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: A Ruby framework for interacting with Amazon Alexa. Designed to work
|
56
|
+
with Sinatra, although can be used with a few other web frameworks.
|
57
|
+
email:
|
58
|
+
- samm@makersacademy.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/ralyxa.rb
|
74
|
+
- lib/ralyxa/card.rb
|
75
|
+
- lib/ralyxa/handler.rb
|
76
|
+
- lib/ralyxa/output_speech.rb
|
77
|
+
- lib/ralyxa/register_intents.rb
|
78
|
+
- lib/ralyxa/request.rb
|
79
|
+
- lib/ralyxa/response.rb
|
80
|
+
- lib/ralyxa/response_builder.rb
|
81
|
+
- lib/ralyxa/skill.rb
|
82
|
+
- lib/ralyxa/version.rb
|
83
|
+
- ralyxa.gemspec
|
84
|
+
homepage: https://github.com/sjmog/ralyxa
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 2.3.1
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.6.6
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: A Ruby framework for interacting with Amazon Alexa.
|
108
|
+
test_files: []
|