Class: Kogno::Context
- Inherits:
-
Object
- Object
- Kogno::Context
- Defined in:
- lib/core/lib/context.rb
Constant Summary collapse
- @@callbacks =
class callbacks: after_initialize,before_blocks, before_exit, after_blocks
{}
- @@expiration_time =
{}
Class Method Summary collapse
- .after_blocks(method) ⇒ Object
- .after_initialize(method) ⇒ Object
- .base_template(action_group, action, params, returnable, instance) ⇒ Object
- .before_blocks(method) ⇒ Object
- .before_exit(method) ⇒ Object
- .class_callbacks ⇒ Object
- .expiration_in(expiration_time) ⇒ Object
- .get(value) ⇒ Object
- .get_from_payload(postback_payload) ⇒ Object
- .get_from_typed_postback(msg, user) ⇒ Object
- .router(route, type = :default) ⇒ Object
- .run_sequence(action) ⇒ Object
- .set_class_callback(action, method) ⇒ Object
Instance Method Summary collapse
- #after(section, &block) ⇒ Object
- #after_all(&block) ⇒ Object
- #answer(route, &block) ⇒ Object
- #any_attachment(&block) ⇒ Object
- #any_intent(&block) ⇒ Object
- #any_number(&block) ⇒ Object
- #any_postback(&block) ⇒ Object
- #any_text(&block) ⇒ Object
- #anything(&block) ⇒ Object
- #ask(answer_route = nil, params = {}, &block) ⇒ Object
- #before(section, &block) ⇒ Object
- #before_anything(&block) ⇒ Object
-
#before_delegate(&block) ⇒ Object
CallBacks shortcuts.
-
#blocks ⇒ Object
Methods to overwrite.
- #call_after(section = :blocks, values = nil) ⇒ Object
- #call_any_attachment ⇒ Object
- #call_any_intent(block_params) ⇒ Object
- #call_any_number ⇒ Object
- #call_any_postback(block_params) ⇒ Object
- #call_any_text ⇒ Object
- #call_anything ⇒ Object
- #call_before(section = :blocks, values = nil) ⇒ Object
-
#call_block(action, params = nil, input = :empty) ⇒ Object
CALLS.
- #call_callback(callback_name) ⇒ Object
- #call_class_callback(callback_name) ⇒ Object
- #call_commands ⇒ Object
- #call_deep_link ⇒ Object
- #call_everything_else ⇒ Object
- #call_expiration_time ⇒ Object
- #call_intents ⇒ Object
-
#call_keywords ⇒ Object
def call_stickers block_response = false unless @message.stickers.empty? call_before(:stickers) return true if halted? @message.stickers.each do |sticker| block_response = call_block(:stickers, nil, sticker) end block_response = call_block(:stickers, nil, “*”) unless block_response call_after(:stickers) return block_response else return false end end.
- #call_location ⇒ Object
- #call_membership_drop ⇒ Object
- #call_membership_new ⇒ Object
- #call_nlp_entity ⇒ Object
- #call_postbacks(triggers = true) ⇒ Object
- #call_recurring_notifications ⇒ Object
- #call_regular_expressions ⇒ Object
- #call_typed_postbacks ⇒ Object
- #callback(callback_name, &block) ⇒ Object
- #change_locale(locale) ⇒ Object
- #change_to(route, params = {}) ⇒ Object
- #change_to_saved_context ⇒ Object
-
#command(input, &block) ⇒ Object
Telegram only.
- #continue ⇒ Object
- #continue? ⇒ Boolean
- #current_context_route ⇒ Object
- #current_sub_context ⇒ Object
- #datetime(&block) ⇒ Object
- #datetime_range(&block) ⇒ Object
- #debugger ⇒ Object
- #deep ⇒ Object
- #deep_link(&block) ⇒ Object
- #delegate_to(route, args = {}) ⇒ Object
- #destroy_nlp_reference ⇒ Object
- #duration(&block) ⇒ Object
- #entity(input, &block) ⇒ Object
- #everything_else(&block) ⇒ Object
- #exit ⇒ Object
- #exit_answer ⇒ Object
- #exit_context ⇒ Object
- #expression(input, &block) ⇒ Object
- #get_default_context_for_command(command) ⇒ Object
- #get_sequences ⇒ Object
- #halt(silent = false) ⇒ Object
- #halted? ⇒ Boolean
- #handle_message_from_memory(platform = nil) ⇒ Object
- #in_route? ⇒ Boolean
-
#initialize(user, msg, notification, temp_context = nil, notification_group = nil, chat = nil) ⇒ Context
constructor
A new instance of Context.
- #inline_query(&block) ⇒ Object
- #intent(input, &block) ⇒ Object
- #keep ⇒ Object
- #keyword(input, &block) ⇒ Object
- #location(&block) ⇒ Object
- #membership(type, &block) ⇒ Object
- #memorize_message ⇒ Object
- #name ⇒ Object
- #nlp_entities_present?(entities) ⇒ Boolean
- #nlp_entity(input, &block) ⇒ Object
- #nlp_location(&block) ⇒ Object
- #on_delegate(&block) ⇒ Object
- #on_enter(&block) ⇒ Object
- #on_exit(&block) ⇒ Object
- #params ⇒ Object
- #parent_context_route(context_path = nil) ⇒ Object
- #past(time_elapsed, &block) ⇒ Object
- #postback(input, &block) ⇒ Object
- #recurring_notification(type, &block) ⇒ Object
- #regular_expression(input, &block) ⇒ Object
- #remember_message ⇒ Object
-
#root ⇒ Object
def html_template(route,params,instance) self.class.html_template(action_group, action, params, false, self) end.
- #root_context_route(context_path = nil) ⇒ Object
- #run(args = {run_blocks_method: true, ignore_everything_else: false}) ⇒ Object
- #run_class_callbacks_only ⇒ Object
- #run_for_chat_activity_only(args = {run_blocks_method: true, ignore_everything_else: false}) ⇒ Object
- #run_for_recurring_notification_only ⇒ Object
- #run_for_text_only(args = {run_blocks_method: true, ignore_everything_else: false}) ⇒ Object
- #save_current_context ⇒ Object
- #sequence(stage_name, &block) ⇒ Object
- #sequence_name ⇒ Object
-
#sequences ⇒ Object
Sequences.
- #set_block(action, input, impact, block) ⇒ Object
-
#set_nlp_reference(**values) ⇒ Object
This is used to send context references to Wit wit.ai/docs/http/20170307#context_link.
- #start_sequence(stage_route, context_name = nil) ⇒ Object
- #sticker(input, &block) ⇒ Object
- #stop_sequence(stage_route) ⇒ Object
-
#sub_context(route, &block) ⇒ Object
end.
- #sub_context?(sub_context) ⇒ Boolean
- #template(action_group, action, params = {}) ⇒ Object
- #translate_route(route) ⇒ Object
- #type ⇒ Object
- #valid_context_route? ⇒ Boolean
Constructor Details
#initialize(user, msg, notification, temp_context = nil, notification_group = nil, chat = nil) ⇒ Context
Returns a new instance of Context.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/core/lib/context.rb', line 6 def initialize(user,msg,notification,temp_context=nil,notification_group=nil,chat=nil) @user = user @chat = chat || @user @message = msg @reply = notification @reply_group = notification_group # return true if call_expiration_time() @sub_contexts = [] @sub_context_route = "" # @sub_contexts = {} @sub_contexts_tree = [] @blocks = { deep_link: {}, anything: {}, before: {}, intents: {}, commands: {}, expressions: {}, postbacks: {}, stickers: {}, attachments: {}, keywords: {}, regular_expressions: {}, any_number: {}, any_text: {}, location: {}, nlp_entities: {}, nlp_datetime_range: {}, nlp_location: {}, nlp_search_query: {}, nlp_datetime: {}, nlp_duration: {}, any_postback: {}, any_intent: {}, everything_else: {}, after: {}, membership_new: {}, membership_drop: {}, recurring_notification: {} } @sequences={} @current_sequence_stage = nil @impact = :self @deep_blocks = Marshal.load(Marshal.dump(@blocks)) @callbacks = { before_delegate:{}, on_delegate:{}, on_exit:{}, on_enter:{} } @halt = false @continue = false # self.class.after_initialize() @reply.set_context(self) @reply_group.set_context(self) unless @reply_group.nil? @context = self @current = temp_context.nil? ? @user.context : temp_context # @current = Kogno::Application.config.routes.default if @current.nil? || @current.empty? @memorized_message = nil @current_action = { action: nil, value: nil } @called_block = { action: nil, value: nil, params: nil, context: nil, deep: false } call_class_callback(:after_initialize) end |
Class Method Details
.after_blocks(method) ⇒ Object
112 113 114 |
# File 'lib/core/lib/context.rb', line 112 def self.after_blocks(method) self.set_class_callback(:after_blocks, method) end |
.after_initialize(method) ⇒ Object
100 101 102 |
# File 'lib/core/lib/context.rb', line 100 def self.after_initialize(method) self.set_class_callback(:after_initialize, method) end |
.base_template(action_group, action, params, returnable, instance) ⇒ Object
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 |
# File 'lib/core/lib/context.rb', line 1363 def self.base_template(action_group,action, params,returnable, instance) template = $context_blocks[action_group.to_sym][action.to_sym] rescue nil log_string = " Rendering template: #{File.join("bot","templates",action_group.to_s,"#{action.to_s}.erb")}" log_string = "#{log_string} with params #{params.to_h}" unless params.to_h.empty? logger.write log_string, :pink unless template.nil? if !returnable template.render(instance, params) else template end else logger.write " ERROR: Template #{action_group}/#{action}.erb not found.", :red end end |
.before_blocks(method) ⇒ Object
104 105 106 |
# File 'lib/core/lib/context.rb', line 104 def self.before_blocks(method) self.set_class_callback(:before_blocks, method) end |
.before_exit(method) ⇒ Object
108 109 110 |
# File 'lib/core/lib/context.rb', line 108 def self.before_exit(method) self.set_class_callback(:before_exit, method) end |
.class_callbacks ⇒ Object
884 885 886 |
# File 'lib/core/lib/context.rb', line 884 def self.class_callbacks @@callbacks end |
.expiration_in(expiration_time) ⇒ Object
96 97 98 |
# File 'lib/core/lib/context.rb', line 96 def self.expiration_in(expiration_time) @@expiration_time[self] = expiration_time end |
.get(value) ⇒ Object
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 |
# File 'lib/core/lib/context.rb', line 1194 def self.get(value) if !value.nil? && value.class == String $contexts.each do |context| if value.start_with?("#{context}_") return( { context: context, param: value.sub("#{context}_","") } ) end end end return nil end |
.get_from_payload(postback_payload) ⇒ Object
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 |
# File 'lib/core/lib/context.rb', line 1181 def self.get_from_payload(postback_payload) if !postback_payload.nil? && postback_payload.class == String routes = postback_payload.split(Regexp.union(["/","-",'__'])) if routes.count > 1 routes.pop return routes.join(".") end end return nil end |
.get_from_typed_postback(msg, user) ⇒ Object
651 652 653 654 655 656 |
# File 'lib/core/lib/context.rb', line 651 def self.get_from_typed_postback(msg,user) typed_postbacks = user.vars[:typed_postbacks] keyword = msg.text.to_payload postback = (typed_postbacks[keyword] rescue nil) return(self.get_from_payload(postback)) end |
.router(route, type = :default) ⇒ Object
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 |
# File 'lib/core/lib/context.rb', line 1152 def self.router(route,type=:default) type = :default if type.to_sym == :message context_class = nil sub_context_route = nil if route.to_s.empty? context_class = "#{Kogno::Application.routes.to_h[type.to_s]}_context".classify else route_array = route.to_s.split(Regexp.union(["/","."])) context_name = route_array[0] sub_context_route = route_array[1..].join(".") context_class = "#{context_name}_context".classify end valid_class = (eval(context_class) rescue nil) if valid_class.nil? valid = false else valid = true context_class = valid_class end return ( { class: context_class, sub_context_route: sub_context_route, valid: valid } ) end |
.run_sequence(action) ⇒ Object
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 |
# File 'lib/core/lib/context.rb', line 1459 def self.run_sequence(action) logger.write "Running delayed action ##{action.id}|#{action.route} for user ##{action.user.psid}" action.user.get_session_vars if action.user.platform == "messenger" notification = Kogno::Messenger::Notification.new(action.user) elsif action.user.platform == "telegram" notification = Kogno::Telegram::Notification.new(action.user) else notification = Kogno::Notification.new(action.user) end context = self.router(action.route)[:class].new(action.user,{},notification) context.sequences() user = action.user # context.get_sequences[action.route].sort{|x,y| y<=>x}.each do |past,block| if !context.get_sequences[action.route].nil? context.get_sequences[action.route].sort.each do |past,block| if past > action.last_executed execution_time = action.last_hit_at+past if Time.now.utc > execution_time block.call notification.send user.log_response(notification) if Kogno::Application.config.store_log_in_database action.last_executed = past action.save else action.execution_time = execution_time action.save logger.write "It's not time yet. Execution time at #{execution_time} it will be executed at #{execution_time-Time.now.utc} seconds.", :red return false end end end logger.write "End of cycle, there is no more in this sequence so this user will be exited from the stage:#{action.route}" context.stop_sequence return false else logger.write "Not action found for #{action.route}" context.stop_sequence return false end end |
.set_class_callback(action, method) ⇒ Object
116 117 118 119 120 |
# File 'lib/core/lib/context.rb', line 116 def self.set_class_callback(action,method) @@callbacks[self] = {} if @@callbacks[self].nil? @@callbacks[self][action] = [] if @@callbacks[self][action].nil? @@callbacks[self][action].push(method) end |
Instance Method Details
#after(section, &block) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/core/lib/context.rb', line 157 def after(section, &block) set_block(:after, section, @impact, block) @impact = :self end |
#after_all(&block) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/core/lib/context.rb', line 173 def after_all(&block) after :blocks do block.call end @impact = :self end |
#answer(route, &block) ⇒ Object
417 418 419 420 421 |
# File 'lib/core/lib/context.rb', line 417 def answer(route, &block) sub_context route do |params| block.call params end end |
#any_attachment(&block) ⇒ Object
243 244 245 246 |
# File 'lib/core/lib/context.rb', line 243 def (&block) set_block(:attachments, nil, @impact, block) @impact = :self end |
#any_intent(&block) ⇒ Object
323 324 325 326 |
# File 'lib/core/lib/context.rb', line 323 def any_intent(&block) set_block(:any_intent, nil, @impact, block) @impact = :self end |
#any_number(&block) ⇒ Object
308 309 310 311 |
# File 'lib/core/lib/context.rb', line 308 def any_number(&block) set_block(:any_number, nil, @impact, block) @impact = :self end |
#any_postback(&block) ⇒ Object
318 319 320 321 |
# File 'lib/core/lib/context.rb', line 318 def any_postback(&block) set_block(:any_postback, nil, @impact, block) @impact = :self end |
#any_text(&block) ⇒ Object
313 314 315 316 |
# File 'lib/core/lib/context.rb', line 313 def any_text(&block) set_block(:any_text, nil, @impact, block) @impact = :self end |
#anything(&block) ⇒ Object
303 304 305 306 |
# File 'lib/core/lib/context.rb', line 303 def anything(&block) set_block(:anything, nil, @impact, block) @impact = :self end |
#ask(answer_route = nil, params = {}, &block) ⇒ Object
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 |
# File 'lib/core/lib/context.rb', line 1264 def ask(answer_route=nil, params={}, &block) unless answer_route.nil? route = (self.parent_context_route != "" and answer_route[0] != "/") ? "../#{answer_route}" : answer_route change_to(route, params) end unless block.nil? if answer_route.nil? callback :on_enter do block.call end else block.call end end end |
#before(section, &block) ⇒ Object
150 151 152 153 154 155 |
# File 'lib/core/lib/context.rb', line 150 def before(section, &block) set_block(:before, section, @impact, block) @impact = :self end |
#before_anything(&block) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/core/lib/context.rb', line 164 def before_anything(&block) before :blocks do block.call end @impact = :self end |
#before_delegate(&block) ⇒ Object
CallBacks shortcuts
339 340 341 342 343 |
# File 'lib/core/lib/context.rb', line 339 def before_delegate(&block) callback :before_delegate do block.call end end |
#blocks ⇒ Object
Methods to overwrite
1355 1356 1357 |
# File 'lib/core/lib/context.rb', line 1355 def blocks #overwrite this with your routes end |
#call_after(section = :blocks, values = nil) ⇒ Object
528 529 530 531 |
# File 'lib/core/lib/context.rb', line 528 def call_after(section=:blocks, values=nil) return true if halted? call_block(:after, values, section) end |
#call_any_attachment ⇒ Object
827 828 829 830 831 832 833 |
# File 'lib/core/lib/context.rb', line 827 def unless @message..nil? return call_block(:attachments, @message.) else return false end end |
#call_any_intent(block_params) ⇒ Object
843 844 845 846 847 848 849 |
# File 'lib/core/lib/context.rb', line 843 def call_any_intent(block_params) unless @message.nlp.intent.nil? return call_block(:any_intent, block_params) else return false end end |
#call_any_number ⇒ Object
809 810 811 812 813 814 815 816 817 |
# File 'lib/core/lib/context.rb', line 809 def call_any_number if @message.numbers_in_text.count > 0 block_response = call_block(:any_number, @message.numbers_in_text) return block_response else return false end end |
#call_any_postback(block_params) ⇒ Object
835 836 837 838 839 840 841 |
# File 'lib/core/lib/context.rb', line 835 def call_any_postback(block_params) unless @message.payload.nil? return call_block(:any_postback, block_params) else return false end end |
#call_any_text ⇒ Object
819 820 821 822 823 824 825 |
# File 'lib/core/lib/context.rb', line 819 def call_any_text unless @message.text.empty? return call_block(:any_text, @message.text) else return false end end |
#call_anything ⇒ Object
573 574 575 |
# File 'lib/core/lib/context.rb', line 573 def call_anything call_block(:anything) end |
#call_before(section = :blocks, values = nil) ⇒ Object
523 524 525 526 |
# File 'lib/core/lib/context.rb', line 523 def call_before(section=:blocks, values=nil) return true if halted? call_block(:before, values, section) end |
#call_block(action, params = nil, input = :empty) ⇒ Object
CALLS
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/core/lib/context.rb', line 457 def call_block(action, params=nil, input=:empty) @current_action = { name: action, value: input == :empty ? nil : input } if input.nil? return false elsif input == :empty block = @blocks[action][current_sub_context()] rescue nil else block = @blocks[action][current_sub_context()][input] rescue nil end if block.class == Proc logger_call(action, input, params, true) if params.class == Kogno::BlockParams block.call params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7] else block.call params end unless continue? @called_block = { name: action, value: input == :empty ? nil : input, params: params, context: @current, deep: false } return true else logger_call(action, input, [], false) return false end else if input == :empty deep_action_block = @deep_blocks[action] rescue nil else deep_action_block = @deep_blocks[action][input] rescue nil end if deep_action_block.class == Proc logger_call(action, input, params, true, true) if params.class == Kogno::BlockParams deep_action_block.call params[0], params[1], params[3], params[4], params[5], params[6], params[7] else deep_action_block.call params end unless continue? @called_block = { name: action, value: input == :empty ? nil : input, params: params, context: @current, deep: true } return true else logger_call(action, input, [], false) return false end else logger_call(action, input, [], false) return false end end end |
#call_callback(callback_name) ⇒ Object
856 857 858 859 860 861 862 863 864 865 866 |
# File 'lib/core/lib/context.rb', line 856 def call_callback(callback_name) callback_block = (@callbacks[callback_name.to_sym][current_sub_context()] rescue nil) unless callback_block.nil? logger_call("callback", callback_name, [], true) callback_block.call return true else logger_call("callback", callback_name, [], false) return false end end |
#call_class_callback(callback_name) ⇒ Object
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 |
# File 'lib/core/lib/context.rb', line 868 def call_class_callback(callback_name) conversation_methods = (@@callbacks[Conversation][callback_name].uniq rescue []) class_methods = (@@callbacks[self.class][callback_name] rescue []) methods = [] methods += conversation_methods unless conversation_methods.nil? methods += class_methods unless class_methods.nil? if methods.length > 0 methods.each do |method| if !method.nil? and !halted? logger.write "\t#{method}() method found", :red self.send(method) end end end end |
#call_commands ⇒ Object
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
# File 'lib/core/lib/context.rb', line 608 def call_commands return false unless @message.platform == :telegram command = @message.command unless command.nil? @message.nlp.process_phrase(@message.command_text) block_response = call_block(:commands, Kogno::BlockParams.new([@message.command_text, @message.nlp.entities, @message.nlp.traits]), command) if block_response return block_response else default_context = get_default_context_for_command(command) unless self.name == default_context.to_s logger_call(:commands, command, [], true) if self.delegate_to(default_context) halt return true else return false end else false end end else return false end end |
#call_deep_link ⇒ Object
548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/core/lib/context.rb', line 548 def call_deep_link if !@message.referral(:ref).nil? return true if halted? param = @message.deep_link_param || @message.referral(:ref) call_block(:deep_link, param) elsif !@message.deep_link_data.nil? param = @message.deep_link_param || @message.deep_link_data call_block(:deep_link, param) else return false end end |
#call_everything_else ⇒ Object
851 852 853 854 |
# File 'lib/core/lib/context.rb', line 851 def call_everything_else params = @message.type == :post_comment ? { text: @message.text } : { text: @message.text, payload: @message.payload, entities: @message.nlp.entities, traits: @message.nlp.traits } return call_block(:everything_else, params) end |
#call_expiration_time ⇒ Object
888 889 890 891 892 893 894 895 896 |
# File 'lib/core/lib/context.rb', line 888 def call_expiration_time unless @@expiration_time[self.class].nil? if @user.last_usage > @@expiration_time[self.class] delegate_to(:main, true) return true end end return false end |
#call_intents ⇒ Object
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
# File 'lib/core/lib/context.rb', line 577 def call_intents intent_data = @message.nlp.intent(false) intent = intent_data[:name] rescue nil confidence = intent_data[:confidence] rescue nil unless intent.nil? context_in_intent = self.class.get(intent) new_context = nil unless context_in_intent.nil? new_context = context_in_intent[:context] intent = context_in_intent[:param] end if !new_context.nil? && self.name != new_context # new_context = "#{new_context}.inline_query" if @message.type == :inline_query logger_call(:intents, intent, [], true) if self.delegate_to("/#{new_context}") halt return true else return false end else return true if halted? block_response = call_block(:intents, Kogno::BlockParams.new([@message.text, @message.nlp.entities, @message.nlp.traits, confidence]), intent) block_response = call_any_intent(Kogno::BlockParams.new([intent, @message.text, @message.nlp.entities, @message.nlp.traits, confidence])) unless block_response return block_response end else return false end end |
#call_keywords ⇒ Object
def call_stickers
block_response = false
unless @message.stickers.empty?
call_before(:stickers)
return true if halted?
@message.stickers.each do |sticker|
block_response = call_block(:stickers, nil, sticker)
end
block_response = call_block(:stickers, nil, "*") unless block_response
call_after(:stickers)
return block_response
else
return false
end
end
709 710 711 712 713 714 715 716 717 718 719 720 721 722 |
# File 'lib/core/lib/context.rb', line 709 def call_keywords keyword = @message.text.downcase.strip unless keyword.empty? # call_before(:keywords) return true if halted? block_response = call_block(:keywords, nil ,keyword) # call_after(:keywords) return block_response else return false end end |
#call_location ⇒ Object
776 777 778 779 780 781 782 783 784 785 786 787 |
# File 'lib/core/lib/context.rb', line 776 def call_location unless @message.location.nil? # call_before(:location) # return true if halted? block_response = call_block(:location, @message.location) # call_after(:location) return block_response else return false end end |
#call_membership_drop ⇒ Object
541 542 543 544 545 546 |
# File 'lib/core/lib/context.rb', line 541 def call_membership_drop if @message.type == :chat_activity and !@message.chat_membership_status return call_block(:membership_drop, @message.chat) end return false end |
#call_membership_new ⇒ Object
534 535 536 537 538 539 |
# File 'lib/core/lib/context.rb', line 534 def call_membership_new if @message.type == :chat_activity and @message.chat_membership_status return call_block(:membership_new, @message.chat) end return false end |
#call_nlp_entity ⇒ Object
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 |
# File 'lib/core/lib/context.rb', line 789 def call_nlp_entity block_response = false # call_before(:nlp_entity) # return true if halted? if @message.nlp.datetime_range? range = @message.nlp.datetime_range block_response = call_block(:nlp_datetime_range, Kogno::BlockParams.new([range[:from], range[:to]])) end unless block_response @message.nlp.entities.each do |entity,values| return true if block_response block_response = call_block(:nlp_entities, values, entity) end end # call_after(:nlp_entity) if block_response return block_response end |
#call_postbacks(triggers = true) ⇒ Object
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 |
# File 'lib/core/lib/context.rb', line 677 def call_postbacks(triggers=true) postback = @message.payload(true) unless postback.nil? postback = postback.downcase block_params = Kogno::BlockParams.new([postback, @message.params]) # call_before(:postbacks, block_params) if triggers return true if halted? block_response = call_block(:postbacks, @message.params, postback) block_response = call_any_postback(block_params) unless block_response # call_after(:postbacks, block_params) if triggers return block_response else return false end end |
#call_recurring_notifications ⇒ Object
561 562 563 564 565 566 567 568 569 570 571 |
# File 'lib/core/lib/context.rb', line 561 def call_recurring_notifications return false if @message.type != :recurring_notification if @message. == :active return call_block(:recurring_notification, @user.messenger_recurring_notification.data, :granted) elsif @message. == :stopped return call_block(:recurring_notification, @user.messenger_recurring_notification.data, :removed) else return false end end |
#call_regular_expressions ⇒ Object
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 |
# File 'lib/core/lib/context.rb', line 724 def call_regular_expressions text = @message.text.upcase regular_expressions = @blocks[:regular_expressions][current_sub_context()] unless regular_expressions.nil? regular_expressions.each do |regular_expression,block| @current_action = { name: :regular_expressions, value: regular_expression, deep: false } matches = text.scan(regular_expression) if matches.count > 0 logger_call("regular_expression", regular_expression, matches, true) block.call matches @called_block = { name: :regular_expressions, value: regular_expression, params: nil, context: @current, deep: false } return true unless continue? end end end deep_regular_expressions = @deep_blocks[:regular_expressions][current_sub_context()] unless deep_regular_expressions.nil? deep_regular_expressions.each do |regular_expression,block| @current_action = { name: :regular_expressions, value: regular_expression, deep: true } matches = text.scan(regular_expression) if matches.count > 0 logger_call("regular_expression", regular_expression, matches, true, true) block.call matches @called_block = { name: :regular_expressions, value: regular_expression, param: nil, context: @current, deep: true } return true unless continue? end end end logger_call("regular_expression", nil, [], false) return false end |
#call_typed_postbacks ⇒ Object
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
# File 'lib/core/lib/context.rb', line 658 def call_typed_postbacks # expression = @message.nlp.expression typed_postbacks = @user.vars[:typed_postbacks] @user.vars.delete(:typed_postbacks) if !@message.text.nil? keyword = @message.text.to_payload logger_call("typed_postbacks", keyword, [], false) postback = (typed_postbacks[keyword] rescue nil) unless postback.nil? logger.write "KEYWORD FOUND:#{postback.to_yaml}", :green @message.overwrite_postback(postback) return call_postbacks() end end return false end |
#callback(callback_name, &block) ⇒ Object
333 334 335 |
# File 'lib/core/lib/context.rb', line 333 def callback(callback_name,&block) @callbacks[callback_name.to_sym][@sub_context_route] = block end |
#change_locale(locale) ⇒ Object
1528 1529 1530 1531 1532 |
# File 'lib/core/lib/context.rb', line 1528 def change_locale(locale) @user.set_locale(locale) @message.set_nlp(locale) I18n.locale = locale end |
#change_to(route, params = {}) ⇒ Object
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 |
# File 'lib/core/lib/context.rb', line 1244 def change_to(route, params={}) route = translate_route(route) call_callback(:on_exit) unless sub_context?(route) # will_exit? prevents to not execute :on_exit callback when the the contexts changes to a sub_context context_route = self.class.router(route, @message.type) context_class = context_route[:class] unless context_route[:valid] logger.write "ERROR: #{context_class.to_s} doesn't exist.", :red return false else call_class_callback(:before_exit) if self.class != context_class # only will be executed if the contexts is changed to another unless self.halted? @user.set_context(route, params) context = context_class.new(@user,@message,@reply) context.blocks() context.call_callback(:on_enter) end end end |
#change_to_saved_context ⇒ Object
1286 1287 1288 1289 1290 1291 1292 1293 1294 |
# File 'lib/core/lib/context.rb', line 1286 def change_to_saved_context saved_context = @user.vars[:saved_context] unless saved_context.nil? @user.delete_previous_context change_to(saved_context[:context], saved_context[:params]) else self.exit() end end |
#command(input, &block) ⇒ Object
Telegram only
211 212 213 214 |
# File 'lib/core/lib/context.rb', line 211 def command(input, &block) # Telegram only set_block(:commands, input.to_s.downcase, @impact, block) @impact = :self end |
#continue ⇒ Object
432 433 434 |
# File 'lib/core/lib/context.rb', line 432 def continue @continue = true end |
#continue? ⇒ Boolean
436 437 438 439 440 441 442 443 444 |
# File 'lib/core/lib/context.rb', line 436 def continue? if @continue logger.write "********** CONTINUE **********", :red @continue = false return true else return false end end |
#current_context_route ⇒ Object
915 916 917 918 919 920 921 |
# File 'lib/core/lib/context.rb', line 915 def current_context_route if @current.nil? || @current.empty? self.name else @current end end |
#current_sub_context ⇒ Object
898 899 900 901 902 |
# File 'lib/core/lib/context.rb', line 898 def current_sub_context context_tree = @current.to_s.split(Regexp.union(["/","."])) context_tree.shift return context_tree.join(".") end |
#datetime(&block) ⇒ Object
279 280 281 282 283 284 |
# File 'lib/core/lib/context.rb', line 279 def datetime(&block) entity "wit$datetime:datetime" do |values| block.call values.map{|v| v[:value] || v[:from][:value] rescue nil} end @impact = :self end |
#datetime_range(&block) ⇒ Object
299 300 301 |
# File 'lib/core/lib/context.rb', line 299 def datetime_range(&block) set_block(:nlp_datetime_range, nil, @impact, block) end |
#debugger ⇒ Object
1540 1541 1542 1543 1544 1545 1546 |
# File 'lib/core/lib/context.rb', line 1540 def debugger { blocks: @blocks, deep_blocks: @deep_blocks, callbacks: @callbacks } end |
#deep ⇒ Object
145 146 147 148 |
# File 'lib/core/lib/context.rb', line 145 def deep @impact = :deep self end |
#deep_link(&block) ⇒ Object
195 196 197 198 |
# File 'lib/core/lib/context.rb', line 195 def deep_link(&block) set_block(:deep_link, nil, @impact, block) @impact = :self end |
#delegate_to(route, args = {}) ⇒ Object
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 |
# File 'lib/core/lib/context.rb', line 1213 def delegate_to(route, args={}) args = {ignore_everything_else:true}.merge(args) action_found = false route = translate_route(route) call_callback(:before_delegate) context_route = self.class.router(route, @message.type) context_class = context_route[:class] sub_context_route = context_route[:sub_context_route] unless context_route[:valid] logger.write "ERROR: #{context_class.to_s} doesn't exist.", :red return false end logger.write "Delegating to #{route}", :pink delegated_context = context_class.new(@user,@message,@reply,route) delegated_context.blocks() delegated_context.call_callback(:on_delegate) unless delegated_context.halted? if @run_type == :text_only action_found = delegated_context.run_for_text_only({run_blocks_method: false, ignore_everything_else: args[:ignore_everything_else]}) else action_found = delegated_context.run({run_blocks_method: false, ignore_everything_else: args[:ignore_everything_else]}) end end # if action_found halt(true) return true # else # return false # end end |
#destroy_nlp_reference ⇒ Object
1349 1350 1351 |
# File 'lib/core/lib/context.rb', line 1349 def destroy_nlp_reference @user.vars.delete(:nlp_context_ref) end |
#duration(&block) ⇒ Object
287 288 289 290 291 292 |
# File 'lib/core/lib/context.rb', line 287 def duration(&block) entity "wit$duration:duration" do |values| block.call values end @impact = :self end |
#entity(input, &block) ⇒ Object
274 275 276 277 |
# File 'lib/core/lib/context.rb', line 274 def entity(input, &block) nlp_entity(input, &block) @impact = :self end |
#everything_else(&block) ⇒ Object
328 329 330 331 |
# File 'lib/core/lib/context.rb', line 328 def everything_else(&block) set_block(:everything_else, nil, @impact, block) @impact = :self end |
#exit ⇒ Object
1329 1330 1331 1332 1333 1334 1335 |
# File 'lib/core/lib/context.rb', line 1329 def exit call_callback(:on_exit) call_class_callback(:before_exit) unless self.class == MainContext unless self.halted? @user.exit_context end end |
#exit_answer ⇒ Object
1296 1297 1298 |
# File 'lib/core/lib/context.rb', line 1296 def exit_answer self.exit() end |
#exit_context ⇒ Object
1337 1338 1339 |
# File 'lib/core/lib/context.rb', line 1337 def exit_context self.exit end |
#expression(input, &block) ⇒ Object
216 217 218 219 |
# File 'lib/core/lib/context.rb', line 216 def expression(input, &block) set_block(:expressions, input, @impact, block) @impact = :self end |
#get_default_context_for_command(command) ⇒ Object
1534 1535 1536 1537 1538 |
# File 'lib/core/lib/context.rb', line 1534 def get_default_context_for_command(command) default_context = Kogno::Application.config.routes.commands[command.to_sym] default_context = Kogno::Application.config.routes.default if default_context.nil? return default_context end |
#get_sequences ⇒ Object
1501 1502 1503 |
# File 'lib/core/lib/context.rb', line 1501 def get_sequences @sequences end |
#halt(silent = false) ⇒ Object
423 424 425 426 |
# File 'lib/core/lib/context.rb', line 423 def halt(silent=false) logger.write("********** HALT **********", :red) unless silent @halt = true end |
#halted? ⇒ Boolean
428 429 430 |
# File 'lib/core/lib/context.rb', line 428 def halted? @halt end |
#handle_message_from_memory(platform = nil) ⇒ Object
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 |
# File 'lib/core/lib/context.rb', line 1516 def (platform=nil) unless @memorized_message.nil? if platform == :telegram = Kogno::Telegram::Message.new(@memorized_message) else = Kogno::Messenger::Message.new(@memorized_message) end .handle_event() @memorized_message = nil end end |
#in_route? ⇒ Boolean
446 447 448 449 450 451 452 453 |
# File 'lib/core/lib/context.rb', line 446 def in_route? current_sub_context = current_sub_context() if current_sub_context.include?("#{@sub_context_route}.") or @sub_context_route == current_sub_context true else false end end |
#inline_query(&block) ⇒ Object
221 222 223 224 225 |
# File 'lib/core/lib/context.rb', line 221 def inline_query(&block) sub_context :inline_query do block.call end end |
#intent(input, &block) ⇒ Object
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/core/lib/context.rb', line 200 def intent(input, &block) if input.class == Array input.each do |i| set_block(:intents, i.to_s.downcase, @impact, block) end else set_block(:intents, input.to_s.downcase, @impact, block) end @impact = :self end |
#keep ⇒ Object
1300 1301 1302 |
# File 'lib/core/lib/context.rb', line 1300 def keep @user.set_context(@current) end |
#keyword(input, &block) ⇒ Object
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/core/lib/context.rb', line 248 def keyword(input, &block) if input.class == Array input.each do |i| set_block(:keywords, i.to_s.downcase, @impact, block) end else set_block(:keywords, input.to_s.downcase, @impact, block) end @impact = :self end |
#location(&block) ⇒ Object
264 265 266 267 |
# File 'lib/core/lib/context.rb', line 264 def location(&block) set_block(:location, nil, @impact, block) @impact = :self end |
#membership(type, &block) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/core/lib/context.rb', line 182 def membership(type, &block) if type == :new set_block(:membership_new, nil, :self, block) elsif type == :drop set_block(:membership_drop, nil, :self, block) end end |
#memorize_message ⇒ Object
1505 1506 1507 |
# File 'lib/core/lib/context.rb', line 1505 def @user.vars[:memory] = @message.data end |
#name ⇒ Object
1144 1145 1146 |
# File 'lib/core/lib/context.rb', line 1144 def name self.class.to_s.underscore.sub("_context","") end |
#nlp_entities_present?(entities) ⇒ Boolean
935 936 937 |
# File 'lib/core/lib/context.rb', line 935 def nlp_entities_present?(entities) (entities & @message.nlp.entities.keys).present? end |
#nlp_entity(input, &block) ⇒ Object
269 270 271 272 |
# File 'lib/core/lib/context.rb', line 269 def nlp_entity(input, &block) set_block(:nlp_entities, input.to_s.downcase.to_sym, @impact, block) @impact = :self end |
#nlp_location(&block) ⇒ Object
294 295 296 297 |
# File 'lib/core/lib/context.rb', line 294 def nlp_location(&block) nlp_entity("wit$location:location", &block) @impact = :self end |
#on_delegate(&block) ⇒ Object
345 346 347 348 349 |
# File 'lib/core/lib/context.rb', line 345 def on_delegate(&block) callback :on_delegate do block.call end end |
#on_enter(&block) ⇒ Object
357 358 359 360 361 |
# File 'lib/core/lib/context.rb', line 357 def on_enter(&block) callback :on_enter do block.call end end |
#on_exit(&block) ⇒ Object
351 352 353 354 355 |
# File 'lib/core/lib/context.rb', line 351 def on_exit(&block) callback :on_exit do block.call end end |
#params ⇒ Object
1359 1360 1361 |
# File 'lib/core/lib/context.rb', line 1359 def params @user.get_context_params end |
#parent_context_route(context_path = nil) ⇒ Object
904 905 906 907 908 |
# File 'lib/core/lib/context.rb', line 904 def parent_context_route(context_path=nil) context_tree = context_path.nil? ? @current.to_s.split(Regexp.union(["/","."])) : context_path.split(Regexp.union(["/","."])) context_tree.pop return context_tree.join(".") end |
#past(time_elapsed, &block) ⇒ Object
1447 1448 1449 1450 1451 |
# File 'lib/core/lib/context.rb', line 1447 def past(time_elapsed,&block) return false if @current_sequence_stage.nil? sequence = sequence_name() @sequences[sequence][time_elapsed.to_i] = block end |
#postback(input, &block) ⇒ Object
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/core/lib/context.rb', line 227 def postback(input, &block) if input.class == Array input.each do |i| set_block(:postbacks, i.to_s.downcase, @impact, block) end else set_block(:postbacks, input.to_s.downcase, @impact, block) end @impact = :self end |
#recurring_notification(type, &block) ⇒ Object
190 191 192 |
# File 'lib/core/lib/context.rb', line 190 def recurring_notification(type, &block) set_block(:recurring_notification, type, :self, block) end |
#regular_expression(input, &block) ⇒ Object
259 260 261 262 |
# File 'lib/core/lib/context.rb', line 259 def regular_expression(input, &block) set_block(:regular_expressions, input, @impact, block) @impact = :self end |
#remember_message ⇒ Object
1509 1510 1511 1512 1513 1514 |
# File 'lib/core/lib/context.rb', line 1509 def unless @user.vars[:memory].nil? @memorized_message = @user.vars[:memory] @user.vars.delete(:memory) end end |
#root ⇒ Object
def html_template(route,params,instance)
self.class.html_template(action_group, action, params, false, self)
end
1400 1401 1402 1403 1404 |
# File 'lib/core/lib/context.rb', line 1400 def root class_name = self.class.to_s last_occurence = class_name.rindex("Context") - 1 class_name[0..last_occurence].underscore end |
#root_context_route(context_path = nil) ⇒ Object
910 911 912 913 |
# File 'lib/core/lib/context.rb', line 910 def root_context_route(context_path=nil) context_tree = context_path.nil? ? @current.to_s.split(Regexp.union(["/","."])) : context_path.split(Regexp.union(["/","."])) return context_tree.first end |
#run(args = {run_blocks_method: true, ignore_everything_else: false}) ⇒ Object
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 |
# File 'lib/core/lib/context.rb', line 940 def run(args={run_blocks_method: true, ignore_everything_else: false}) @run_type = :full logger.write "\n\n#{self.nice_current_route} => Starting matching process\n", :bright # before_blocks() call_class_callback(:before_blocks) unless halted? blocks() if args[:run_blocks_method] #Loading blocks unless self.valid_context_route? logger.write "Error: the context in route #{@current} doesn't exist", :red self.exit_context() return false end call_before(:blocks) if !call_location() and !halted? # Deprecated if !call_postbacks() and !halted? if !call_typed_postbacks() and !halted? if !call_deep_link() and !halted? if !@message.empty? if !call_commands() and !halted? # Telegram only # if !call_stickers() and !halted? if !() and !halted? if !call_regular_expressions() and !halted? if !call_keywords() and !halted? call_before(:nlp) @message.nlp.process_phrase_once call_after(:nlp, @message.nlp.entities) # if !call_expressions() and !halted? if !call_intents() and !halted? if !call_nlp_entity() and !halted? if !call_any_number() and !halted? if !call_any_text() and !halted? unless args[:ignore_everything_else] if !call_everything_else() and !halted? logger.write "\tNo match found in #{@current}", :yellow call_after(:blocks) call_class_callback(:after_blocks) return false end else return false end end end end end # end end end end # end end else logger.write "---- THIS MESSAGE IS EMPTY ----", :red end end end end end unless halted? call_after(:blocks) call_class_callback(:after_blocks) end end return @called_block end |
#run_class_callbacks_only ⇒ Object
1014 1015 1016 1017 1018 1019 |
# File 'lib/core/lib/context.rb', line 1014 def run_class_callbacks_only call_class_callback(:before_blocks) unless halted? call_class_callback(:after_blocks) end end |
#run_for_chat_activity_only(args = {run_blocks_method: true, ignore_everything_else: false}) ⇒ Object
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 |
# File 'lib/core/lib/context.rb', line 1080 def run_for_chat_activity_only(args={run_blocks_method: true, ignore_everything_else: false}) @run_type = :text_only logger.write "\n\n#{self.nice_current_route} => Starting matching process (chat activity only)\n", :bright call_class_callback(:before_blocks) unless halted? blocks() if args[:run_blocks_method] #Loading blocks unless self.valid_context_route? logger.write "Error: the context in route #{@current} doesn't exist", :red self.exit_context() return false end call_before(:blocks) if !call_membership_new() if !call_membership_drop() logger.write "#{self.nice_current_route}.run_for_chat_activity_only => nothing found", :yellow call_after(:blocks) call_class_callback(:after_blocks) end return false end end return @called_block end |
#run_for_recurring_notification_only ⇒ Object
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 |
# File 'lib/core/lib/context.rb', line 1112 def run_for_recurring_notification_only() @run_type = :text_only logger.write "\n\n#{self.nice_current_route} => Starting matching process (recurring notification only)\n", :bright call_class_callback(:before_blocks) unless halted? blocks() unless self.valid_context_route? logger.write "Error: the context in route #{@current} doesn't exist", :red self.exit_context() return false end call_before(:blocks) call_recurring_notifications() if !call_postbacks(false) and !halted? call_class_callback(:after_blocks) return false end end call_class_callback(:after_blocks) return @called_block end |
#run_for_text_only(args = {run_blocks_method: true, ignore_everything_else: false}) ⇒ Object
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 |
# File 'lib/core/lib/context.rb', line 1021 def run_for_text_only(args={run_blocks_method: true, ignore_everything_else: false}) @run_type = :text_only logger.write "\n\n#{self.nice_current_route} => Starting matching process (text only)\n", :bright # before_blocks() call_class_callback(:before_blocks) unless halted? blocks() if args[:run_blocks_method] #Loading blocks unless self.valid_context_route? logger.write "Error: the context in route #{@current} doesn't exist", :red self.exit_context() return false end call_before(:blocks) if !@message.empty? if !call_regular_expressions() and !halted? if !call_keywords() and !halted? call_before(:nlp) @message.nlp.process_phrase_once call_after(:nlp, @message.nlp.entities) # if !call_expressions() and !halted? if !call_intents() and !halted? if !call_nlp_entity() and !halted? if !call_any_number() and !halted? if !call_any_text() and !halted? unless args[:ignore_everything_else] if !call_everything_else() and !halted? logger.write "#{self.class.name.to_s}.run_for_text_only => nothing found | #{@current}", :yellow call_after(:blocks) call_class_callback(:after_blocks) return false end else return false end end end end end # end end end else logger.write "---- THIS MESSAGE IS EMPTY AND WE DON'T DOING NOTHING WITH IT ----", :red end unless halted? call_after(:blocks) call_class_callback(:after_blocks) end end return @called_block end |
#save_current_context ⇒ Object
1282 1283 1284 |
# File 'lib/core/lib/context.rb', line 1282 def save_current_context @user.save_current_context end |
#sequence(stage_name, &block) ⇒ Object
1439 1440 1441 1442 1443 1444 1445 |
# File 'lib/core/lib/context.rb', line 1439 def sequence(stage_name, &block) @current_sequence_stage = stage_name.to_sym sequence = sequence_name() @sequences[sequence] = {} if @sequences[sequence].nil? block.call @current_sequence_stage = nil end |
#sequence_name ⇒ Object
1453 1454 1455 1456 |
# File 'lib/core/lib/context.rb', line 1453 def sequence_name return nil if @current_sequence_stage.nil? "#{self.name}.#{@current_sequence_stage}" end |
#sequences ⇒ Object
Sequences
1408 1409 1410 1411 1412 |
# File 'lib/core/lib/context.rb', line 1408 def sequences #You should overwrite this in the Context end |
#set_block(action, input, impact, block) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/core/lib/context.rb', line 122 def set_block(action, input, impact, block) if input.nil? @blocks[action][@sub_context_route] = block if impact == :deep # logger.write "---- ADD DEEP IMPACT TO #{action}", :red @deep_blocks[action] = block end else @blocks[action][@sub_context_route] = {} if @blocks[action][@sub_context_route].nil? @blocks[action][@sub_context_route][input] = block if impact == :deep @deep_blocks[action][input] = block # logger.write "---- ADD DEEP IMPACT TO #{action}(#{imput})", :red end end # if impact == :deep # @deep_blocks[action][input] = block # end end |
#set_nlp_reference(**values) ⇒ Object
This is used to send context references to Wit wit.ai/docs/http/20170307#context_link
1345 1346 1347 |
# File 'lib/core/lib/context.rb', line 1345 def set_nlp_reference(**values) # This is used to send context references to Wit https://wit.ai/docs/http/20170307#context_link @user.vars[:nlp_context_ref] = values end |
#start_sequence(stage_route, context_name = nil) ⇒ Object
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 |
# File 'lib/core/lib/context.rb', line 1414 def start_sequence(stage_route, context_name=nil) stage_array = stage_route.to_s.split("/") if stage_array.count == 2 context_name = stage_array[0] stage = stage_array[1] else context_name = self.name stage = stage_route end @user.set_sequence(stage, context_name) end |
#sticker(input, &block) ⇒ Object
238 239 240 241 |
# File 'lib/core/lib/context.rb', line 238 def sticker(input, &block) set_block(:stickers, sticker_id, @impact, block) @impact = :self end |
#stop_sequence(stage_route) ⇒ Object
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 |
# File 'lib/core/lib/context.rb', line 1426 def stop_sequence(stage_route) stage_array = stage_route.to_s.split("/") if stage_array.count == 2 context_name = stage_array[0] stage = stage_array[1] else context_name = self.name stage = stage_route end @user.exit_sequence(stage, context_name) end |
#sub_context(route, &block) ⇒ Object
end
405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/core/lib/context.rb', line 405 def sub_context(route, &block) @sub_contexts_tree.push(route) @sub_context_route = @sub_contexts_tree.join(".") @sub_contexts.push(@sub_context_route) if in_route? # The sub_context to execute will be only the ones that are in the route of the current context. # @halt = {@sub_context_route => false} block.call @user.get_context_params end @sub_contexts_tree.pop @sub_context_route = @sub_contexts_tree.join(".") end |
#sub_context?(sub_context) ⇒ Boolean
1341 1342 1343 |
# File 'lib/core/lib/context.rb', line 1341 def sub_context?(sub_context) (sub_context =~ /#{@current.to_s}/) == 0 end |
#template(action_group, action, params = {}) ⇒ Object
1381 1382 1383 |
# File 'lib/core/lib/context.rb', line 1381 def template(action_group,action, params={}) self.class.base_template(action_group, action, params, false, self) end |
#translate_route(route) ⇒ Object
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 |
# File 'lib/core/lib/context.rb', line 1304 def translate_route(route) route = route.to_s case route when "/", "", nil return "" when :parent, "../" return self.parent_context_route else if route[0] == "/" tmp_route = route.split("/") tmp_route.slice!(0) return tmp_route.join(".") elsif route[0..2] == "../" return "#{self.parent_context_route}.#{route[3..]}" elsif route [0..1] == "./" return "#{self.current_context_route}.#{route[2..]}" elsif !route.index("/").nil? return route.gsub("/",".") else # return "#{@current}.#{route}" return "#{self.current_context_route}.#{route}" end end end |
#type ⇒ Object
1148 1149 1150 |
# File 'lib/core/lib/context.rb', line 1148 def type :context end |
#valid_context_route? ⇒ Boolean
923 924 925 926 927 928 929 930 931 932 933 |
# File 'lib/core/lib/context.rb', line 923 def valid_context_route? if @current.nil? || @current.empty? return true elsif @current == self.name return true elsif @sub_contexts.include?(current_sub_context()) return true else return false end end |