forked from badkaktus/gorocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
63 lines (47 loc) · 1.15 KB
/
hooks.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
package gorocket
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
type HookMessage struct {
Text string `json:"text"`
Attachments []HookAttachment `json:"attachments"`
}
type HookAttachment struct {
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
ImageURL string `json:"image_url"`
Color string `json:"color"`
}
type HookResponse struct {
Success bool `json:"success"`
}
func (c *Client) Hooks(msg *HookMessage, token string) (*HookResponse, error) {
opt, _ := json.Marshal(msg)
url := fmt.Sprintf("%s/hooks/%s", c.baseURL, token)
req, err := http.NewRequest("POST",
url,
bytes.NewBuffer(opt))
fmt.Println(url)
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if err != nil {
log.Println("Request error")
return nil, err
}
res, err := c.HTTPClient.Do(req)
defer res.Body.Close()
if err != nil {
log.Println(err)
return nil, err
}
resp := HookResponse{}
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return nil, err
}
return &resp, nil
}