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 }