-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack.go
55 lines (44 loc) · 1.34 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
package slack
import (
"bytes"
"encoding/json"
"log"
"net/http"
)
type slackMessage struct {
Username string `json:"username"`
Text string `json:"text"`
IconUrl string `json:"icon_url"`
Attachments []SlackAttachment `json:"attachments"`
}
type SlackAttachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Pretext string `json:"pretext"`
AuthorName string `json:"author_name"`
AuthorLink string `json:"author_link"`
AuthorIcon string `json:"author_icon"`
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
MarkdownIn string `json:"mrkdwn_in"`
Fields []SlackAttachmentField `json:"fields"`
}
type SlackAttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
func Send(username, webhookIncoming, text, iconUrl string, attachments ...SlackAttachment) error {
message := slackMessage{Username: username, Text: text, IconUrl: iconUrl, Attachments: attachments}
return send(webhookIncoming, &message)
}
func send(webhookIncoming string, message *slackMessage) error {
jsonMessage, err := json.Marshal(message)
if err != nil {
return err
}
resp, err := http.Post(webhookIncoming, "application/json", bytes.NewBuffer(jsonMessage))
log.Println(resp)
return err
}