Skip to content

Commit cb2eada

Browse files
committed
Use custom Slack client to avoid SSL issues with the previous one.
1 parent 1512ee1 commit cb2eada

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

app/controllers/games_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'custom_slack_client'
2+
13
class GamesController < ApplicationController
24
skip_before_action :verify_authenticity_token, only: [:say]
35
http_basic_authenticate_with name: "", password: Rails.application.secrets.say_password, except: :show
@@ -26,9 +28,12 @@ def say
2628
text = params[:text]
2729
game_id = params[:game_id]
2830
game = ReadModel::GameReadModel.find_by(id: game_id)
29-
client = Slack::Web::Client.new(token: game.slack_token)
3031
begin
31-
client.chat_postMessage(channel: game.slack_channel, text: text)
32+
CustomSlackClient.post_message(
33+
channel: game.slack_channel,
34+
text: text,
35+
token: game.slack_token
36+
)
3237
rescue => e
3338
puts(e.inspect)
3439
end

lib/custom_slack_client.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'faraday'
2+
require 'faraday/net_http_persistent'
3+
4+
class CustomSlackClient
5+
CONNECTION = Faraday.new(
6+
url: 'https://slack.com',
7+
headers: { 'Content-Type' => 'application/json' }
8+
) do |faraday|
9+
faraday.adapter :net_http_persistent
10+
end
11+
12+
def self.post_message(channel:, text:, token:)
13+
response = CONNECTION.post('/api/chat.postMessage') do |req|
14+
req.headers['Authorization'] = "Bearer #{token}"
15+
req.headers['Accept'] = "application/json; charset=utf-8"
16+
req.headers['Content-Type'] = "application/json"
17+
req.body = {
18+
channel: channel,
19+
text: text
20+
}.to_json
21+
end
22+
23+
body = JSON.parse(response.body)
24+
unless body['ok']
25+
Rails.logger.error "Error posting to Slack: #{body['error']}"
26+
end
27+
28+
response
29+
end
30+
end

notifications/lib/notifications/slack_notifier.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'custom_slack_client'
2+
13
module Notifications
24
class SlackNotifier
35
def initialize(logger: nil, event_store:)
@@ -23,10 +25,10 @@ def call(event)
2325
def new_turn_notification(event)
2426
game = ReadModel::GameReadModel.find_by(id: event.data[:game_id])
2527
return unless game && game.slack_token
26-
client = Slack::Web::Client.new(token: game.slack_token)
27-
client.chat_postMessage(
28+
CustomSlackClient.post_message(
2829
channel: game.slack_channel,
2930
text: game.build_slack_new_turn_message(event.data),
31+
token: game.slack_token
3032
)
3133
end
3234

@@ -37,13 +39,11 @@ def maybe_notify_remaining_players(event)
3739
if current_turn.unfinished_player_ids.size <= game.number_of_remaining_players_to_notify
3840
remaining_players_mentions =
3941
Player.where(id: current_turn.unfinished_player_ids).map { |player| "<@#{player.slack_id}>" }.join(" ")
40-
client = Slack::Web::Client.new(token: game.slack_token)
41-
response =
42-
client.chat_postMessage(
43-
channel: game.slack_channel,
44-
text: "Turn " + remaining_players_mentions,
45-
)
46-
logger.warn("Slack response: #{response.pretty_inspect}") if logger
42+
CustomSlackClient.post_message(
43+
channel: game.slack_channel,
44+
text: "Turn " + remaining_players_mentions,
45+
token: game.slack_token
46+
)
4747
end
4848
end
4949

notifications/spec/notifications/slack_notifier_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def given(*domain_events)
5454
"Authorization" => "Bearer xoxb-302139800755-nR1O848GLyVS5ZfNNMpBLm0b",
5555
"Accept" => "application/json; charset=utf-8",
5656
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
57-
"Content-Type" => "application/x-www-form-urlencoded"
57+
"Content-Type" => "application/json"
5858
}
5959
)
6060
.to_return(status: 200, body: {ok: true}.to_json, headers: {})
@@ -76,7 +76,7 @@ def given(*domain_events)
7676
"Authorization" => "Bearer xoxb-302139800755-nR1O848GLyVS5ZfNNMpBLm0b",
7777
"Accept" => "application/json; charset=utf-8",
7878
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
79-
"Content-Type" => "application/x-www-form-urlencoded"
79+
"Content-Type" => "application/json"
8080
}
8181
)
8282
.to_return(status: 200, body: {ok: true}.to_json, headers: {})
@@ -119,7 +119,7 @@ def given(*domain_events)
119119
"Authorization" => "Bearer xoxb-302139800755-nR1O848GLyVS5ZfNNMpBLm0b",
120120
"Accept" => "application/json; charset=utf-8",
121121
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
122-
"Content-Type" => "application/x-www-form-urlencoded"
122+
"Content-Type" => "application/json"
123123
}
124124
)
125125
.to_return(status: 200, body: {ok: true}.to_json, headers: {})
@@ -134,7 +134,7 @@ def given(*domain_events)
134134
"Authorization" => "Bearer xoxb-302139800755-nR1O848GLyVS5ZfNNMpBLm0b",
135135
"Accept" => "application/json; charset=utf-8",
136136
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
137-
"Content-Type" => "application/x-www-form-urlencoded"
137+
"Content-Type" => "application/json"
138138
}
139139
)
140140
.to_return(status: 200, body: {ok: true}.to_json, headers: {})

0 commit comments

Comments
 (0)