Sha256: ea8ef361ec43287b862a26043cae05d81d96d9c9f2ad29eed5050f34d61cc83c

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 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
      policy-enforcement
      pass_thread_control
      take_thread_control
    ].freeze

    include Bobot::GraphFacebook

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

      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

      def receive_message(payload)
        receive(payload, false)
      end

      def receive_standby(payload)
        receive(payload, true)
      end

      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

      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

      def hooks
        @hooks ||= {}
      end

      def unhook
        @hooks = {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bobot-5.0.4 lib/bobot/commander.rb