Class: Slackify::SlackController

Inherits:
ActionController::API
  • Object
show all
Includes:
SlackTokenVerify
Defined in:
app/controllers/slackify/slack_controller.rb

Constant Summary collapse

SLACK_TIMEOUT_SECONDS =
3.seconds

Instance Method Summary collapse

Instance Method Details

#event_callbackObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/slackify/slack_controller.rb', line 9

def event_callback
  if params[:type] == "url_verification"
    render plain: params["challenge"]
  elsif Slackify.configuration.custom_event_type_handlers[params[:event][:type]]
    handle_custom_event_type
  elsif params[:event][:type] == "message"
    handle_direct_message_event
    head :ok
  else
    head :bad_request
  end
end

#interactive_callbackObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/slackify/slack_controller.rb', line 22

def interactive_callback
  parsed_payload = JSON.parse(params[:payload])

  callback_id = if parsed_payload.key?('view')
                  parsed_payload.dig('view', 'callback_id')
                else
                  parsed_payload['callback_id']
                end

  response = handler_from_callback_id(callback_id).call(parsed_payload)
  if !response.nil?
    Timeout.timeout(SLACK_TIMEOUT_SECONDS) do
      render json: response
    end
  else
    head :ok
  end
rescue Timeout::Error
  raise Timeout::Error, "Slack interactive callback timed out for #{callback_id}"
end

#slash_command_callbackObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/slackify/slack_controller.rb', line 43

def slash_command_callback
  handler_class = params[:handler_class]
  handler_method = params[:handler_method]
  verify_handler_slash_permission(handler_class, handler_method)

  response = handler_class.camelize.constantize.method(handler_method).call(params)
  if !response.nil?
    Timeout.timeout(SLACK_TIMEOUT_SECONDS) do
      render json: response
    end
  else
    head :ok
  end
rescue Timeout::Error
  raise Timeout::Error, "Slack slash command callback timed out for command #{params[:command]}"
end