Module: Bobot::Commander

Includes:
GraphFacebook
Defined in:
lib/bobot/commander.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

EVENTS =
%i[
  message
  delivery
  postback
  optin
  read
  account_linking
  referral
  message_echo
  message_request
].freeze

Constants included from GraphFacebook

GraphFacebook::GRAPH_FB_URL, GraphFacebook::GRAPH_HEADERS

Class Method Summary collapse

Methods included from GraphFacebook

included

Class Method Details

.deliver(body:, query:) ⇒ Object



20
21
22
23
24
# File 'lib/bobot/commander.rb', line 20

def deliver(body:, query:)
  graph_post '/me/messages', body: body, query: {
    access_token: query.fetch(:access_token),
  }
end

.hooksObject



49
50
51
# File 'lib/bobot/commander.rb', line 49

def hooks
  @hooks ||= {}
end

.on(event, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/bobot/commander.rb', line 26

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) ⇒ Object



34
35
36
37
38
39
# File 'lib/bobot/commander.rb', line 34

def receive(payload)
  Bobot::CommanderJob.send(
    Bobot.config.async ? :perform_later : :perform_now,
    { payload: payload }
  )
end

.trigger(payload) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/bobot/commander.rb', line 41

def trigger(payload)
  event = Bobot::Event.parse(payload)
  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

.unhookObject



53
54
55
# File 'lib/bobot/commander.rb', line 53

def unhook
  @hooks = {}
end