Class: BotPlatform::Dialogs::WaterfallDialog

Inherits:
Dialog
  • Object
show all
Includes:
Asserts
Defined in:
lib/bot_platform/dialogs/waterfall_dialog.rb

Constant Summary collapse

LOGLEVELS =
%w[DEBUG INFO WARN ERROR FATAL UNKNOWN].freeze

Constants inherited from Dialog

Dialog::END_OF_TURN

Instance Attribute Summary collapse

Attributes inherited from Dialog

#id

Instance Method Summary collapse

Methods included from Asserts

#assert_activity_is_not_null, #assert_activity_list_is_not_null, #assert_activity_type_is_not_null, #assert_context_is_not_null, #assert_conversation_reference_is_not_null, #assert_dialog_context_is_valid, #assert_dialog_id_is_valid, #assert_dialog_is_uniq, #assert_dialog_is_valid, #assert_dialog_set_is_valid, #assert_dialog_state_is_valid, #assert_is_not_empty, #assert_middleware_is_not_null, #assert_middleware_list_is_not_null, #assert_prompt_options_is_valid, #assert_turn_context_is_valid, #assert_waterfall_step_context_is_valid

Methods inherited from Dialog

#reprompt

Constructor Details

#initialize(dialog_id, actions) ⇒ WaterfallDialog

Returns a new instance of WaterfallDialog.



14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



12
13
14
# File 'lib/bot_platform/dialogs/waterfall_dialog.rb', line 12

def steps
  @steps
end

Instance Method Details

#add_step(step) ⇒ Object



28
29
30
31
# File 'lib/bot_platform/dialogs/waterfall_dialog.rb', line 28

def add_step(step)
  @steps.push step
  return self
end

#continue(dc) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



89
90
91
92
93
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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

#versionObject



24
25
26
# File 'lib/bot_platform/dialogs/waterfall_dialog.rb', line 24

def version
  return "#{@id}:#{@steps.count}"
end

#waterfall_step_name(index) ⇒ Object



112
113
114
115
116
117
118
119
120
# 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