diff options
Diffstat (limited to 'webhook.go')
| -rw-r--r-- | webhook.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/webhook.go b/webhook.go new file mode 100644 index 0000000..897a249 --- /dev/null +++ b/webhook.go @@ -0,0 +1,52 @@ +package checkup + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +func SendDiscordAlert(hookURL string, message string) error { + return SendWebhook(hookURL, + struct { + Content string `json:"content"` + }{ + Content: message, + }, http.StatusNoContent) + +} + +func SendRocketChatAlert(hookURL string, message string) error { + return SendWebhook(hookURL, + struct { + Text string `json:"text"` + }{ + Text: message, + }, http.StatusOK) +} + +func SendWebhook(hookURL string, msg interface{}, code int) error { + buf := &bytes.Buffer{} + enc := json.NewEncoder(buf) + + err := enc.Encode(msg) + if err != nil { + return err + } + + resp, err := http.Post(hookURL, "application/json", buf) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != code { + bod, err := ioutil.ReadAll(resp.Body) + return fmt.Errorf("Bad status code: %d, expected %d : %s (Read errs: %s)", + resp.StatusCode, http.StatusOK, bod, err) + } + + return nil +} |
