class Sinbotra::Messenger::Bot

Constants

Conversation
GET_STARTED_ID
Listener

Attributes

_conversations[R]
_default_listener[R]
_dont_understand_phrases[R]
_listeners[R]
get_started_method[R]
current_user[R]
get_started_method[R]
message[R]
platform[R]

Public Class Methods

conversations(conversation_map) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 24
def conversations(conversation_map)
  c = conversation_map.each_with_object({}) { |(id, klass), h|
    h[id] = Conversation.new(id, klass)
  }
  @_conversations = c
end
default_listener(listener) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 20
def default_listener(listener)
  @_default_listener = listener
end
dont_understand_phrases(phrases) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 31
def dont_understand_phrases(phrases)
  @_dont_understand_phrases = phrases
end
get_started(method_name) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 11
def get_started(method_name)
  @get_started_method = method_name
  Sinbotra::Messenger::Platform.get_started(GET_STARTED_ID)
end
listeners(methods) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 16
def listeners(methods)
  @_listeners = methods.map { |name, matcher| Listener.new(matcher, name) }
end
new(message) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 48
def initialize(message)
  @current_user = UserPresenter.new(message.sender)
  @message      = MessagePresenter.new(message)
  @platform     = Platform.new(@current_user.id)
  @phrases      = setup_phrases
  log_message!
end

Public Instance Methods

conversations() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 43
def conversations;      self.class._conversations;     end
default_listener() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 42
def default_listener;   self.class._default_listener;  end
get_intent(msg) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 110
def get_intent(msg)
  # TODO: Send msg to NLP API
end
get_started_message?() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 92
def get_started_message?
  pb = message.postback
  pb.to_s == self.class::GET_STARTED_ID
end
handle_default!() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 101
def handle_default!
  send(default_listener, message)
end
handle_get_started!() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 97
def handle_get_started!
  send(get_started_method, message)
end
has_conversation?(intent) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 105
def has_conversation?(intent)
  # TODO: Figure this out ;)
  false
end
in_conversation?() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 61
def in_conversation?; current_user.in_conversation?; end
listeners() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 41
def listeners;          self.class._listeners;         end
make_conversation(convo_id) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 70
def make_conversation(convo_id)
  convo = conversations[convo_id]
  convo.klass.new(self, platform)
end
next_dont_understand_phrase() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 114
def next_dont_understand_phrase
  return "I don't understand" unless @phrases.any?
  @phrase_index ||= 0
  phrase = @phrases[@phrase_index % @phrases.size]
  @phrase_index += 1
  phrase
end
respond() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 56
def respond
  $logger.debug("Responding...")
  in_conversation? ? send_to_conversation! : send_to_listeners!
end
send_to_conversation!() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 75
def send_to_conversation!
  $logger.debug("Send message to conversation")
  id    = current_user.conversation.id
  convo = make_conversation(id)
  convo.continue_dialogue
end
send_to_listeners!() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 82
def send_to_listeners!
  $logger.debug("Send message through handler stack")
  listener = listeners.find { |l| matches_message?(l) }
  if listener.nil?
    get_started_message? ? handle_get_started! : handle_default!
  else
    send(listener.method_name, message)
  end
end
start_conversation(convo_id) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 63
def start_conversation(convo_id)
  current_user.start_conversation(convo_id)
  convo = make_conversation(convo_id)
  $logger.debug("Start Conversation: #{convo}")
  convo.start
end

Private Instance Methods

log_message!() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 138
def log_message!
  Sinbotra::MessageStore.log_in_message!(:facebook, message, message.sender)
end
matches_message?(listener) click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 124
def matches_message?(listener)
  if txt = message.text
    return true if txt.match(listener.matcher)
  elsif pb = message.postback
    return true if pb.to_s.match(listener.matcher)
  end
  return false
end
setup_phrases() click to toggle source
# File lib/sinbotra/messenger/bot.rb, line 133
def setup_phrases
  phrases = self.class._dont_understand_phrases || []
  phrases.shuffle
end