Class: Aws::Lex::Conversation

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Support::Mixins::Responses
Defined in:
lib/aws/lex/conversation.rb,
lib/aws/lex/conversation/spec.rb,
lib/aws/lex/conversation/version.rb,
lib/aws/lex/conversation/type/bot.rb,
lib/aws/lex/conversation/simulator.rb,
lib/aws/lex/conversation/type/base.rb,
lib/aws/lex/conversation/type/slot.rb,
lib/aws/lex/conversation/type/event.rb,
lib/aws/lex/conversation/type/intent.rb,
lib/aws/lex/conversation/handler/base.rb,
lib/aws/lex/conversation/handler/echo.rb,
lib/aws/lex/conversation/type/context.rb,
lib/aws/lex/conversation/type/message.rb,
lib/aws/lex/conversation/response/base.rb,
lib/aws/lex/conversation/slot/elicitor.rb,
lib/aws/lex/conversation/spec/matchers.rb,
lib/aws/lex/conversation/type/response.rb,
lib/aws/lex/conversation/response/close.rb,
lib/aws/lex/conversation/type/sentiment.rb,
lib/aws/lex/conversation/type/checkpoint.rb,
lib/aws/lex/conversation/type/input_mode.rb,
lib/aws/lex/conversation/type/slot_shape.rb,
lib/aws/lex/conversation/type/slot_value.rb,
lib/aws/lex/conversation/handler/delegate.rb,
lib/aws/lex/conversation/slot/elicitation.rb,
lib/aws/lex/conversation/type/enumeration.rb,
lib/aws/lex/conversation/response/delegate.rb,
lib/aws/lex/conversation/support/inflector.rb,
lib/aws/lex/conversation/type/time_to_live.rb,
lib/aws/lex/conversation/type/dialog_action.rb,
lib/aws/lex/conversation/type/response_card.rb,
lib/aws/lex/conversation/type/session_state.rb,
lib/aws/lex/conversation/type/transcription.rb,
lib/aws/lex/conversation/type/interpretation.rb,
lib/aws/lex/conversation/response/elicit_slot.rb,
lib/aws/lex/conversation/type/sentiment_score.rb,
lib/aws/lex/conversation/response/elicit_intent.rb,
lib/aws/lex/conversation/type/fulfillment_state.rb,
lib/aws/lex/conversation/type/intent_confidence.rb,
lib/aws/lex/conversation/type/invocation_source.rb,
lib/aws/lex/conversation/handler/slot_resolution.rb,
lib/aws/lex/conversation/response/confirm_intent.rb,
lib/aws/lex/conversation/type/confirmation_state.rb,
lib/aws/lex/conversation/type/dialog_action_type.rb,
lib/aws/lex/conversation/type/sentiment_response.rb,
lib/aws/lex/conversation/type/session_attributes.rb,
lib/aws/lex/conversation/support/mixins/responses.rb,
lib/aws/lex/conversation/type/proposed_next_state.rb,
lib/aws/lex/conversation/exception/missing_handler.rb,
lib/aws/lex/conversation/type/message/content_type.rb,
lib/aws/lex/conversation/type/response_card/button.rb,
lib/aws/lex/conversation/type/slot_elicitation_style.rb,
lib/aws/lex/conversation/support/mixins/slot_elicitation.rb,
lib/aws/lex/conversation/type/transcription/resolved_context.rb

Defined Under Namespace

Modules: Exception, Handler, Response, Slot, Spec, Support, Type Classes: Simulator

Constant Summary collapse

VERSION =
'6.4.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Mixins::Responses

#close, #confirm_intent, #delegate, #elicit_intent, #elicit_slot

Constructor Details

#initialize(opts = {}) ⇒ Conversation

Returns a new instance of Conversation.



15
16
17
18
19
# File 'lib/aws/lex/conversation.rb', line 15

def initialize(opts = {})
  self.event = opts.fetch(:event)
  self.context = opts.fetch(:context)
  self.lex = Type::Event.shrink_wrap(event)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/aws/lex/conversation.rb', line 11

def context
  @context
end

#eventObject

Returns the value of attribute event.



11
12
13
# File 'lib/aws/lex/conversation.rb', line 11

def event
  @event
end

#lexObject

Returns the value of attribute lex.



11
12
13
# File 'lib/aws/lex/conversation.rb', line 11

def lex
  @lex
end

Instance Method Details

#active_context(name:) ⇒ Object



115
116
117
# File 'lib/aws/lex/conversation.rb', line 115

def active_context(name:)
  lex.session_state.active_contexts.find { |c| c.name == name }
end

#active_context!(name:, turns: 10, seconds: 300, attributes: {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/aws/lex/conversation.rb', line 119

