2018-05-09 00:11:24 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2018-06-15 23:03:28 +02:00
|
|
|
"fmt"
|
2022-09-27 20:21:02 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2018-05-09 00:11:24 +02:00
|
|
|
"strings"
|
2022-09-27 20:21:02 +02:00
|
|
|
"time"
|
2018-05-09 00:11:24 +02:00
|
|
|
|
2019-10-10 05:07:33 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
2018-06-12 00:14:15 +02:00
|
|
|
|
2018-05-09 00:11:24 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-05-04 18:27:35 +02:00
|
|
|
"github.com/slack-go/slack"
|
2018-05-09 00:11:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2022-09-27 21:35:13 +02:00
|
|
|
c := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).DialContext,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ResponseHeaderTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
slackApi = slack.New(token, slack.OptionHTTPClient(c))
|
2018-05-09 00:11:24 +02:00
|
|
|
defaultChannel = channel
|
|
|
|
defaultUsername = username
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendToSlackUser Sends message to a specific user.
|
2018-07-24 02:01:35 +02:00
|
|
|
func SendToSlackUser(user, username, format string, a ...interface{}) error {
|
|
|
|
message := format
|
|
|
|
if len(a) > 0 {
|
|
|
|
message = fmt.Sprintf(format, a...)
|
|
|
|
}
|
2018-05-09 00:11:24 +02:00
|
|
|
if !strings.HasPrefix(user, "@") {
|
|
|
|
user = "@" + user
|
|
|
|
}
|
|
|
|
return sendToSlack(user, username, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendToSlackChannel Sends message to a specific channel.
|
2018-07-24 02:01:35 +02:00
|
|
|
func SendToSlackChannel(channel, username, format string, a ...interface{}) error {
|
|
|
|
message := format
|
|
|
|
if len(a) > 0 {
|
|
|
|
message = fmt.Sprintf(format, a...)
|
|
|
|
}
|
2018-05-09 00:11:24 +02:00
|
|
|
if !strings.HasPrefix(channel, "#") {
|
|
|
|
channel = "#" + channel
|
|
|
|
}
|
|
|
|
return sendToSlack(channel, username, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendToSlack Sends message to the default channel.
|
2018-07-24 02:01:35 +02:00
|
|
|
func SendToSlack(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-07-24 02:01:35 +02:00
|
|
|
if defaultChannel == "" {
|
|
|
|
return errors.Err("no default slack channel set")
|
2018-06-18 01:50:59 +02:00
|
|
|
}
|
2018-07-24 02:01:35 +02:00
|
|
|
|
|
|
|
return sendToSlack(defaultChannel, defaultUsername, message)
|
2018-06-15 23:03:28 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2022-09-27 22:48:51 +02:00
|
|
|
for {
|
|
|
|
_, _, err = slackApi.PostMessage(channel, slack.MsgOptionText(message, false), slack.MsgOptionUsername(username))
|
2022-09-27 22:56:34 +02:00
|
|
|
if err != nil && strings.Contains(err.Error(), "timeout awaiting response headers") {
|
2022-09-27 22:48:51 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-05-09 00:11:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("error sending to slack: " + err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|