mirror of
https://github.com/TwiN/gatus.git
synced 2024-12-23 15:18:55 +01:00
32 lines
827 B
Go
32 lines
827 B
Go
package alerting
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/TwinProduction/gatus/client"
|
|
"io/ioutil"
|
|
)
|
|
|
|
type requestBody struct {
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// SendSlackMessage sends a message to the given Slack webhook
|
|
func SendSlackMessage(webhookUrl, service, description string) error {
|
|
body, _ := json.Marshal(requestBody{Text: fmt.Sprintf("*[Gatus]*\n*service:* %s\n*description:* %s", service, description)})
|
|
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
|
|
}
|