-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhango.go
More file actions
71 lines (58 loc) · 1.35 KB
/
hango.go
File metadata and controls
71 lines (58 loc) · 1.35 KB
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
64
65
66
67
68
69
70
71
package hango
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
// Client is Webhook client for Hangout chat.
type Client struct {
httpClient *http.Client
webhook string
}
// PostData represents post data for Google chat.
type PostData struct {
Text string `json:"text"`
Thread *Thread `json:"thread"`
}
// Thread represents thread.
type Thread struct {
Name string `json:"name"`
}
// NewClient create new Webhook client for Hangout chat.
func NewClient(webhook string) *Client {
c := &Client{
httpClient: &http.Client{},
webhook: webhook,
}
return c
}
// Post message with a new thread.
func (c *Client) Post(message string) ([]byte, error) {
return c.post(message, "")
}
// PostToThread post massage to an existing thread.
func (c *Client) PostToThread(message, thread string) ([]byte, error) {
return c.post(message, thread)
}
func (c *Client) post(message, thread string) ([]byte, error) {
// Create post data
j := PostData{message, &Thread{thread}}
jsonStr, _ := json.Marshal(j)
req, err := http.NewRequest(
http.MethodPost,
c.webhook,
bytes.NewBuffer([]byte(jsonStr)),
)
if err != nil {
return nil, err
}
// Set Content-Type.
req.Header.Set("Content-Type", "application/json")
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}