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_FB_VERSION

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



60
61
62
# File 'lib/bobot/commander.rb', line 60

def hooks
  @hooks ||= {}
end

.on(event, &block) ⇒ Object



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

def on(event, &block)
  unless EVENTS.include? event
    raise Error.new("#{event} is not a valid event; available events are #{EVENTS.join(',')}")
  end
  hooks[event] = block
end

.receive(payload) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bobot/commander.rb', line 33

def receive(payload)
  event = Bobot::Event.parse(payload)
  hooks.fetch(Bobot::Event::EVENTS.invert[event.class].to_sym)
  puts "[ActiveJob] << Bobot::HookJob with event #{event.class}" if Bobot.config.debug_log
  # event.mark_as_seen
  job = Bobot::CommanderJob
  if Bobot.config.async
    job.perform_later(payload: payload)
  else
    job.perform_now(payload: payload)
  end
rescue KeyError
  warn "Ignoring #{event.class} (no hook registered)"
end

.trigger(payload) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bobot/commander.rb', line 48

def trigger(payload)
  event = Bobot::Event.parse(payload)
  hook = hooks.fetch(Bobot::Event::EVENTS.invert[event.class].to_sym)
  puts "[ActiveJob] >> Bobot::HookJob related to event #{name.class}" if Bobot.config.debug_log
  # event.show_typing(state: true)
  hook.call(event)
  # event.show_typing(state: false)
  [event, event.payloads_sent[1..-2]]
rescue KeyError
  warn "Ignoring #{event.class} (no hook registered)"
end

.unhookObject



64
65
66
# File 'lib/bobot/commander.rb', line 64

def unhook
  @hooks = {}
end