-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathslack.go
69 lines (61 loc) · 1.75 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
68
69
package main
import (
"net/http"
"strings"
"encoding/json"
)
type SlackPayload struct {
Username string `json:"username"`
IconEmoji string `json:"icon_emoji"`
IconURL string `json:"icon_url"`
Channel string `json:"channel"`
Text string `json:"text"`
Attachments []SlackAttachment `json:"attachments"`
Markdown bool `json:"mrkdwn"`
}
type SlackAttachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Pretext string `json:"pretext"`
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
Fields []SlackAttachmentField `json:"fields"`
MarkdownIn []string `json:"mrkdwn_in"`
}
type SlackAttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
type SlackClient struct {
WebhookURL string
}
func CreateSlackClient(webhookURL string) SlackClient {
return SlackClient{webhookURL}
}
func (c *SlackClient) SendPayload(payload SlackPayload) error {
data, err := json.Marshal(payload)
if err != nil {
return err
}
reader := strings.NewReader(string(data))
_, err = http.Post(c.WebhookURL, "application/json", reader)
if err != nil {
return err
}
// Maybe do something here like check response? For now it's OK
return nil
}
//func (c *SlackClient) SendMessage(message string) error {
// return c.SendPayload(SlackPayload{
// Text: message,
// })
//}
//
//func (c *SlackClient) SendMessageToChannel(channel, message string) error {
// return c.SendPayload(SlackPayload{
// Channel: channel,
// Text: message,
// })
//}