Sha256: c8c369b442090d215cf3ad2d81dc2c10b65ec6ee9c8f23bcc782ef6f3c59661a

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

module Bobot
  module Commander
    class Error < Bobot::FacebookError; end

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

    include Bobot::GraphFacebook

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

      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

      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

      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

      def hooks
        @hooks ||= {}
      end

      def unhook
        @hooks = {}
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bobot-2.6.2 lib/bobot/commander.rb
bobot-2.6.1 lib/bobot/commander.rb
bobot-2.6.0 lib/bobot/commander.rb
bobot-2.5.0 lib/bobot/commander.rb
bobot-2.3.0 lib/bobot/commander.rb
bobot-2.1.0 lib/bobot/commander.rb