Simple library to interface with the Alexa Skills Kit
The primary way to interact with this library is by extending the AlexaSkillsRuby::Handler
class. Create a subclass and
register event handlers.
The following handlers are available:
on_authenticate
- called before checking the ApplicationIDon_session_start
- called first if the request is flagged as a new sessionon_launch
- called for a LaunchRequeston_session_end
- called for a SessionEndedRequeston_intent
called for an IntentRequest. Takes an optional string argument that specifies which intents it will be trigged on
In event handlers, the following methods are available:
request
- returns the current Request object, aAlexaSkillsRuby::JsonObjects::BaseRequest
session
- returns the current Session object, aAlexaSkillsRuby::JsonObjects::Session
session_attributes
- Hash containing the current session attributes. If the response is not flagged to end the session, all values will be sent with the responseresponse
- returns the current Response object, aAlexaSkillsRuby::JsonObjects::Response
application_id
- returns the value specified in the constructor optionslogger
- returns the value specified in the constructor options
The AlexaSkillsRuby::Handler
constructor takes an options hash and processes the following keys:
application_id
- If set, will raise aAlexaSkillsRuby::InvalidApplicationId
if a request's application_id does not matchlogger
- Will be available through thelogger
method in the handler; not otherwise used by the base class
require 'sinatra'
require 'alexa_skills_ruby'
class CustomHandler < AlexaSkillsRuby::Handler
on_intent("GetZodiacHoroscopeIntent") do
slots = request.intent.slots
response.set_output_speech_text("Horiscope Text")
response.set_simple_card("title", "content")
logger.info 'GetZodiacHoroscopeIntent processed'
end
end
post '/' do
content_type :json
handler = CustomHandler.new(application_id: ENV['APPLICATION_ID'], logger: logger)
begin
handler.handle(request.body.read)
rescue AlexaSkillsRuby::InvalidApplicationId => e
logger.error e.to_s
403
end
end