You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
osem_notify/core/notifier_slack.go

74 lines
1.6 KiB
Go

package core
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/dghubble/sling"
"github.com/spf13/viper"
)
var slackClient = sling.New().Client(&http.Client{})
var notificationColors = map[string]string {
CheckOk: "#00ff00",
CheckErr: "#ff0000",
}
// slack Notifier has no configuration
type SlackNotifier struct {
webhook string
}
type SlackMessage struct {
Text string `json:"text"`
Username string `json:"username,omitempty`
Attachments []SlackAttachment `json:"attachments,omitempty"`
}
type SlackAttachment struct {
Text string `json:"text"`
Color string `json:"color,omitempty"`
}
func (n SlackNotifier) New(config TransportConfig) (AbstractNotifier, error) {
// validate transport configuration
// :TransportConfSourceHack
baseUrl := viper.GetString("slack.webhook")
if baseUrl == "" {
return nil, fmt.Errorf("Missing configuration key slack.webhook")
}
return SlackNotifier{
webhook: baseUrl,
}, nil
}
func (n SlackNotifier) Submit(notification Notification) error {
message := &SlackMessage{
Username: "osem_notify box healthcheck",
Text: notification.Subject,
Attachments: []SlackAttachment{ { notification.Body, notificationColors[notification.Status] } },
}
req, err := slackClient.Post(n.webhook).BodyJSON(message).Request()
if err != nil {
return err
}
c := http.Client{}
res, err2 := c.Do(req)
if err2 != nil {
return err2
}
if res.StatusCode > 200 {
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("slack webhook failed: %s", body)
}
return nil
}