initial slack webhook

master
Sven Speckmaier 2020-01-02 18:58:19 +01:00
rodzic 7b40b1af2d
commit e8d940c078
2 zmienionych plików z 60 dodań i 0 usunięć

Wyświetl plik

@ -69,3 +69,62 @@ func grafanaParserFunc(r *http.Request) (string, error) {
return message, nil
}
/*************
SLACK PARSER
*************/
type SlackMessage struct {
Channel string `json:"channel"`
IconEmoji string `json:"icon_emoji"`
Username string `json:"username"`
Text string `json:"text"`
Attachments []SlackAttachment `json:"attachments"`
}
type SlackAttachment struct {
Color string `json:"color"`
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
}
func nonemptyAppendNewline(message string) (string) {
if len(message) == 0 {
return message
}
return message+"\n"
}
func slackParserFunc(r *http.Request) (string, error) {
// get alert data from request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
// grafana alert struct
alert := SlackMessage{}
// parse body into the alert struct
err = json.Unmarshal(body, &alert)
if err != nil {
return "", err
}
// contruct alert message
message := ""
hasText := (alert.Text != "")
if hasText {
message = alert.Text
}
for _, attachment := range alert.Attachments {
message = nonemptyAppendNewline(message)
message = message + attachment.Title+": "+attachment.TitleLink+"\n"
message = message + attachment.Text
}
return message, nil
}

Wyświetl plik

@ -148,6 +148,7 @@ func main() {
// initialize handler for grafana alerts
http.Handle("/grafana", newMessageHandler(messages, grafanaParserFunc))
http.Handle("/slack", newMessageHandler(messages, slackParserFunc))
// listen for requests
http.ListenAndServe(":4321", nil)