2018-05-09 00:11:24 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2018-06-15 23:03:28 +02:00
|
|
|
"fmt"
|
2018-05-09 00:11:24 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-06-12 00:14:15 +02:00
|
|
|
|
2018-05-09 00:11:24 +02:00
|
|
|
"github.com/nlopes/slack"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaultChannel string
|
|
|
|
var defaultUsername string
|
|
|
|
var slackApi *slack.Client
|
|
|
|
|
|
|
|
// InitSlack Initializes a slack client with the given token and sets the default channel.
|
|
|
|
func InitSlack(token string, channel string, username string) {
|
|
|
|
slackApi = slack.New(token)
|
|
|
|
defaultChannel = channel
|
|
|
|
defaultUsername = username
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendToSlackUser Sends message to a specific user.
|
|
|
|
func SendToSlackUser(user, username, message string) error {
|
|
|
|
if !strings.HasPrefix(user, "@") {
|
|
|
|
user = "@" + user
|
|
|
|
}
|
|
|
|
return sendToSlack(user, username, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendToSlackChannel Sends message to a specific channel.
|
|
|
|
func SendToSlackChannel(channel, username, message string) error {
|
|
|
|
if !strings.HasPrefix(channel, "#") {
|
|
|
|
channel = "#" + channel
|
|
|
|
}
|
|
|
|
return sendToSlack(channel, username, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendToSlack Sends message to the default channel.
|
|
|
|
func SendToSlack(message string) error {
|
|
|
|
|
|
|
|
if defaultChannel == "" {
|
|
|
|
return errors.Err("no default slack channel set")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendToSlack(defaultChannel, defaultUsername, message)
|
|
|
|
}
|
|
|
|
|
2018-06-15 23:03:28 +02:00
|
|
|
// SendErrorToSlack Sends an error message to the default channel and to the process log.
|
|
|
|
func SendErrorToSlack(format string, a ...interface{}) error {
|
2018-06-18 01:50:59 +02:00
|
|
|
message := format
|
|
|
|
if len(a) > 0 {
|
|
|
|
message = fmt.Sprintf(format, a...)
|
|
|
|
}
|
2018-06-15 23:03:28 +02:00
|
|
|
log.Errorln(message)
|
|
|
|
return sendToSlack(defaultChannel, defaultUsername, ":sos: "+message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendInfoToSlack Sends an info message to the default channel and to the process log.
|
|
|
|
func SendInfoToSlack(format string, a ...interface{}) error {
|
2018-06-18 01:50:59 +02:00
|
|
|
message := format
|
|
|
|
if len(a) > 0 {
|
|
|
|
message = fmt.Sprintf(format, a...)
|
|
|
|
}
|
2018-06-15 23:03:28 +02:00
|
|
|
log.Debugln(message)
|
|
|
|
return sendToSlack(defaultChannel, defaultUsername, ":information_source: "+message)
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:11:24 +02:00
|
|
|
func sendToSlack(channel, username, message string) error {
|
|
|
|
var err error
|
2018-06-12 00:14:15 +02:00
|
|
|
|
2018-05-09 00:11:24 +02:00
|
|
|
if slackApi == nil {
|
|
|
|
err = errors.Err("no slack token provided")
|
|
|
|
} else {
|
|
|
|
log.Debugln("slack: " + channel + ": " + message)
|
|
|
|
_, _, err = slackApi.PostMessage(channel, message, slack.PostMessageParameters{Username: username})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("error sending to slack: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|