From 8abcab6a8ff6f7b729a886a52d658d411adba026 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Tue, 18 Aug 2020 22:24:00 -0400 Subject: [PATCH] Start working on #2: Slack alerts --- alerting/slack.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 alerting/slack.go diff --git a/alerting/slack.go b/alerting/slack.go new file mode 100644 index 00000000..a83df34f --- /dev/null +++ b/alerting/slack.go @@ -0,0 +1,31 @@ +package alerting + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/TwinProduction/gatus/client" + "io/ioutil" +) + +type requestBody struct { + Text string `json:"text"` +} + +// SendMessage sends a message to the given Slack webhook +func SendMessage(webhookUrl, msg string) error { + body, _ := json.Marshal(requestBody{Text: msg}) + response, err := client.GetHttpClient().Post(webhookUrl, "application/json", bytes.NewBuffer(body)) + if err != nil { + return err + } + defer response.Body.Close() + output, err := ioutil.ReadAll(response.Body) + if err != nil { + return fmt.Errorf("unable to read response body: %v", err.Error()) + } + if string(output) != "ok" { + return fmt.Errorf("error: %s", string(output)) + } + return nil +}