-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathslack_poster.rb
110 lines (98 loc) · 2.68 KB
/
slack_poster.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class SlackPoster
attr_accessor :webhook_url, :poster, :mood, :season_name
def initialize(team_channel, mood)
@webhook_url = ENV["SLACK_WEBHOOK"]
@team_channel = team_channel
@mood = mood
@today = Date.today
mood_hash
channel
create_poster
end
def create_poster
@poster = Slack::Poster.new(webhook_url.to_s, slack_options)
end
def send_request(message)
if ENV["DRY"]
puts "Will post #{mood} message to #{channel} on #{today.strftime('%A')}"
puts slack_options.inspect
puts message
else
poster.send_message(message)
end
rescue StandardError => e
puts "Error sending request: #{e.message}"
end
private
attr_reader :today
def slack_options
{
icon_emoji: @mood_hash[:icon_emoji],
username: @mood_hash[:username],
channel: @team_channel,
}
end
def mood_hash
@mood_hash = {}
check_season
set_mood_from_team
assign_poster_settings
@mood_hash
end
def assign_poster_settings
@mood_hash[:icon_emoji], @mood_hash[:username] =
case @mood
when "panda"
[":panda_face:", "Dependapanda"]
when "informative"
[":#{@season_symbol}informative_seal:", "#{@season_name}Informative Seal"]
when "approval"
[":#{@season_symbol}seal_of_approval:", "#{@season_name}Seal of Approval"]
when "angry"
[":#{@season_symbol}angrier_seal:", "#{@season_name}Angry Seal"]
when "robot_face"
[":robot_face:", "Angry CI Robot"]
when "gem"
[":gem:", "Gem Release Robot"]
when "tea"
[":manatea:", "Tea Seal"]
when "charter"
[":happyseal:", "Team Charter Seal"]
when "fun-workstream"
[":wholesome-seal:", "It's ok Seal"]
when "govuk-green-team"
[":happybabyseal:", "Elephant Seal"]
else
raise "Bad mood: #{mood}."
end
end
def check_season
@season_name = case [today.day, today.month]
in [23..31, 10]
"Halloween "
in [1, 1] | [1..31, 12]
"Festive Season "
else
""
end
@season_symbol = snake_case(@season_name)
end
def snake_case(string)
string.downcase.gsub(" ", "_")
end
def set_mood_from_team
@mood = case @team_channel
when "#club-tea"
"tea"
when "#govuk", "#gds-community", "#sealtesting"
"fun-workstream"
when "#govuk-green-team"
"govuk-green-team"
else
@mood.nil? ? "charter" : @mood
end
end
def channel
@team_channel = "#bot-testing" if ENV["DEVELOPMENT"]
end
end