This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
slack.go
58 lines (45 loc) · 1.6 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"
"net/http"
)
//SlackAttachment represents Slack Attachment structure for Slack API
type SlackAttachment struct {
// Fallback is our simple fallback text equivalent.
Fallback string `json:"fallback"`
// Color can be 'good', 'warning', 'danger', or any hex color code.
Color string `json:"color,omitempty"`
// Pretext is our text above the attachment section.
Pretext string `json:"pretext,omitempty"`
// Title is the notification title.
Title string `json:"title,omitempty"`
// TitleLink is the url link attached to the title.
TitleLink string `json:"title_link,omitempty"`
// Text is the main text in the attachment.
Text string `json:"text"`
// Footer is a brief text to help contextualize and identify an attachment.
// Limited to 300 characters, and may be truncated further when displayed
// to users in environments with limited screen real estate.
Footer string `json:"footer,omitempty"`
// Timestamp is an integer Unix timestamp that is used to related your attachment to
// a specific time. The attachment will display the additional timestamp value as part
// of the attachment's footer.
Timestamp int64 `json:"ts,omitempty"`
}
//SlackMessage represents Slack message structure for Slack API
type SlackMessage struct {
Attachments []SlackAttachment `json:"attachments"`
}
//Notify post the message via Slack API
func (sn *SlackMessage) Notify(slackURL string) error {
bp, err := json.Marshal(sn)
if err != nil {
return err
}
_, err = http.Post(slackURL, "application/json", bytes.NewBuffer(bp))
if err != nil {
return err
}
return nil
}