-
Notifications
You must be signed in to change notification settings - Fork 167
/
slack_lib.rb
115 lines (98 loc) · 2.95 KB
/
slack_lib.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
111
112
113
114
115
require 'slack/poster'
class SlackLib
SlackResponseError = Class.new(StandardError)
attr_accessor :webhook_url, :poster, :mood, :mood_hash, :channel, :season_name, :halloween_season, :festive_season
def initialize(webhook_url, team_channel, mood)
@webhook_url = webhook_url
@team_channel = team_channel
@mood = mood
@today = Date.today
@postable_day = !today.saturday? && !today.sunday?
mood_hash
channel
create_poster
end
def create_poster
@poster = Slack::Poster.new("#{webhook_url}", slack_options)
end
def send_request(message)
if ENV['DRY']
puts "Will#{' not' unless postable_day} post #{mood} message to #{channel} on #{today.strftime('%A')}"
puts slack_options.inspect
puts message
else
if postable_day
response = poster.send_message(message)
raise SlackResponseError, "Posting to webhook failed with: #{response.status} #{response.reason_phrase} #{response.body}" unless response.success?
end
end
end
private
attr_reader :postable_day, :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
check_if_quotes
assign_poster_settings
end
def assign_poster_settings
if @mood == "informative"
@mood_hash[:icon_emoji]= ":#{@season_symbol}informative_seal:"
@mood_hash[:username]= "#{@season_name}Informative Seal"
elsif @mood == "approval"
@mood_hash[:icon_emoji]= ":#{@season_symbol}seal_of_approval:"
@mood_hash[:username]= "#{@season_name}Seal of Approval"
elsif @mood == "angry"
@mood_hash[:icon_emoji]= ":#{@season_symbol}angrier_seal:"
@mood_hash[:username]= "#{@season_name}Angry Seal"
elsif @mood == "tea"
@mood_hash[:icon_emoji]= ":manatea:"
@mood_hash[:username]= "Tea Seal"
elsif @mood == "charter"
@mood_hash[:icon_emoji]= ":happyseal:"
@mood_hash[:username]= "Team Charter Seal"
else
fail "Bad mood: #{mood}."
end
end
def check_season
if halloween_season?
@season_name = "Halloween "
elsif festive_season?
@season_name = "Festive Season "
else
@season_name = ""
end
@season_symbol = snake_case(@season_name)
end
def halloween_season?
this_year = today.year
today <= Date.new(this_year, 10, 31) && today >= Date.new(this_year,10,23)
end
def festive_season?
this_year = today.year
return true if today <= Date.new(this_year, 12, 31) && today >= Date.new(this_year,12,1)
today == Date.new(this_year, 01, 01)
end
def snake_case(string)
string.downcase.gsub(" ", "_")
end
def check_if_quotes
if @team_channel == "#tea"
@mood = "tea"
elsif @mood == nil
@mood = "charter"
@postable_day = today.tuesday? || today.thursday?
end
end
def channel
@team_channel = '#angry-seal-bot-test' if ENV["DYNO"].nil?
end
end