-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
67 lines (65 loc) · 1.46 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
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func notifySlack(config *slackConfig, zoneName string, serial uint32, diff string) error {
if config.WebhookURL == "" {
return nil
}
data := map[string]interface{}{
"channel": config.Channel,
"username": config.Name,
"icon_emoji": config.IcomEmoji,
"icon_url": config.IconURL,
"attachments": []interface{}{
map[string]interface{}{
"fallback": fmt.Sprintf("%s is updated.", zoneName),
"color": "#36a64f",
"title": "DNS zone update notification",
"fields": []interface{}{
map[string]interface{}{
"title": "Zone",
"value": zoneName,
"short": true,
},
map[string]interface{}{
"title": "Serial",
"value": fmt.Sprintf("%d", serial),
"short": true,
},
map[string]interface{}{
"title": "Timestamp",
"value": time.Now().Format(time.RFC3339),
"short": false,
},
map[string]interface{}{
"title": "Diff",
"value": diff,
"short": false,
},
},
},
},
}
input, err := json.Marshal(data)
if err != nil {
return err
}
resp, err := http.Post(config.WebhookURL, "application/json", bytes.NewBuffer(input))
if err != nil {
return err
}
if resp.StatusCode != 200 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("invaild statuscode: %d", resp.StatusCode)
}
return fmt.Errorf("received error: %s", b)
}
return nil
}