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
  policy-enforcement
].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



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

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

.hooksObject



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

def hooks
  @hooks ||= {}
end

.on(event, &block) ⇒ Object



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

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



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

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

.trigger(payload) ⇒ Object



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

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



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

def unhook
  @hooks = {}
end