-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.go
48 lines (39 loc) · 794 Bytes
/
send.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
package vslack
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func (v *VSlack) sendWithChannel(e chan error) {
if err := v.send(); err != nil {
e <- err
}
e <- nil
}
func (v *VSlack) send() error {
b, err := json.Marshal(v)
if err != nil {
return err
}
req, err := http.NewRequest("POST", v.IncomingWebhookURI, bytes.NewBuffer(b))
if err != nil {
return err
}
client := &http.Client{}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("Failed to send slack message with response code %d", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
}