module Bobot::Commander

Constants

EVENTS

Public Class Methods

deliver(endpoint: '/me/messages', body:, query:) click to toggle source
# File lib/bobot/commander.rb, line 23
def deliver(endpoint: '/me/messages', body:, query:)
  graph_post(
    endpoint,
    body: body,
    query: {
      access_token: query.fetch(:access_token),
    },
  )
end
hooks() click to toggle source
# File lib/bobot/commander.rb, line 71
def hooks
  @hooks ||= {}
end
on(event, &block) click to toggle source
# File lib/bobot/commander.rb, line 33
def on(event, &block)
  if EVENTS.include? event
    hooks[event] = block
  else
    warn "[bobot trigger] Ignoring #{event.class} (not available in [#{EVENTS.join(', ')}])"
  end
end
receive(payload, standby) click to toggle source
# File lib/bobot/commander.rb, line 49
def receive(payload, standby)
  event = Bobot::Event.parse(payload)
  event.mark_as_seen if event.page.present? && !standby && [Bobot::Event::MessageEcho, Bobot::Event::PassThreadControl, Bobot::Event::TakeThreadControl].none? { |c| event.is_a?(c) }
  hooks.fetch(Bobot::Event::EVENTS.invert[event.class].to_sym)
  Bobot::CommanderJob.send(
    Bobot.config.async ? :perform_later : :perform_now,
    { payload: payload },
  )
rescue KeyError
  warn "[bobot trigger] Ignoring #{event.class} (no hook registered)"
end
receive_message(payload) click to toggle source
# File lib/bobot/commander.rb, line 41
def receive_message(payload)
  receive(payload, false)
end
receive_standby(payload) click to toggle source
# File lib/bobot/commander.rb, line 45
def receive_standby(payload)
  receive(payload, true)
end
trigger(payload) click to toggle source
# File lib/bobot/commander.rb, line 61
def trigger(payload)
  event = Bobot::Event.parse(payload)
  return if !event.page.present?

  hook = hooks.fetch(Bobot::Event::EVENTS.invert[event.class].to_sym)
  hook.call(event)
rescue KeyError
  warn "[bobot trigger] Ignoring #{event.class} (no hook registered)"
end
unhook() click to toggle source
# File lib/bobot/commander.rb, line 75
def unhook
  @hooks = {}
end