class AlexaToolbox::Response
Attributes
card[R]
player_response[RW]
reprompt[R]
response[R]
session_attributes[R]
session_end[RW]
speech[R]
version[R]
Public Class Methods
new(player_response = false, ssml = false, version = '1.0')
click to toggle source
Every response will have a version, response, and shouldEndSession
# File lib/alexa_toolbox/response.rb, line 8 def initialize(player_response = false, ssml = false, version = '1.0') @session_attributes = Hash.new @version = version @directives = [] @player_response = player_response @ssml = ssml @session_end = true @reprompt = nil @speech = nil end
Public Instance Methods
add_audio_clear_directive(clear_all = false)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 74 def add_audio_clear_directive(clear_all = false) @directives << { 'type' => 'AudioPlayer.ClearQueue', 'clearBehavior' => clear_all ? "CLEAR_ALL" : "CLEAR_ENQUEUED" } end
add_audio_play_directive(url, play_behavior = '', token = '', expected_previous_token = '', offset = 0)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 58 def add_audio_play_directive(url, play_behavior = '', token = '', expected_previous_token = '', offset = 0) directive = { 'type' => 'AudioPlayer.Play', 'playBehavior' => play_behavior, 'audioItem' => { 'stream' => { 'token' => token, 'url' => url, 'offsetInMilliseconds' => offset } } } directive['audioItem']['stream']['expectedPreviousToken'] = expected_previous_token if play_behavior == "ENQUEUE" @directives << directive end
add_audio_stop_directive()
click to toggle source
# File lib/alexa_toolbox/response.rb, line 81 def add_audio_stop_directive @directives << { 'type' => 'AudioPlayer.Stop' } end
add_card(type = nil, title = nil, content = nil, smallImageUrl = nil, largeImageUrl = nil)
click to toggle source
“type”: “string”, “title”: “string”, “content”: “string” Standard uses :text instead of :content
# File lib/alexa_toolbox/response.rb, line 91 def add_card(type = nil, title = nil, content = nil, smallImageUrl = nil, largeImageUrl = nil) @card = Hash.new @card[:type] = type.nil? ? 'Simple' : type if @card[:type] != "LinkAccount" @card[:title] = title unless title.nil? @card[:type] = 'Simple' if smallImageUrl.nil? && largeImageUrl.nil? if @card[:type] == 'Simple' @card[:content] = content unless content.nil? else @card[:text] = content unless content.nil? @card[:image] = { :smallImageUrl => smallImageUrl, :largeImageUrl => largeImageUrl } end end end
add_display_directive_object(display_directive)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 54 def add_display_directive_object(display_directive) @directives << display_directive.build_directive end
add_hash_card(card)
click to toggle source
Add a card as a single hash
# File lib/alexa_toolbox/response.rb, line 118 def add_hash_card(card) card[:type] = 'Simple' if card[:type].nil? @card = card end
add_permission_card(full_address = false)
click to toggle source
Add Permission Card for Location Information
# File lib/alexa_toolbox/response.rb, line 110 def add_permission_card(full_address = false) @card = Hash.new @card[:type] = 'AskForPermissionsConsent' permission = full_address ? "read::alexa:device:all:address" : "read::alexa:device:all:address:country_and_postal_code" @card[:permissions] = [permission] end
add_plain_speech(speech_text)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 33 def add_plain_speech(speech_text) @speech = { :type => 'PlainText', :text => speech_text } end
add_reprompt(speech_text, ssml = nil)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 41 def add_reprompt(speech_text, ssml = nil) ssml = ssml.nil? ? @ssml : ssml if ssml @reprompt = { "outputSpeech" => { :type => 'SSML', :ssml => check_ssml(speech_text) } } else @reprompt = { "outputSpeech" => { :type => 'PlainText', :text => speech_text } } end end
add_session_attribute(key, value)
click to toggle source
Adds a key,value pair to the session object.
# File lib/alexa_toolbox/response.rb, line 20 def add_session_attribute(key, value) @session_attributes[key.to_sym] = AlexaToolbox.transform_keys_to_symbols(value) end
add_speech(speech_text, ssml = nil)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 24 def add_speech(speech_text, ssml = nil) ssml = ssml.nil? ? @ssml : ssml if ssml @speech = { :type => 'SSML', :ssml => check_ssml(speech_text) } else @speech = { :type => 'PlainText', :text => speech_text } end end
add_ssml_reprompt(speech_text)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 50 def add_ssml_reprompt(speech_text) @reprompt = { "outputSpeech" => { :type => 'SSML', :ssml => check_ssml(speech_text) } } end
add_ssml_speech(speech_text)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 37 def add_ssml_speech(speech_text) @speech = { :type => 'SSML', :ssml => check_ssml(speech_text) } end
build_player_response_object(session_end = true)
click to toggle source
For Responses to AudioPlayer
or PlaybackController Requests Cannot Include: outputSpeech, card, reprompt, shouldEndSession
# File lib/alexa_toolbox/response.rb, line 136 def build_player_response_object(session_end = true) @response = Hash.new @response[:directives] = @directives unless @directives.empty? @response end
build_response(session_end = nil, player_response = nil, json = true)
click to toggle source
Builds a response object, can be json or hash if json is false
# File lib/alexa_toolbox/response.rb, line 143 def build_response(session_end = nil, player_response = nil, json = true) is_player_response = player_response.nil? ? @player_response : player_response end_session = session_end.nil? ? @session_end : session_end response = Hash.new if is_player_response response_object = build_player_response_object(end_session) else response_object = build_standard_response_object(end_session) response[:sessionAttributes] = @session_attributes unless @session_attributes.empty? end response[:version] = @version response[:response] = response_object json ? JSON.parse(response.to_json) : response end
build_standard_response_object(session_end = true)
click to toggle source
The response object as hash (with outputspeech, cards and session end)
# File lib/alexa_toolbox/response.rb, line 124 def build_standard_response_object(session_end = true) @response = Hash.new @response[:outputSpeech] = @speech unless @speech.nil? @response[:directives] = @directives unless @directives.empty? @response[:card] = @card unless @card.nil? @response[:reprompt] = @reprompt unless @reprompt.nil? @response[:shouldEndSession] = session_end @response end
Private Instance Methods
check_ssml(ssml_string)
click to toggle source
# File lib/alexa_toolbox/response.rb, line 160 def check_ssml(ssml_string) ssml_string = ssml_string.strip[0..6] == "<speak>" ? ssml_string : "<speak>" + ssml_string ssml_string.strip[-8..-1] == "</speak>" ? ssml_string : ssml_string + "</speak>" end