Class: BotFramework::Session
- Inherits:
-
Object
- Object
- BotFramework::Session
- Defined in:
- lib/bot_framework/session.rb
Overview
< Events::EventEmitter
Instance Attribute Summary collapse
-
#conversation_data ⇒ Object
Returns the value of attribute conversation_data.
-
#dialog_state ⇒ Object
Returns the value of attribute dialog_state.
-
#library ⇒ Object
Returns the value of attribute library.
-
#localizer ⇒ Object
Returns the value of attribute localizer.
-
#message ⇒ Object
Returns the value of attribute message.
-
#private_conversation_data ⇒ Object
Returns the value of attribute private_conversation_data.
-
#session_state ⇒ Object
Returns the value of attribute session_state.
-
#user_data ⇒ Object
Returns the value of attribute user_data.
Class Method Summary collapse
-
.active_dialog_stack_entry(stack) ⇒ Object
Returns a active stack entry or nil.
-
.findEachDialogStackEntry(stack, dialog_id) ⇒ Object
Searches a dialog stack for a specific dialog, in either a forward or reverse direction, returning its index.
-
.forEachDialogStackEntry(stack) ⇒ Object
Enumerates all a stacks dialog entries in either a forward or reverse direction.
-
.pop_dialog_stack_entry(stack) ⇒ Object
Pop active dialog out of the stack.
-
.prune_dialog_stack(_stack, _start) ⇒ Object
Deletes all dialog stack entries starting with the specified index and returns the new active dialog.
-
.push_dialog_stack_entry(statck, entry) ⇒ Object
Pushes a new dialog into stack and return it as active dialog.
-
.validate_dialog_stack(_stack, _root) ⇒ Object
Ensures that all of the entries on a dialog stack reference valid dialogs within a library hierarchy.
Instance Method Summary collapse
-
#begin_dialog(id, args = nil) ⇒ Object
Begin a new dialog.
- #cancel_dialog ⇒ Object
-
#clear_dialog_stack ⇒ Object
Clears the current Dialog stack.
-
#dialog_stack(new_stack = nil) ⇒ Object
Gets and Sets current dialog stack.
- #dispatch(session_state, message) ⇒ Object
-
#end_conversation(message = nil, _args = {}) ⇒ Object
End conversation with the user.
- #end_dialog(message = nil, args = {}) ⇒ Object
-
#end_dialog_with_result(result) ⇒ Object
Ends current dialog and returns a value to the caller.
- #error(_error) ⇒ Object
-
#gettext(message_id, options = {}) ⇒ Object
Gets and formats localized text string.
-
#initialize(options) ⇒ Session
constructor
A new instance of Session.
-
#ngettext(message_id, message_id_plural, count) ⇒ Object
Gets and formats the singular/plural form of a localized text string.
- #preferred_locale(locale = nil) ⇒ Object
- #replace_dialog(id, args = nil) ⇒ Object
- #reset ⇒ Object
-
#route_to_active_dialog ⇒ Object
Dispatches handling of the current message to the active dialog stack entry.
-
#save ⇒ Object
Manually save current session state.
- #send(message, args = []) ⇒ Object
- #send_batch ⇒ Object
- #send_localized(_localization_namspace, message, _args = []) ⇒ Object
-
#send_typing ⇒ Object
Sends a typing indicator to the user.
- #to_recognize_context ⇒ Object
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() super @library = [:library] @localizer = [: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 = || { 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 [:auto_batch_delay].is_a? Integer @timers = Timers::Group.new end |
Instance Attribute Details
#conversation_data ⇒ Object
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_state ⇒ Object
Returns the value of attribute dialog_state.
3 4 5 |
# File 'lib/bot_framework/session.rb', line 3 def dialog_state @dialog_state end |
#library ⇒ Object
Returns the value of attribute library.
3 4 5 |
# File 'lib/bot_framework/session.rb', line 3 def library @library end |
#localizer ⇒ Object
Returns the value of attribute localizer.
3 4 5 |
# File 'lib/bot_framework/session.rb', line 3 def localizer @localizer end |
#message ⇒ Object
Returns the value of attribute message.
3 4 5 |
# File 'lib/bot_framework/session.rb', line 3 def @message end |
#private_conversation_data ⇒ Object
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_state ⇒ Object
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_data ⇒ Object
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_dialog ⇒ Object
290 |
# File 'lib/bot_framework/session.rb', line 290 def cancel_dialog; end |
#clear_dialog_stack ⇒ Object
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, ) 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 ||= { text: '' } [: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( = nil, _args = {}) if # 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( = nil, args = {}) if # TODO: end_dialog_with_result end cur = cur_dialog if cur if m = if (.is_a? String) || (.is_a? Array) (cur_library_name, , args) else end @msg_sent = true (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 [:dialog_error_message] end_conversation([: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(, = {}) # 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(, , 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 |
#reset ⇒ Object
292 |
# File 'lib/bot_framework/session.rb', line 292 def reset; end |
#route_to_active_dialog ⇒ Object
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 |
#save ⇒ Object
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(, args = []) args.unshift(@cur_library_name, ) send_localized(args, ) end |
#send_batch ⇒ Object
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 [: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 [: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, , _args = []) # TODO: Incomplete @msg_sent = true m = { text: } (m) @batch << m self end |
#send_typing ⇒ Object
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 = (m) @batch.push(m) logger.info 'Send Typing' send_batch end |
#to_recognize_context ⇒ Object
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: , 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 |