Class: WitBot::Bot::Conversation::Base

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/wit_bot/bot/conversation/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(json) ⇒ Object



55
56
57
# File 'lib/wit_bot/bot/conversation/base.rb', line 55

def self.from_hash(json)
  self.new.from_hash json.with_indifferent_access
end

Instance Method Details

#from_hash(json) ⇒ Object



50
51
52
53
# File 'lib/wit_bot/bot/conversation/base.rb', line 50

def from_hash(json)
  @thread = WitBot::MessageThread.from_hash json[:thread]
  self
end

#input(input, meta = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/wit_bot/bot/conversation/base.rb', line 23

def input(input, meta=nil)
  message = thread.create_message input
  message.meta = meta
  message.send 1, false
  changed
  notify_observers :input, message
  message
end

#listen_with(listener_class) ⇒ Object



32
33
34
# File 'lib/wit_bot/bot/conversation/base.rb', line 32

def listen_with(listener_class)
  listener_class.new self
end

#messages(user: true, bot: true) ⇒ Object



15
16
17
# File 'lib/wit_bot/bot/conversation/base.rb', line 15

def messages(user: true, bot: true)
  thread.messages_list user, bot
end

#metadataObject



7
8
9
# File 'lib/wit_bot/bot/conversation/base.rb', line 7

def 
  thread.
end

#output(text, meta = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/wit_bot/bot/conversation/base.rb', line 36

def output(text, meta=nil)
  message = thread.create_bot_message text
  message.meta = meta
  changed
  notify_observers :output, message
  message
end

#send_message(text, sender = nil, meta = nil) ⇒ Object



19
20
21
# File 'lib/wit_bot/bot/conversation/base.rb', line 19

def send_message(text, sender=nil, meta=nil)
  sender && sender.bot? ? output(text, meta) : input(text, meta)
end

#threadObject



11
12
13
# File 'lib/wit_bot/bot/conversation/base.rb', line 11

def thread
  @thread ||= MessageThread.new("#{self.class.name}-#{SecureRandom.uuid}")
end

#to_hashObject



44
45
46
47
48
# File 'lib/wit_bot/bot/conversation/base.rb', line 44

def to_hash
  {
    thread: @thread
  }
end