Skip to content

Added Dialog Functionality #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions fixtures/response-dialogConfirmIntentDirective.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "1.0",
"response": {
"directives": [
{
"type": "Dialog.ConfirmIntent",
"updatedIntent": {
"name": "GetZodiacHoroscopeIntent",
"confirmationStatus": "NONE",
"slots": {
"City": {
"name": "City",
"confirmationStatus": "NONE"
},
"ZodiacSign": {
"name": "ZodiacSign",
"value": "virgo",
"confirmationStatus": "NONE"
}
}
}
}
],
"shouldEndSession" :true
}
}
27 changes: 27 additions & 0 deletions fixtures/response-dialogConfirmSlotDirective.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": "1.0",
"response": {
"directives": [
{
"type": "Dialog.ConfirmSlot",
"slotToConfirm": "ZodiacSign",
"updatedIntent": {
"name": "GetZodiacHoroscopeIntent",
"confirmationStatus": "NONE",
"slots": {
"City": {
"name": "City",
"confirmationStatus": "NONE"
},
"ZodiacSign": {
"name": "ZodiacSign",
"value": "virgo",
"confirmationStatus": "NONE"
}
}
}
}
],
"shouldEndSession" :true
}
}
26 changes: 26 additions & 0 deletions fixtures/response-dialogDelegateDirective.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "1.0",
"response": {
"directives": [
{
"type": "Dialog.Delegate",
"updatedIntent": {
"name": "GetZodiacHoroscopeIntent",
"confirmationStatus": "NONE",
"slots": {
"City": {
"name": "City",
"confirmationStatus": "NONE"
},
"ZodiacSign": {
"name": "ZodiacSign",
"value": "virgo",
"confirmationStatus": "NONE"
}
}
}
}
],
"shouldEndSession" :true
}
}
27 changes: 27 additions & 0 deletions fixtures/response-dialogElicitSlotDirective.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"version": "1.0",
"response": {
"directives": [
{
"type": "Dialog.ElicitSlot",
"slotToElicit": "City",
"updatedIntent": {
"name": "GetZodiacHoroscopeIntent",
"confirmationStatus": "NONE",
"slots": {
"City": {
"name": "City",
"confirmationStatus": "NONE"
},
"ZodiacSign": {
"name": "ZodiacSign",
"value": "virgo",
"confirmationStatus": "NONE"
}
}
}
}
],
"shouldEndSession" :true
}
}
15 changes: 15 additions & 0 deletions fixtures/sample-Intent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "GetZodiacHoroscopeIntent",
"confirmationStatus": "NONE",
"slots": {
"City": {
"name": "City",
"confirmationStatus": "NONE"
},
"ZodiacSign": {
"name": "ZodiacSign",
"value": "virgo",
"confirmationStatus": "NONE"
}
}
}
6 changes: 4 additions & 2 deletions fixtures/sample-IntentRequest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
"requestId": "amzn1.echo-api.request.6919844a-733e-4e89-893a-fdcb77e2ef0d",
"intent": {
"name": "GetZodiacHoroscopeIntent",
"confirmationStatus": "NONE",
"slots": {
"ZodiacSign": {
"name": "ZodiacSign",
"value": "virgo"
"value": "virgo",
"confirmationStatus": "NONE"
}
}
}
}
}
}
3 changes: 2 additions & 1 deletion lib/alexa_rubykit/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module AlexaRubykit
class Request
require 'json'
require 'alexa_rubykit/session'
attr_accessor :version, :type, :session, :json # global
attr_accessor :version, :type, :session, :json, :dialog_state# global
attr_accessor :request_id, :locale # on request

def initialize(json_request)
Expand All @@ -15,6 +15,7 @@ def initialize(json_request)
@version = json_request['version']
@locale = json_request['request']['locale']
@json = json_request
@dialog_state = json_request['request']['dialogState']

# TODO: We probably need better session handling.
@session = AlexaRubykit::Session.new(json_request['session'])
Expand Down
35 changes: 32 additions & 3 deletions lib/alexa_rubykit/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def add_speech(speech_text, ssml = false)
end
@speech
end

