1
0
mirror of https://github.com/badkaktus/gorocket.git synced 2024-12-12 11:15:05 +02:00
gorocket/hooks.go

61 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-05-05 13:22:50 +02:00
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"`
}
2020-05-05 14:26:28 +02:00
type HookResponse struct {
Success bool `json:"success"`
}
func (c *Client) Hooks(msg *HookMessage, token string) (*HookResponse, error) {
2020-05-05 13:22:50 +02:00
opt, _ := json.Marshal(msg)
2020-05-06 08:43:20 +02:00
url := fmt.Sprintf("%s/hooks/%s", c.baseURL, token)
2020-05-05 13:22:50 +02:00
req, err := http.NewRequest("POST",
2020-05-06 08:43:20 +02:00
url,
2020-05-05 13:22:50 +02:00
bytes.NewBuffer(opt))
2020-05-05 14:26:28 +02:00
2020-05-05 13:22:50 +02:00
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
2020-05-05 13:22:50 +02:00
}
2020-05-05 14:26:28 +02:00
res, err := c.HTTPClient.Do(req)
defer res.Body.Close()
if err != nil {
log.Println(err)
return nil, err
2020-05-05 14:26:28 +02:00
}
resp := HookResponse{}
if err = json.NewDecoder(res.Body).Decode(&resp); err != nil {
return nil, err
}
return &resp, nil
2020-05-05 13:22:50 +02:00
}