Class: BotFramework::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/bot_framework/session.rb

Overview

< Events::EventEmitter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Session

Returns a new instance of Session.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bot_framework/session.rb', line 5

def initialize(options)
  super
  @library = options[:library]
  @localizer = options[:localizer]
  @msg_sent = false
  @is_reset = false
  @last_send_time = Time.now # TODO: Move to proc?
  @batch = []
  @batch_timer = Timers::Group.new
  @batch_started = false
  @sending_batch = false
  @in_middleware = false
  @_locale = nil

  @options = options || {
    on_save: nil,
    on_send: nil,
    library: nil,
    localizer: nil,
    middleware: nil,
    dialog_id: nil,
    dialog_args: nil,
    auto_batch_delay: nil,
    dialog_error_message: nil,
    actions: nil
  }
  @auto_batch_delay = 250 unless options[:auto_batch_delay].is_a? Integer
  @timers = Timers::Group.new
end

Instance Attribute Details

#conversation_dataObject

Returns the value of attribute conversation_data.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def conversation_data
  @conversation_data
end

#dialog_stateObject

Returns the value of attribute dialog_state.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def dialog_state
  @dialog_state
end

#libraryObject

Returns the value of attribute library.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def library
  @library
end

#localizerObject

Returns the value of attribute localizer.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def localizer
  @localizer
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def message
  @message
end

#private_conversation_dataObject

Returns the value of attribute private_conversation_data.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def private_conversation_data
  @private_conversation_data
end

#session_stateObject

Returns the value of attribute session_state.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def session_state
  @session_state
end

#user_dataObject

Returns the value of attribute user_data.



3
4
5
# File 'lib/bot_framework/session.rb', line 3

def user_data
  @user_data
end

Class Method Details

.active_dialog_stack_entry(stack) ⇒ Object

Returns a active stack entry or nil



331
332
333
# File 'lib/bot_framework/session.rb', line 331

def self.active_dialog_stack_entry(stack)
  stack.last || nil
end

.findEachDialogStackEntry(stack, dialog_id) ⇒ Object

Searches a dialog stack for a specific dialog, in either a forward or reverse direction, returning its index.



323
324
325
326
327
328
# File 'lib/bot_framework/session.rb', line 323

def self.findEachDialogStackEntry(stack, dialog_id)
  stack.each_with_index do |item, index|
    return index if item[:id] = dialog_id
  end
  -1
end

.forEachDialogStackEntry(stack) ⇒ Object

Enumerates all a stacks dialog entries in either a forward or reverse direction.



318
319
320
# File 'lib/bot_framework/session.rb', line 318

def self.forEachDialogStackEntry(stack)
  stack.each { |item| yield(item) }
end

.pop_dialog_stack_entry(stack) ⇒ Object

Pop active dialog out of the stack



344
345
346
347
# File 'lib/bot_framework/session.rb', line 344

def self.pop_dialog_stack_entry(stack)
  stack.pop if stack
  Session.active_dialog_stack_entry(stack)
end

.prune_dialog_stack(_stack, _start) ⇒ Object

Deletes all dialog stack entries starting with the specified index and returns the new active dialog.



350
351
# File 'lib/bot_framework/session.rb', line 350

def self.prune_dialog_stack(_stack, _start)
end

.push_dialog_stack_entry(statck, entry) ⇒ Object

Pushes a new dialog into stack and return it as active dialog



336
337
338
339
340
341
# File 'lib/bot_framework/session.rb', line 336

def self.push_dialog_stack_entry(statck, entry)
  entry[:state] ||= {}
  statck ||= []
  stack.push(entry)
  entry
end

.validate_dialog_stack(_stack, _root) ⇒ Object

Ensures that all of the entries on a dialog stack reference valid dialogs within a library hierarchy.



354
355
# File 'lib/bot_framework/session.rb', line 354

def self.validate_dialog_stack(_stack, _root)
end

Instance Method Details

#begin_dialog(id, args = nil) ⇒ Object

Begin a new dialog



188
189
190
191
192
193
194
195
196
197
# File 'lib/bot_framework/session.rb', line 188