def add_audio_url(url, token='', offset=0)
@directives << {
'type' => 'AudioPlayer.Play',
Expand All @@ -39,6 +39,36 @@ def add_audio_url(url, token='', offset=0)
}
end

def add_dialog_delegate_directive(intent:)
@directives << {
'type' => 'Dialog.Delegate',
'updatedIntent' => intent
}
end

def add_dialog_elicit_slot_directive(slot_to_elicit:, intent:)
@directives << {
'type' => 'Dialog.ElicitSlot',
'slotToElicit' => slot_to_elicit,
'updatedIntent' => intent
}
end

def add_dialog_confirm_slot_directive(slot_to_confirm:, intent:)
@directives << {
'type' => 'Dialog.ConfirmSlot',
'slotToConfirm' => slot_to_confirm,
'updatedIntent' => intent
}
end

def add_dialog_confirm_intent_directive(intent:)
@directives << {
'type' => 'Dialog.ConfirmIntent',
'updatedIntent' => intent
}
end

def add_reprompt(speech_text, ssml = false)
if ssml
@reprompt = { "outputSpeech" => { :type => 'SSML', :ssml => check_ssml(speech_text) } }
Expand All @@ -48,7 +78,6 @@ def add_reprompt(speech_text, ssml = false)
@reprompt
end

#
#"type": "string",
# "title": "string",
# "subtitle": "string",
Expand Down Expand Up @@ -128,4 +157,4 @@ def check_ssml(ssml_string)
ssml_string.strip[-8..1] == "</speak>" ? ssml_string : ssml_string + "</speak>"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/alexa_rubykit/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AlexaRubykit
VERSION = '1.3.0'
VERSION = '1.3.1'
end
48 changes: 46 additions & 2 deletions spec/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
sample_json = JSON.parse(File.read('fixtures/response-sessionAtt.json')).to_json
expect(response_json).to eq(sample_json)
end

it 'should create a valid response with an audio stream directive' do
response = AlexaRubykit::Response.new
response.add_audio_url('http://test/url.mp3','token',100)
Expand All @@ -123,4 +123,48 @@
expect(response_json).to eq(sample_json)
end

end
it 'should create a valid response with a dialog delegate directive' do
response = AlexaRubykit::Response.new
intent = JSON.parse(File.read('fixtures/sample-Intent.json'))
response.add_dialog_delegate_directive(intent: intent)
response.build_response_object
response_json = response.build_response
sample_json = JSON.parse(File.read('fixtures/response-dialogDelegateDirective.json')).to_json
expect(response_json).to eq(sample_json)
end

it 'should create a valid response with a dialog confirm intent directive' do
response = AlexaRubykit::Response.new
intent = JSON.parse(File.read('fixtures/sample-Intent.json'))
response.add_dialog_confirm_intent_directive(intent: intent)
response.build_response_object
response_json = response.build_response
sample_json = JSON.parse(File.read('fixtures/response-dialogConfirmIntentDirective.json')).to_json
expect(response_json).to eq(sample_json)
end

it 'should create a valid response with a dialog confirm slot directive' do
response = AlexaRubykit::Response.new
intent = JSON.parse(File.read('fixtures/sample-Intent.json'))
response.add_dialog_confirm_slot_directive(slot_to_confirm: 'ZodiacSign', intent: intent)
response.build_response_object
response_json = response.build_response
sample_json = JSON.parse(File.read('fixtures/response-dialogConfirmSlotDirective.json')).to_json
puts ''
puts response_json
puts sample_json
expect(response_json).to eq(sample_json)
end

it 'should create a valid response with a dialog elicit slot directive' do
response = AlexaRubykit::Response.new
intent = JSON.parse(File.read('fixtures/sample-Intent.json'))
response.add_dialog_elicit_slot_directive(slot_to_elicit: 'City', intent: intent)
response.build_response_object
response_json = response.build_response
sample_json = JSON.parse(File.read('fixtures/response-dialogElicitSlotDirective.json')).to_json
expect(response_json).to eq(sample_json)
end


end