forked from mr-karan/calert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.go
53 lines (46 loc) · 1.19 KB
/
notification.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
package main
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
// Notifier represents an instance that pushes out notifications to
// Google Chat Webhook endpoint.
type Notifier struct {
root string
httpClient *http.Client
}
// NewNotifier initialises a new instance of the Notifier.
func NewNotifier(h http.Client) Notifier {
return Notifier{
httpClient: &h,
}
}
// PushNotification pushes out a notification to Google Chat Room.
func (n *Notifier) PushNotification(notif ChatNotification, webHookURL string) error {
out, err := json.Marshal(notif)
if err != nil {
return err
}
// prepare POST request to webhook endpoint
req, err := http.NewRequest("POST", webHookURL, bytes.NewBuffer(out))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
// send the request
resp, err := n.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// if response is not 200 log error response from gchat
if resp.StatusCode != http.StatusOK {
respMsg, _ := ioutil.ReadAll(resp.Body)
errLog.Printf("Error sending alert Webhook API error: %s", string(respMsg))
return errors.New("Error while sending alert")
}
return nil
}