-
Notifications
You must be signed in to change notification settings - Fork 36
/
slack.go
59 lines (47 loc) · 1.08 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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)
func sendSlackMessage(message string) error {
slackUrl := "https://slack.com/api/chat.postMessage"
content := map[string]interface{}{
"channel": config.Slack["channel"],
"text": fmt.Sprintf("```%v```", message),
}
jsonContent, err := json.Marshal(content)
if err != nil {
return err
}
request, err := http.NewRequest("POST", slackUrl, bytes.NewReader(jsonContent))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", config.Slack["bottoken"]))
resp, err := config.httpClient.Do(request)
if err != nil {
return err
}
if resp.Body == nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
responseBody := make(map[string]interface{})
err = json.Unmarshal(body, &responseBody)
if err != nil {
return err
}
if !responseBody["ok"].(bool) {
return errors.New(responseBody["error"].(string))
}
return nil
}