-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack-client.go
60 lines (49 loc) · 1.81 KB
/
slack-client.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type Attachment struct {
Color string `json:"color,omitempty"`
Fallback string `json:"fallback,omitempty"`
CallbackID string `json:"callback_id,omitempty"`
ID int `json:"id,omitempty"`
AuthorID string `json:"author_id,omitempty"`
AuthorName string `json:"author_name,omitempty"`
AuthorSubname string `json:"author_subname,omitempty"`
AuthorLink string `json:"author_link,omitempty"`
AuthorIcon string `json:"author_icon,omitempty"`
Title string `json:"title,omitempty"`
TitleLink string `json:"title_link,omitempty"`
Pretext string `json:"pretext,omitempty"`
Text string `json:"text,omitempty"`
ImageURL string `json:"image_url,omitempty"`
ThumbURL string `json:"thumb_url,omitempty"`
MarkdownIn []string `json:"mrkdwn_in,omitempty"`
Ts json.Number `json:"ts,omitempty"`
}
type SlackPayload struct {
Username string `json:"username,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
var slackHttpClient http.Client
func init() {
slackHttpClient = *http.DefaultClient
}
func SendSlack(webhook string, payload *SlackPayload) error {
if len(webhook) == 0 {
return fmt.Errorf("no webhook url was provided")
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", webhook, bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
resp, _ := slackHttpClient.Do(req)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("could not send slack notification, service returned %d", resp.StatusCode)
}
return nil
}