def begin_dialog(id, args = nil)
  logger.info "Beginning new dialog #{id}"
  id = resolve_dialog_id(id)
  dialog = find_dialog(id)
  raise "Dialog #{id} Not found" unless dialog
  push_dialog(id: id, state: {})
  start_batch
  dialog.begin(self, args)
  self
end

#cancel_dialogObject



290
# File 'lib/bot_framework/session.rb', line 290

def cancel_dialog; end

#clear_dialog_stackObject

Clears the current Dialog stack



312
313
314
315
# File 'lib/bot_framework/session.rb', line 312

def clear_dialog_stack
  @session_state[:call_stack] = []
  @dialog_data = nil
end

#dialog_stack(new_stack = nil) ⇒ Object

Gets and Sets current dialog stack



297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/bot_framework/session.rb', line 297

def dialog_stack(new_stack = nil)
  if new_stack
    # Update stack and dialog data

    stack = @session_state[:call_stack] = new_stack || []
    @dialog_data = stack.empty? nil || stack.last
  else
    # Copy over dialog data to slack
    stack = @session_state[:call_stack] || []
    stack.last.state = @dialog_data || {}
  end
  stack
end

#dispatch(session_state, message) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/bot_framework/session.rb', line 50

def dispatch(session_state, message)
  index = 0
  session = self
  now = Time.now
  middleware = @options[:middleware] || []

  _next = lambda do
    handler = middleware[index] if index < middleware.length
    if handler
      index += 1
      handler(session, _next)
    else
      @in_middleware = false
      @session_state[:last_acess] = now
      done
    end
  end
  session_state ||= { call_stack: [], last_acess: Time.now, version: 0.0 }
  # Making sure that dialog is properly initialized
  cur = cur_dialog
  self.dialog_data = cur.state if cur
  # Dispatch message
  message ||= { text: '' }
  message[:type] ||= 'message'
  # Ensure that localized prompts are loaded
  # TODO
  self
end

#end_conversation(message = nil, _args = {}) ⇒ Object

End conversation with the user



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bot_framework/session.rb', line 214

def end_conversation(message = nil, _args = {})
  if message
    # TODO: sent message
  end
  # Clear private conversation data
  @private_conversation_data = {}

  # Clear stack and save
  logger.info 'End conversation'
  ss = @session_state
  ss.call_stack = []
  send_batch
  self
end

#end_dialog(message = nil, args = {}) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/bot_framework/session.rb', line 229

def end_dialog(message = nil, args = {})
  if message
    # TODO: end_dialog_with_result
  end
  cur = cur_dialog
  if cur
    if message
      m = if (message.is_a? String) || (message.is_a? Array)
            create_message(cur_library_name, message, args)
          else
            message
          end
      @msg_sent = true
      prepare_message(m)
      @batch.push(m)
    end

    # Pop the dialog of the stack and resume the parent
    logger.info 'Session.end_dialog'
    child_id = cur.id
    cur = pop_dialog
    start_batch
    if cur
      dialog = find_dialog(cur.id)
      if dialog
        dialog.dialog_resumed(self, resumed: :completed, response: true, child_id: child_id)
      else
        # Bad dialog !! , we should never reach here
        raise "Can't resume , missing parent"
      end
    end
  end
end

#end_dialog_with_result(result) ⇒ Object

Ends current dialog and returns a value to the caller



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/bot_framework/session.rb', line 264

def end_dialog_with_result(result)
  # Validate call stack
  cur = cur_dialog
  if cur
    # Validate the result
    result ||= {}
    result[:resumed] ||= :completed
    result.child_id = cur.id
    logger.info 'Session.end_dialog_with_result'

    # Pop dialog of the stack and resume parent dialog
    cur = pop_dialog
    start_batch
    if cur
      dialog = find_dialog(cur.id)
      if dialog
        dialog.dialog_resumed(self, result)
      else
        # Bad dialog state
        raise "Can't resume , missing parent dialog"
      end
    end
  end
  self
end

#error(_error) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bot_framework/session.rb', line 79

def error(_error)
  logger.info 'Session error'
  if options[:dialog_error_message]
    end_conversation(options[:dialog_error_message])
  else
    # TODO: Add localisation
    locale = preferred_locale
    end_conversation 'Error in conversation'
  end

  # TODO: Log error
end

#gettext(message_id, options = {}) ⇒ Object

