class SlackEventApi

Public Class Methods

new(params={}) click to toggle source
Calls superclass method Envoy::new
# File lib/botanalytics/slack.rb, line 179
def initialize(params={})
    super(params)
    @slack_token = params.fetch(:slack_token, nil)
    raise ArgumentError.new "slack_token can not be nil or empty" if @slack_token.nil? || @slack_token.to_s.empty?
    @path = 'messages/slack/event/'
    @initialize_path = 'bots/slack/initialize/'
    @interactive_path = 'messages/slack/interactive/'
    @async = params.fetch(:async, false)
    informs("Logging enabled for #{self.class.name}...")
    if @async
        require 'concurrent'
        @executor_service = Concurrent::ThreadPoolExecutor.new(
            min_threads: 1,
            max_threads: Concurrent.processor_count * 2 + 1,
            max_queue: Concurrent.processor_count * 1000,
            fallback_policy: :caller_runs
        )
        @executor_service.post do
            fetch
        end
        informs("Mode: Async...")
    else
        Thread.new do
            fetch
        end
    end
    @accepted_types = %w(event_callback interactive_message)
end

Public Instance Methods

log(payload) click to toggle source

@param payload Hash

# File lib/botanalytics/slack.rb, line 257
def log(payload)

    if payload.is_a?(Hash)
        return unless payload['challenge'].nil?
    end

    validation = validate(payload)
    if validation[:ok]
        if @accepted_types.include?(payload['type'])
            informs('Logging message...')
            informs(payload)
            if @async
                @executor_service.post do
                    submits(payload, payload['type'] == 'event_callback' ? @path : @interactive_path)
                end
            else
                submits(payload, payload['type'] == 'event_callback' ? @path : @interactive_path)
            end
        else
            fails(
                Exception.new("Expected types, #{@accepted_types} but found #{payload['type']}"),
                'If you are sure this is a new event type, contact us < tech@botanalytics.co >',
                payload
            )
        end
    else
        fails(validation[:err], validation[:reason], payload)
    end
end

Private Instance Methods

fetch() click to toggle source
# File lib/botanalytics/slack.rb, line 208
def fetch
    informs("Initializing slack event api bot...")
    wait_interval = 2
    update_interval = 3600
    uri = URI.parse('https://slack.com/api/rtm.start')
    while true
        begin
            response = Net::HTTP.post_form(uri, :token => @slack_token)
            case response
            when Net::HTTPSuccess
                start_data = JSON.parse(response.body)
                if start_data['ok']
                    if submits(start_data, @initialize_path, "Successfully updated bot '#{start_data['self']['name']}' info...")
                        informs("Botanalytics::#{self.class.name} initialized team info successfully...")
                        sleep update_interval
                        wait_interval = 2
                    else
                        sleep wait_interval
                        if wait_interval < 20
                            wait_interval += 3
                        end
                        informs("Botanalytics::#{self.class.name} can not initialize team info, retrying...")
                    end
                else
                    # nope there is a problem here, try
                    sleep wait_interval
                    if wait_interval < 20
                        wait_interval += 3
                    end
                    informs("Botanalytics::#{self.class.name} can not initialize team info, retrying...")
                end
            else
                informs("Botanalytics::#{self.class.name} can not initialize team info, Reason: statuscode:#{response.code} retrying...")
                sleep wait_interval
                if wait_interval < 20
                    wait_interval += 3
                end
            end
        rescue Exception => e
            informs("Botanalytics::#{self.class.name} can not initialize team info due to http error, Error: #{e.message}, retrying...")
            sleep wait_interval
            if wait_interval < 20
                wait_interval += 3
            end
        end
    end
end
validate(payload) click to toggle source
# File lib/botanalytics/slack.rb, line 287
def validate(payload)
    is_valid(payload, "", 'event', 'type')
end