-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication_controller.rb
More file actions
50 lines (40 loc) · 1.42 KB
/
application_controller.rb
File metadata and controls
50 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Slack::ApplicationController < ApplicationController
skip_before_action :verify_authenticity_token
skip_before_action :check_user_token
before_action :valid_slack_request?
private
def valid_slack_request?
@verified ||= verify_slack_signature
end
def verify_slack_signature
timestamp = request.headers["X-Slack-Request-Timestamp"]
signature = request.headers["X-Slack-Signature"]
if Time.now.to_i - timestamp.to_i > 300
@verified = false
return
end
base_string = "v0:#{timestamp}:#{request.raw_post}"
my_signature = "v0=" + OpenSSL::HMAC.hexdigest(
"SHA256",
ENV["SLACK_SIGNING_SECRET"],
base_string
)
ActiveSupport::SecurityUtils.secure_compare(signature, my_signature)
end
def slack_client
@slack_client ||= SlackClient::Client.instance
end
def open_view(view, trigger_id:)
slack_client = SlackClient::Client.instance
slack_client.views_open view: view, trigger_id: trigger_id
rescue Slack::Web::Api::Errors::SlackError => e
Rails.logger.error "Failed to open Slack modal: #{e.message} #{e.response_metadata}"
head :unprocessable_entity
end
def send_message(message, channel_id:)
SlackClient::Client.instance.chat_postMessage(channel: channel_id, blocks: message)
rescue Slack::Web::Api::Errors::SlackError => e
Rails.logger.error "Failed to send Slack message: #{e.message}"
Sentry.capture_exception(e)
end
end