Gets and formats localized text string



110
111
112
113
# File 'lib/bot_framework/session.rb', line 110

def gettext(message_id, options = {})
  # TODO
  # stub
end

#ngettext(message_id, message_id_plural, count) ⇒ Object

Gets and formats the singular/plural form of a localized text string.



116
# File 'lib/bot_framework/session.rb', line 116

def ngettext(message_id, message_id_plural, count); end

#preferred_locale(locale = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bot_framework/session.rb', line 92

def preferred_locale(locale = nil)
  if locale
    @_locale = locale
    @user_data['BotBuilder.Data.PreferredLocale'] = locale if @user_data
    @localizer.load if @localizer # TODO
  elsif !@_locale
    if @user_data && @user_data['BotBuilder.Data.PreferredLocale']
      @_locale = @user_data['BotBuilder.Data.PreferredLocale']
    elsif @message && @message[:text_locale]
      @_locale = @message[:text_locale]
    elsif @localizer
      @_locale = @localizer[:default_locale]
    end
  end
  @_locale
end

#replace_dialog(id, args = nil) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/bot_framework/session.rb', line 199

def replace_dialog(id, args = nil)
  logger.info "Session replace dialog #{id}"
  id = resolve_dialog_id(id)
  dialog = find_dialog(id)
  raise "Dialog #{id} Not found" unless dialog

  # Update the stack and start dialog
  pop_dialog
  push_dialog(id: id, state: {})
  start_batch
  dialog.begin(self, args)
  self
end

#resetObject



292
# File 'lib/bot_framework/session.rb', line 292

def reset; end

#route_to_active_dialogObject

Dispatches handling of the current message to the active dialog stack entry.



361
# File 'lib/bot_framework/session.rb', line 361

def route_to_active_dialog; end

#saveObject

Manually save current session state



119
120
121
122
123
# File 'lib/bot_framework/session.rb', line 119

def save
  logger.info 'Session.save'
  start_batch
  self
end

#send(message, args = []) ⇒ Object



125
126
127
128
# File 'lib/bot_framework/session.rb', line 125

def send(message, args = [])
  args.unshift(@cur_library_name, message)
  send_localized(args, message)
end

#send_batchObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/bot_framework/session.rb', line 149

def send_batch
  logger.info "Sending batch with elements #{@batch.count}"
  return if sending_batch?
  # TODO: timer
  @batch_timer = nil
  batch = @batch
  @batch_started = false
  @sending_batch = true
  cur = cur_dialog
  cur.state = @dialog_data
  options[:on_save] = proc do |err|
    if err
      @sending_batch = false
      case (err.code || '')
      when 'EBADMSG'
      when 'EMSGSIZE'
        # Something went wrong , let's reset everything
        @user_data = {}
        @batch = []
        end_conversation(@options[:dialog_error_message] || 'Something went wrong and we need to startover')
      end
      yield(err) if block_given?
    else
      if batch.length
        options[:on_send].call(batch) do |err|
          @sending_batch = false
          start_batch if @batch_started # What is this :o
          yield(err) if block_given?
        end
      else
        @sending_batch = false
        start_batch if @batch_started
        yield(err) if block_given?
      end
    end
  end
end

#send_localized(_localization_namspace, message, _args = []) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/bot_framework/session.rb', line 130

def send_localized(_localization_namspace, message, _args = [])
  # TODO: Incomplete
  @msg_sent = true
  m = { text: message }
  prepare_message(m)
  @batch << m
  self
end

#send_typingObject

Sends a typing indicator to the user



140
141
142
143
144
145
146
147
# File 'lib/bot_framework/session.rb', line 140

def send_typing
  @msg_sent = true
  m = { type: 'typing' }
  m = prepare_message(m)
  @batch.push(m)
  logger.info 'Send Typing'
  send_batch
end

#to_recognize_contextObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bot_framework/session.rb', line 35

def to_recognize_context
  {
    message: message,
    user_data: user_data,
    conversation_data: conversation_data,
    private_conversation_data: private_conversation_data,
    localizer: localizer,
    dialog_stack: dialog_stack,
    preferred_locale: preferred_locale,
    get_text: get_text,
    nget_text: nget_text,
    locale: preferred_locale
  }
end