forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
49 lines (43 loc) · 1.15 KB
/
slack.go
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
package checkup
import (
"fmt"
"log"
"strings"
slack "github.com/ashwanthkumar/slack-go-webhook"
)
// Slack consist of all the sub components required to use Slack API
type Slack struct {
Name string `json:"name"`
Username string `json:"username"`
Channel string `json:"channel"`
Webhook string `json:"webhook"`
}
// Notify implements notifier interface
func (s Slack) Notify(results []Result) error {
for _, result := range results {
if !result.Healthy {
s.Send(result)
}
}
return nil
}
// Send request via Slack API to create incident
func (s Slack) Send(result Result) error {
color := "danger"
attach := slack.Attachment{}
attach.AddField(slack.Field{Title: result.Title, Value: result.Endpoint})
attach.AddField(slack.Field{Title: "Status", Value: strings.ToUpper(fmt.Sprint(result.Status()))})
attach.Color = &color
payload := slack.Payload{
Text: result.Title,
Username: s.Username,
Channel: s.Channel,
Attachments: []slack.Attachment{attach},
}
err := slack.Send(s.Webhook, "", payload)
if len(err) > 0 {
log.Printf("ERROR: %s", err)
}
log.Printf("Create request for %s", result.Endpoint)
return nil
}