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.
watchtower/pkg/notifications/slack.go

81 lines
2.0 KiB
Go

package notifications
import (
"strings"
shoutrrrDisco "github.com/containrrr/shoutrrr/pkg/services/discord"
shoutrrrSlack "github.com/containrrr/shoutrrr/pkg/services/slack"
t "github.com/containrrr/watchtower/pkg/types"
"github.com/johntdyer/slackrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
const (
slackType = "slack"
)
type slackTypeNotifier struct {
slackrus.SlackrusHook
}
func newSlackNotifier() t.ConvertibleNotifier {
hookURL := viper.GetString("notification-slack-hook-url")
userName := viper.GetString("notification-slack-identifier")
channel := viper.GetString("notification-slack-channel")
emoji := viper.GetString("notification-slack-icon-emoji")
iconURL := viper.GetString("notification-slack-icon-url")
n := &slackTypeNotifier{
SlackrusHook: slackrus.SlackrusHook{
HookURL: hookURL,
Username: userName,
Channel: channel,
IconEmoji: emoji,
IconURL: iconURL,
AcceptedLevels: []log.Level{},
},
}
return n
}
func (s *slackTypeNotifier) GetURL(title string) (string, error) {
trimmedURL := strings.TrimRight(s.HookURL, "/")
trimmedURL = strings.TrimLeft(trimmedURL, "https://")
parts := strings.Split(trimmedURL, "/")
if parts[0] == "discord.com" || parts[0] == "discordapp.com" {
log.Debug("Detected a discord slack wrapper URL, using shoutrrr discord service")
conf := &shoutrrrDisco.Config{
WebhookID: parts[len(parts)-3],
Token: parts[len(parts)-2],
Color: ColorInt,
Title: title,
SplitLines: true,
Username: s.Username,
}
return conf.GetURL().String(), nil
}
webhookToken := strings.Replace(s.HookURL, "https://hooks.slack.com/services/", "", 1)
conf := &shoutrrrSlack.Config{
BotName: s.Username,
Color: ColorHex,
Channel: "webhook",
Title: title,
}
if s.IconURL != "" {
conf.Icon = s.IconURL
} else if s.IconEmoji != "" {
conf.Icon = s.IconEmoji
}
if err := conf.Token.SetFromProp(webhookToken); err != nil {
return "", err
}
return conf.GetURL().String(), nil
}