• Stars
    star
    180
  • Rank 211,878 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 7 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Ruby framework for interacting with Amazon Alexa.

Ralyxa

Build Status

A Ruby framework for interacting with Amazon Alexa. Designed to work with Sinatra, although can be used with a few other web frameworks.

An example application implementing the gem can be played with here.

Installation

Add this line to your application's Gemfile:

gem 'ralyxa'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ralyxa

Usage

Getting Started

First, you've gotta pass the request from Sinatra to Ralyxa. Add a single POST route to your Sinatra application, with the following:

require 'sinatra'
require 'ralyxa'

post '/' do
  Ralyxa::Skill.handle(request)
end

Second, you've gotta define your intents. To define a new intent, create a directory called 'intents'. Inside there, create a .rb file:

intent "IntentName" do
  # Whatever logic you want to do
  # fetching something for the response
  # persisting something etc
  # this is plain Ruby, so go wild

  respond("This is what Alexa will say to the user")
end

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).

Being more pro

ask and tell

There are two kinds of responses you can send to Alexa: asks and tells. An ask should ask the user a question, and expect them to reply. A tell should end the conversation.

When defining intents, you can use the #ask and #tell methods in place of #respond to keep the session open, or close it:

intent "AskMoreQuestions" do
  ask("What next?")
end
intent "SayGoodbye" do
  tell("Goodbye.")
end

A tell is basically a #respond with end_session: true. You can use that instead if you prefer.

Reading and setting session attributes

You can persist data to an Alexa session:

intent "PersistThis" do
  ask("Got it. What now?", session_attributes: { persist: "this" })
end

And, subsequently, read it:

intent "ReadFromSession" do
  persisted_data = request.session_attribute("persist")
  ask("You persisted: #{ persisted_data }")
end
Playing audio with the AudioPlayer directive
Play

You can play an audio stream right away with:

intent "PlayAudio" do
  audio_player.play(
    'https://s3.amazonaws.com/my-ssml-samples/Flourish.mp3',
    'flourish-token',
    speech: 'Playing Audio'
  )
end
Play Later (Enqueue)

You can queue a song to play next with:

intent "PlayAudioLater" do
  audio_player.play_later(
    'https://s3.amazonaws.com/my-ssml-samples/Flourish.mp3',
    'flourish-token'
  )
end
Stop

You can stop playing with:

intent "StopAudio" do
  audio_player.stop
end
Clear Queued

You can clear enqueued audio with:

intent "ClearQueue" do
  audio_player.clear_queue
end
Reading the session user

You can read the session user's userId and accessToken, and check that the accessToken exists:

request.user_id #=> returns the `userId` value from the request session
request.user_access_token # => returns the `accessToken` value from the request session
request.user_access_token_exists? # => true if the user has an access token, false if not

Go check out the Alexa::Request object to see what else you can do with the request.

Ending sessions

If, for some reason, you want to end a session in some other way than with a tell, you can:

intent "ConfuseTheUser" do
  respond("This actually ends the session.", end_session: true)
end
Starting over

You can start conversations over, which clears the session attributes:

intent "AMAZON.StartOverIntent" do
  ask("Starting over. What next?", start_over: true)
end
Using SSML

You can use Speech Synthesis Markup Language to directly control Alexa's pronunciation:

intent "SpellOut" do
  ask("<speak><say-as interpret-as='spell-out'>Hello World</say-as></speak>", ssml: true)
end
Using Cards

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:

# Simple card
intent "SendSimpleCard" do
  simple_card = card("Hello World", "I'm alive!")
  ask("What do you think of the Simple card I just sent?", card: simple_card)
end

# Standard card
intent "SendStandardCard" do
  standard_card = card("Hello World", "I'm alive!", "http://placehold.it/720x480", "http://placehold.it/1200x800")
  
  ask("What do you think of the Standard card I just sent?", card: standard_card)
end

Card images must be under 2MB and available at an SSL-enabled (HTTPS) endpoint.

Account Linking

You can ask Alexa to send a LinkAccount card for the user to authenticate via OAuth:

intent "SendAccountLinkingCard" do
  tell("Please authorize via the Alexa app.", card: link_account_card)
end

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?.

If, for example, you wanted to require authorization for an intent called SecretIntent:

intent "SecretIntent" do
  return tell("Please authorize via the Alexa app", card: link_account_card) unless request.user_access_token_exists?
  ask("Welcome to the secret zone. What's next?")
end

Ephemera

Alexa says there's a problem if I just fail to reply to a prompt!

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:

intent "SessionEndedRequest" do
  respond
end

You can't actually respond to a SessionEndedRequest, but you might want to do some tidying in this action.

I want to serve card images, audio stream etc. over HTTP not HTTPS

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.

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.

Ralyxa.configure do |config|
  config.require_secure_urls = false
end

Testing

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. This verification is built into Ralyxa and can cause issues when testing your skills with stubbed data.

Disabling verification

Inside of your spec_helper files, include the following to disable verification:

RSpec

require 'ralyxa'

RSpec.configure do |config|
  config.before :each do
    Ralyxa.configure do |config|
      config.validate_requests = false
    end
  end
end

Development

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.

Contributing

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 code of conduct.

The main areas of focus are:

  • Reprompts 🚧
  • Dialogue 🚧
  • Generators of built-in Intents e.g. SessionEndedRequest
  • Automation with the AVS command line tool

License

The gem is available as open source under the terms of the MIT License.

More Repositories

1

airport_challenge_exemplar

Ruby
4
star
2

sinatra_session_global_funs

'Fun' with Sinatra, session and global variables.
Ruby
3
star
3

pizza_buddy

An intermediate application built with Alexa, Sinatra, and Ralyxa.
Ruby
2
star
4

climacons-widget

A beautiful responsive widget based on the awesome Climacons from Adam Whitcroft, animated by Noah Blon.
CSS
2
star
5

battle

A game of Battle.
Ruby
1
star
6

initiative_tracker

Track D&D Initiatives
JavaScript
1
star
7

rails-react-starter

A starter template for Rails with React.
Ruby
1
star
8

town_generator

Generate Towns and Cities for Dungeons and Dragons 5th Edition.
CSS
1
star
9

three-tier-web-architecture

Play with the 3TWA to understand it!
JavaScript
1
star
10

seedling

A Rails/React/Tailwind starter application.
JavaScript
1
star
11

rest

A little discovery tool for learning about REST.
JavaScript
1
star
12

locomotivecms_template

A Rails app that uses the LocomotiveCMS engine for content management.
Ruby
1
star
13

editab.le

Turn any HTML template into an editable one with CMS (and Hosting)
1
star
14

css_example

Examples of CSS with our brave leader.
HTML
1
star
15

likeMe

A likeMe app.
1
star
16

data-engineering-in-the-cloud

A really cool module for learning data engineering in a simulated setting.
1
star
17

rps-refactor

A refactoring challenge for a working Rock, Paper, Scissors game.
Ruby
1
star
18

elsa2.4k

Elsa's site.
CSS
1
star
19

card_frontend

A sweet BEM list of cards.
CSS
1
star
20

sinatra_game

A physical activity to understand web architecture.
HTML
1
star
21

ovr

The best way to read the news.
Ruby
1
star
22

GigggsPrototype

The inner workings of Gigggs.
JavaScript
1
star