def active_context!(name:, turns: 10, seconds: 300, attributes: {})
  # look for an existing active context if present
  instance = active_context(name: name)

  if instance
    clear_context!(name: name)
  else
    instance = Type::Context.new
  end

  # update attributes as requested
  instance.name = name
  instance.context_attributes = attributes
  instance.time_to_live = Type::TimeToLive.new(
    turns_to_live: turns,
    time_to_live_in_seconds: seconds
  )
  lex.session_state.active_contexts << instance
  instance
end

#active_context?(name:) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/aws/lex/conversation.rb', line 111

def active_context?(name:)
  !active_context(name: name).nil?
end

#chainObject



21
22
23
# File 'lib/aws/lex/conversation.rb', line 21

def chain
  @chain ||= []
end

#checkpoint(label:) ⇒ Object



88
89
90
# File 'lib/aws/lex/conversation.rb', line 88

def checkpoint(label:)
  checkpoints.find { |v| v.label == label }
end

#checkpoint!(opts = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aws/lex/conversation.rb', line 64

def checkpoint!(opts = {})
  label = opts.fetch(:label)
  params = {
    label: label,
    dialog_action_type: opts.fetch(:dialog_action_type) { 'Delegate' },
    fulfillment_state: opts[:fulfillment_state],
    intent: opts.fetch(:intent) { lex.current_intent },
    slot_to_elicit: opts[:slot_to_elicit]
  }.compact

  if checkpoint?(label: label)
    # update the existing checkpoint
    checkpoint(label: label).assign_attributes!(params)
  else
    checkpoints.unshift(
      Type::Checkpoint.build(params)
    )
  end
end

#checkpoint?(label:) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/aws/lex/conversation.rb', line 84

def checkpoint?(label:)
  !checkpoint(label: label).nil?
end

#checkpointsObject



92
93
94
# File 'lib/aws/lex/conversation.rb', line 92

def checkpoints
  lex.session_state.session_attributes.checkpoints
end

#clear_all_contexts!Object



144
145
146
# File 'lib/aws/lex/conversation.rb', line 144

def clear_all_contexts!
  lex.session_state.active_contexts = []
end

#clear_context!(name:) ⇒ Object



140
141
142
# File 'lib/aws/lex/conversation.rb', line 140

def clear_context!(name:)
  lex.session_state.active_contexts.delete_if { |c| c.name == name }
end

#handlersObject



40
41
42
# File 'lib/aws/lex/conversation.rb', line 40

def handlers
  chain.map(&:class)
end

#handlers=(list) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aws/lex/conversation.rb', line 25

def handlers=(list)
  last = nil

  reversed = list.reverse.map do |element|
    handler = element.fetch(:handler).new(
      options: element.fetch(:options) { {} },
      successor: last
    )
    last = handler
    handler
  end

  @chain = reversed.reverse
end

#intent_confidenceObject



48
49
50
# File 'lib/aws/lex/conversation.rb', line 48

def intent_confidence
  @intent_confidence ||= Type::IntentConfidence.new(event: lex)
end

#intent_nameObject



52
53
54
# File 'lib/aws/lex/conversation.rb', line 52

def intent_name
  lex.current_intent.name
end

#proposed_next_state?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/aws/lex/conversation.rb', line 148

def proposed_next_state?
  !proposed_next_state.nil?
end

#respondObject



44
45
46
# File 'lib/aws/lex/conversation.rb', line 44

def respond
  chain.first.handle(self)
end

#restore_from!(checkpoint) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/aws/lex/conversation.rb', line 96

def restore_from!(checkpoint)
  # we're done with the stored checkpoint once it's been restored
  checkpoints.delete_if { |c| c.label == checkpoint.label }
  # remove any memoized intent data
  lex.current_intent = nil
  # replace the intent with data from the checkpoint
  lex.session_state.intent = checkpoint.intent
  dialog_action = Type::DialogAction.new(
    type: checkpoint.dialog_action_type,
    slot_to_elicit: checkpoint.slot_to_elicit
  )
  lex.session_state.dialog_action = dialog_action
  self
end

#sessionObject



60
61
62
# File 'lib/aws/lex/conversation.rb', line 60

def session
  lex.session_state.session_attributes
end

#simulate!Object



16
17
18
# File 'lib/aws/lex/conversation/spec.rb', line 16

def simulate!
  @simulate ||= Simulator.new(lex: lex)
end

#slotsObject



56
57
58
# File 'lib/aws/lex/conversation.rb', line 56

def slots
  lex.current_intent.slots
end

#stashObject



152
153
154
# File 'lib/aws/lex/conversation.rb', line 152

def stash
  @stash ||= {}
end