2017-07-07 22:58:29 +00:00
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
2017-07-08 14:55:39 +00:00
|
|
|
"encoding/json"
|
2017-07-07 22:58:29 +00:00
|
|
|
"os"
|
2017-07-08 14:55:39 +00:00
|
|
|
"strconv"
|
2017-07-07 22:58:29 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nlopes/slack"
|
|
|
|
|
2017-11-01 18:25:28 +00:00
|
|
|
"github.com/keel-hq/keel/constants"
|
|
|
|
"github.com/keel-hq/keel/extension/notification"
|
|
|
|
"github.com/keel-hq/keel/types"
|
2017-07-07 22:58:29 +00:00
|
|
|
|
2018-03-03 11:32:00 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-07-07 22:58:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const timeout = 5 * time.Second
|
|
|
|
|
|
|
|
type sender struct {
|
|
|
|
slackClient *slack.Client
|
|
|
|
channels []string
|
|
|
|
botName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
notification.RegisterSender("slack", &sender{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) Configure(config *notification.Config) (bool, error) {
|
|
|
|
var token string
|
|
|
|
// Get configuration
|
|
|
|
if os.Getenv(constants.EnvSlackToken) != "" {
|
|
|
|
token = os.Getenv(constants.EnvSlackToken)
|
|
|
|
} else {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if os.Getenv(constants.EnvSlackBotName) != "" {
|
|
|
|
s.botName = os.Getenv(constants.EnvSlackBotName)
|
|
|
|
} else {
|
|
|
|
s.botName = "keel"
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv(constants.EnvSlackChannels) != "" {
|
|
|
|
channels := os.Getenv(constants.EnvSlackChannels)
|
|
|
|
s.channels = strings.Split(channels, ",")
|
|
|
|
} else {
|
|
|
|
s.channels = []string{"general"}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.slackClient = slack.New(token)
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
2017-07-08 15:25:35 +00:00
|
|
|
"name": "slack",
|
|
|
|
"channels": s.channels,
|
2017-07-07 22:58:29 +00:00
|
|
|
}).Info("extension.notification.slack: sender configured")
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sender) Send(event types.EventNotification) error {
|
|
|
|
params := slack.NewPostMessageParameters()
|
|
|
|
params.Username = s.botName
|
2018-02-25 18:59:29 +00:00
|
|
|
params.IconURL = constants.KeelLogoURL
|
2017-07-08 14:55:39 +00:00
|
|
|
|
|
|
|
params.Attachments = []slack.Attachment{
|
|
|
|
slack.Attachment{
|
|
|
|
Fallback: event.Message,
|
|
|
|
Color: event.Level.Color(),
|
|
|
|
Fields: []slack.AttachmentField{
|
|
|
|
slack.AttachmentField{
|
|
|
|
Title: event.Type.String(),
|
|
|
|
Value: event.Message,
|
|
|
|
Short: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Footer: "keel.sh",
|
|
|
|
Ts: json.Number(strconv.Itoa(int(event.CreatedAt.Unix()))),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-02-25 18:59:29 +00:00
|
|
|
chans := s.channels
|
|
|
|
if len(event.Channels) > 0 {
|
|
|
|
chans = event.Channels
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range chans {
|
2017-07-08 14:55:39 +00:00
|
|
|
_, _, err := s.slackClient.PostMessage(channel, "", params)
|
2017-07-07 22:58:29 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"error": err,
|
|
|
|
"channel": channel,
|
|
|
|
}).Error("extension.notification.slack: failed to send notification")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|