slack_service.rb
1.81 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class NotificationServices::SlackService < NotificationService
LABEL = "slack"
FIELDS += [
[:service_url, {
placeholder: 'Slack Hook URL (https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX)',
label: 'Hook URL'
}]
]
def check_params
if FIELDS.detect { |f| self[f[0]].blank? }
errors.add :base, "You must specify your Slack Hook url."
end
end
def message_for_slack(problem)
"[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem.url}"
end
def post_payload(problem)
{
username: "Errbit",
icon_url: "https://raw.githubusercontent.com/errbit/errbit/master/docs/notifications/slack/errbit.png",
attachments: [
{
fallback: message_for_slack(problem),
title: problem.message.to_s.truncate(100),
title_link: problem.url,
text: problem.where,
color: "#D00000",
fields: [
{
title: "Application",
value: problem.app.name,
short: true
},
{
title: "Environment",
value: problem.environment,
short: true
},
{
title: "Times Occurred",
value: problem.notices_count,
short: true
},
{
title: "First Noticed",
value: problem.first_notice_at.try(:to_s, :db),
short: true
}
]
}
]
}.to_json
end
def create_notification(problem)
HTTParty.post(
service_url,
body: post_payload(problem),
headers: {
'Content-Type' => 'application/json'
}
)
end
def configured?
service_url.present?
end
end