lbry.go/util/slack.go
Mark Beamer Jr afffa668a9 -Added API Server to lbry.go
-Removed dependency on internal apis
-Moved over only required packages.
-Adjusted slack.go to be generic instead of hard coding channel name.
-Moved over travis package from internal-apis
-Added Repository struct for webhook and an IsMatch method. It is possible for any repository to send a webhook to the api and it will trigger a deploy. We should check against the owner, repo and branch.
-Renamed package to api
-removed util.Debugging from server.go
-Added an ErrorHandling function that be used as interface for slack for internal-apis
-Added Map for Header settings that can be set before the serving
-Merged slack code from lbryio/boardbot
-Cleaned up the slack.go code so it made more sense and flowed better
-Removed gitignore entry for `.idea`, should be global
-Removed debugging.go
-Added option for private vs public repository for getting travis public key.
-separated private vs public into if else.
-Changed HeaderSettings to not be pointer.
-Changed ErrorHandler to be named LogErrorFunc
-removed logrus dependency, created loginfo function to handle non-error information.
-Added Daemon Types and adjusted peer_list to be in line with v20
-Fixed rpcclient library usage for latest version to prevent build errors.
-Changed inputs to LogError and LogInfo so that other implementations can make this customizable.
2018-05-27 11:30:04 -04:00

65 lines
1.5 KiB
Go

package util
import (
"strings"
"github.com/lbryio/lbry.go/errors"
"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)
}
func sendToSlack(channel, username, message string) error {
var err error
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
}