Class: BotFramework::Bot

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/bot_framework/bot.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.recognizersObject

Returns the value of attribute recognizers.



5
6
7
# File 'lib/bot_framework/bot.rb', line 5

def recognizers
  @recognizers
end

Class Method Details

.add_recognizer(recognizer) ⇒ Object



21
22
23
# File 'lib/bot_framework/bot.rb', line 21

def add_recognizer(recognizer)
  recognizers << recognizer
end

.conversation_data(activity) ⇒ Object



73
74
75
76
77
78
# File 'lib/bot_framework/bot.rb', line 73

def conversation_data(activity)
  BotFramework::BotState.new('').get_conversation_data(
    'channel_id' => activity.channel_id,
    'conversation_id' => activity.conversation.id
  ).data || {}
end

.hooksObject



80
81
82
# File 'lib/bot_framework/bot.rb', line 80

def hooks
  @hooks ||= {}
end

.intent_callbacksObject



88
89
90
# File 'lib/bot_framework/bot.rb', line 88

def intent_callbacks
  @intent_callbacks ||= {}
end

.on(event, &block) ⇒ Object



7
8
9
# File 'lib/bot_framework/bot.rb', line 7

def on(event, &block)
  hooks[event] = block
end

.on_intent(intent, &block) ⇒ Object



11
12
13
# File 'lib/bot_framework/bot.rb', line 11

def on_intent(intent, &block)
  intent_callbacks[intent] = block
end

.receive(payload) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/bot_framework/bot.rb', line 47

def receive(payload)
  trigger(payload.type.to_sym)
  # Run on default
  trigger(:activity, payload)
  recognizers.each do |recognizer|
    recognizer.recognize(message: payload.as_json) do |_error, intents|
      trigger_intent_call_back(intents[:intent], payload, intents) if intents[:intent]
    end
  end
end

.recognizer=(recognizer) ⇒ Object



15
16
17
18
# File 'lib/bot_framework/bot.rb', line 15

def recognizer=(recognizer)
  warn "DEPRECATED: Use add_recognizer method instead"
  add_recognizer(recognizer)
end

.reply(activity, message = '') ⇒ Object



58
59
60
# File 'lib/bot_framework/bot.rb', line 58

def reply(activity, message = '')
  activity.reply(message)
end

.reset_hooksObject



84
85
86
# File 'lib/bot_framework/bot.rb', line 84

def reset_hooks
  @hooks = {}
end

.set_conversation_data(activity, data) ⇒ Object



66
67
68
69
70
71
# File 'lib/bot_framework/bot.rb', line 66

def set_conversation_data(activity, data)
  data = BotFramework::BotData.new(data: data, e_tag: '*') if data.is_a? Hash
  BotFramework::BotState.new('').set_conversation_data('channel_id' => activity.channel_id,
                                                       'conversation_id' => activity.conversation.id,
                                                       'bot_data' => data)
end

.trigger(event, *args) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/bot_framework/bot.rb', line 29

def trigger(event, *args)
  # hooks.fetch(event).call(*args)
  if hooks[event].nil?
    BotFramework.logger.info "No call back registered for #{event}"
    return false
  end
  instance_exec(*args, &hooks.fetch(event))
end

.trigger_intent_call_back(intent, *args) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/bot_framework/bot.rb', line 38

def trigger_intent_call_back(intent, *args)
  if intent_callbacks[intent].nil?
    BotFramework.logger.info "No call back registered for #{intent}"
    trigger_intent_call_back(:default, *args) if intent_callbacks[:default]
    return false
  end
  instance_exec(*args, &intent_callbacks.fetch(intent))
end

.user_data=(data) ⇒ Object



62
63
64
# File 'lib/bot_framework/bot.rb', line 62

def user_data=(data)
  BotFramework.logger.info "Data set as #{data}"
end