Commit e7d0739dae3596257e3987276481e81eca0c401e
1 parent
b6d7d465
Exists in
master
and in
1 other branch
adding in formatted attachments for slack
Showing
2 changed files
with
110 additions
and
13 deletions
Show diff stats
app/models/notification_services/slack_service.rb
... | ... | @@ -18,7 +18,47 @@ class NotificationServices::SlackService < NotificationService |
18 | 18 | end |
19 | 19 | |
20 | 20 | def post_payload(problem) |
21 | - {:text => message_for_slack(problem) }.to_json | |
21 | + { | |
22 | + :attachments => [ | |
23 | + { | |
24 | + :fallback => message_for_slack(problem), | |
25 | + :pretext => "<#{problem_url(problem)}|Errbit - #{problem.app.name}: #{problem.error_class}>", | |
26 | + :color => "#D00000", | |
27 | + :fields => [ | |
28 | + { | |
29 | + :title => "Environment", | |
30 | + :value => problem.environment, | |
31 | + :short => false | |
32 | + }, | |
33 | + { | |
34 | + :title => "Location", | |
35 | + :value => problem.where, | |
36 | + :short => false | |
37 | + }, | |
38 | + { | |
39 | + :title => "Message", | |
40 | + :value => problem.message.to_s, | |
41 | + :short => false | |
42 | + }, | |
43 | + { | |
44 | + :title => "First Noticed", | |
45 | + :value => problem.first_notice_at, | |
46 | + :short => false | |
47 | + }, | |
48 | + { | |
49 | + :title => "Last Noticed", | |
50 | + :value => problem.last_notice_at, | |
51 | + :short => false | |
52 | + }, | |
53 | + { | |
54 | + :title => "Times Occurred", | |
55 | + :value => problem.notices_count, | |
56 | + :short => false | |
57 | + } | |
58 | + ] | |
59 | + } | |
60 | + ] | |
61 | + }.to_json | |
22 | 62 | end |
23 | 63 | |
24 | 64 | def create_notification(problem) | ... | ... |
spec/models/notification_service/slack_service_spec.rb
1 | -describe NotificationService::SlackService, type: 'model' do | |
2 | - it "it should send a notification to Slack with hook url" do | |
3 | - # setup | |
4 | - notice = Fabricate :notice | |
5 | - notification_service = Fabricate :slack_notification_service, :app => notice.app, :service_url => "https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX" | |
6 | - problem = notice.problem | |
7 | - | |
8 | - # faraday stubbing | |
9 | - payload = {:text => notification_service.message_for_slack(problem)}.to_json | |
10 | - expect(HTTParty).to receive(:post).with(notification_service.service_url, :body => payload, :headers => {"Content-Type" => "application/json"}).and_return(true) | |
11 | - | |
12 | - notification_service.create_notification(problem) | |
1 | +class NotificationServices::SlackService < NotificationService | |
2 | + Label = "slack" | |
3 | + Fields += [ | |
4 | + [:service_url, { | |
5 | + :placeholder => 'Slack Hook URL (https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX)', | |
6 | + :label => 'Hook URL' | |
7 | + }] | |
8 | + ] | |
9 | + | |
10 | + def check_params | |
11 | + if Fields.detect {|f| self[f[0]].blank? } | |
12 | + errors.add :base, "You must specify your Slack Hook url." | |
13 | + end | |
14 | + end | |
15 | + | |
16 | + def message_for_slack(problem) | |
17 | + "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}" | |
18 | + end | |
19 | + | |
20 | + def post_payload(problem) | |
21 | + { | |
22 | + :attachments => [ | |
23 | + { | |
24 | + :fallback => message_for_slack(problem), | |
25 | + :pretext => "<#{problem_url(problem)}|Errbit - #{problem.app.name}: #{problem.error_class}>", | |
26 | + :color => "#D00000", | |
27 | + :fields => [ | |
28 | + { | |
29 | + :title => "Environment", | |
30 | + :value => problem.environment, | |
31 | + :short => false | |
32 | + }, | |
33 | + { | |
34 | + :title => "Location", | |
35 | + :value => problem.where, | |
36 | + :short => false | |
37 | + }, | |
38 | + { | |
39 | + :title => "Message", | |
40 | + :value => problem.message.to_s, | |
41 | + :short => false | |
42 | + }, | |
43 | + { | |
44 | + :title => "First Noticed", | |
45 | + :value => problem.first_notice_at, | |
46 | + :short => false | |
47 | + }, | |
48 | + { | |
49 | + :title => "Last Noticed", | |
50 | + :value => problem.last_notice_at, | |
51 | + :short => false | |
52 | + }, | |
53 | + { | |
54 | + :title => "Times Occurred", | |
55 | + :value => problem.notices_count, | |
56 | + :short => false | |
57 | + } | |
58 | + ] | |
59 | + } | |
60 | + ] | |
61 | + }.to_json | |
62 | + end | |
63 | + | |
64 | + def create_notification(problem) | |
65 | + HTTParty.post(service_url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' }) | |
66 | + end | |
67 | + | |
68 | + def configured? | |
69 | + service_url.present? | |
13 | 70 | end |
14 | 71 | end | ... | ... |