-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack.go
58 lines (47 loc) · 1.11 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
50
51
52
53
54
55
56
57
58
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type SlackMessage struct {
Blocks []interface{} `json:"blocks"`
}
type SlackSection struct {
Type string `json:"type"`
Text SlackBlock `json:"text"`
}
type SlackBlock struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Elements []SlackBlock `json:"elements,omitempty"`
}
func sendToSlack(message SlackMessage) error {
if slackWebHook == "" {
log.Println("No slack webhook set. Skipping alert")
return nil
}
// Send our webhook to slack
buf, err := json.Marshal(message)
if err != nil {
return err
}
resp, err := http.Post(slackWebHook, "application/json", bytes.NewBuffer(buf))
if err != nil {
log.Printf("Error sending message to slack %s", err.Error())
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
return fmt.Errorf("error sending message to slack. status code: %d. resp: %s", resp.StatusCode, bodyString)
}
return nil
}