Class: SlackEventApi

Inherits:
Envoy
  • Object
show all
Defined in:
lib/botanalytics/slack.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ SlackEventApi

Returns a new instance of SlackEventApi.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# 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

Instance Method Details

#log(payload) ⇒ Object

Parameters:

  • payload

    Hash



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# 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 < [email protected] >',
                payload
            )
        end
    else
        fails(validation[:err], validation[:reason], payload)
    end
end