-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathslack.go
86 lines (71 loc) · 1.84 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"strings"
"github.com/rs/zerolog"
"github.com/slack-go/slack"
)
type SlackReporter struct {
ChainInfoConfig ChainInfoConfig
SlackConfig SlackConfig
Params *Params
Logger zerolog.Logger
SlackClient slack.Client
}
func NewSlackReporter(
chainInfoConfig ChainInfoConfig,
slackConfig SlackConfig,
params *Params,
logger *zerolog.Logger,
) *SlackReporter {
return &SlackReporter{
ChainInfoConfig: chainInfoConfig,
SlackConfig: slackConfig,
Params: params,
Logger: logger.With().Str("component", "slack_reporter").Logger(),
}
}
func (r SlackReporter) Serialize(report Report) string {
var sb strings.Builder
for _, entry := range report.Entries {
var (
validatorLink string
timeToJail = ""
)
if entry.Direction == INCREASING {
timeToJail = fmt.Sprintf(" (%s till jail)", entry.GetTimeToJail(r.Params))
}
validatorLink = r.ChainInfoConfig.GetValidatorPage(entry.ValidatorAddress, entry.ValidatorMoniker)
sb.WriteString(fmt.Sprintf(
"%s <strong>%s %s</strong>%s\n",
entry.Emoji,
validatorLink,
entry.Description,
timeToJail,
))
}
return sb.String()
}
func (r *SlackReporter) Init() {
if r.SlackConfig.Token == "" || r.SlackConfig.Chat == "" {
r.Logger.Debug().Msg("Slack credentials not set, not creating Slack reporter.")
return
}
client := slack.New(r.SlackConfig.Token)
r.SlackClient = *client
}
func (r SlackReporter) Enabled() bool {
return r.SlackConfig.Token != "" && r.SlackConfig.Chat != ""
}
func (r SlackReporter) SendReport(report Report) error {
serializedReport := r.Serialize(report)
_, _, err := r.SlackClient.PostMessage(
r.SlackConfig.Chat,
slack.MsgOptionText(serializedReport, false),
slack.MsgOptionDisableLinkUnfurl(),
)
return err
}
func (r SlackReporter) Name() string {
return "SlackReporter"
}