Skip to content

Instantly share code, notes, and snippets.

@bleonard
Created December 24, 2016 16:59
Show Gist options
  • Save bleonard/a59176706a06115b67d6a84e51397e9c to your computer and use it in GitHub Desktop.
Save bleonard/a59176706a06115b67d6a84e51397e9c to your computer and use it in GitHub Desktop.
class SkillsController < ::ActionController::Base
def root
input = AlexaRubykit.build_request(params)
output = AlexaRubykit::Response.new
session_end = false # probably going to keep going
message = "There was an error." # unknown thing happened
session = Session.find_or_initialize_by(session_id: input.session.session_id)
case input.type
when "LAUNCH_REQUEST"
# user talked to our skill but did not say something matching intent
message = "Hi. How can we help?"
when "INTENT_REQUEST"
case session.state
when "selecting_category"
category = select_category(slot_params) # uses generic
if category
session.category = category
message = "What date and time?"
session.state = "deciding_time"
else
message = "Sorry, missed that. Try cleaning or handyman."
end
when "deciding_time"
schedule = select_schedule(slot_params) # uses date/time
if schedule
session.schedule = schedule
message = "Tell us more about it"
session.state = "adding_details"
else
message = "Try things like Friday at noon."
end
when "adding_details" # etc
when "confirming"
if did_confirm?(slot_params) # uses yes
# do it!
message = "Your task has been booked"
session.state = "completed"
elsif did_exit?(slot_parms) # uses no
session.state = "canceled"
session_end = true
else
message = "Ready to confirm? Say yes or no"
end
when "completed" # etc
end
when "SESSION_ENDED_REQUEST"
# it's over
message = nil
session_end = true
end
session.save!
output.add_speech(message) unless message.blank?
render json: output.build_response(session_end)
end
private
def slot_params
# returns all the intent slots
# e.g. {"generic" => "what they said", "schedule_date" => "2016-12-05"}
return @slot_params if @slot_params
@slot_params = {}
return @slot_params unless input.type == "INTENT_REQUEST"
input.slots.each do |name, slot|
key = name.underscore # category_noun, etc
value = slot['value']
@slot_params[key] = value
end
@slot_params
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment