class BotPlatform::Dialogs::WaterfallDialog

Constants

LOGLEVELS

Attributes

steps[R]

Public Class Methods

new(dialog_id, actions) click to toggle source
Calls superclass method BotPlatform::Dialogs::Dialog::new
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 14
def initialize(dialog_id, actions)
  super(dialog_id)
  @steps = actions.nil? ? [] : actions
  @logger = Logger.new(STDOUT)
  
  level ||= LOGLEVELS.index ENV.fetch("BOT_LOG_LEVEL","WARN")
  level ||= Logger::WARN
  @logger.level = level
end

Public Instance Methods

add_step(step) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 28
def add_step(step)
  @steps.push step
  return self
end
continue(dc) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 47
def continue(dc)
  @logger.debug "continue dc:#{dc.inspect}"
  assert_dialog_context_is_valid dc

  if dc.turn_context.activity.type != Activity::TYPES[:message]
    return DialogResult.new :complete
  end

  return resume(dc, DialogReason::CONTINUE_CALLED, dc.turn_context.activity.text)
end
on_step(step_ctx) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 89
def on_step(step_ctx)
  step_name = waterfall_step_name(step_ctx.index)
  instance_id = step_ctx.active_dialog.state[:instance_id]
  return @steps[step_ctx.index][:method].call step_ctx
end
resume(dc, reason, result) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 58
def resume(dc, reason, result)
  assert_dialog_context_is_valid dc

  state = dc.active_dialog.state

  index = state[:step_index]
  run_step(dc, index+1, reason, result)
end
run_step(dc, index, reason, result) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 95
def run_step(dc, index, reason, result)
  assert_dialog_context_is_valid dc

  if index < @steps.count
    state = dc.active_dialog.state
    state[:step_index] = index

    options = state[:options]
    values = state[:values]
    step_context = WaterfallStepContext.new self, dc, options, values, index, reason, result

    return on_step(step_context)
  end

  return dc.end_dialog(result)
end
start(dc, options=nil) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 33
def start(dc, options=nil)
  assert_dialog_context_is_valid dc

  state = dc.active_dialog.state
  instance_id = SecureRandom.uuid

  state[:options] = options
  state[:values] = {}
  state[:instace_id] = instance_id

  # first step
  run_step(dc, 0, DialogReason::BEGIN_CALLED, nil)
end
stop(turn_ctx, instance, reason) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 67
def stop(turn_ctx, instance, reason)
  assert_turn_context_is_valid turn_ctx
  assert_dialog_instance_is_valid instance
  assert_dialog_reason_is_valid reason

  if reason == DialogReason::CANCEL_CALLED
    state = instance.state.dup
    index = state[:step_index]
    step_name = waterfall_step_name(index)
    instance_id = state[:instance_id]

    logger.debug {dialog_id:@id, step_name:step_name, instance_id:instance_id}.to_json
  elsif reason == DialogReason::END_CALLED
    state = instance.state.dup
    index = state[:step_index]
    instance_id = state[:instance_id]

    logger.debug {dialog_id:@id, instance_id:instance_id}.to_json
  end

end
version() click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 24
def version
  return "#{@id}:#{@steps.count}"
end
waterfall_step_name(index) click to toggle source
# File lib/bot_platform/dialogs/waterfall_dialog.rb, line 112
def waterfall_step_name(index)
  step_name = @steps[index][:name]

  if step_name.nil? || step_name.empty? || step_name.include?("<")
    step_name = "Step#{index+1}of#{@steps.count}"
  end

  step_name
end