Skip to content

Commit 7dd14bc

Browse files
committed
slack: Option for logging messages.
1 parent c975b4a commit 7dd14bc

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

util/slack/module.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ type Module struct {
2626
Config *config.Module
2727
Logger *logger.Module
2828

29-
client slackClient
30-
config struct {
29+
LogMessages bool
30+
client slackClient
31+
config struct {
3132
SlackConfig struct {
3233
Channel string `json:"channel"`
3334
APIToken string `json:"api_token"`
@@ -56,14 +57,7 @@ type PostMessageParameters = slack.PostMessageParameters
5657

5758
// Post a message to the default channel
5859
func (m *Module) Post(txt string, params *PostMessageParameters) {
59-
var p PostMessageParameters
60-
if params != nil {
61-
p = *params
62-
}
63-
_, _, err := m.client.PostMessage(m.config.SlackConfig.Channel, txt, p)
64-
if err != nil {
65-
m.Logger.Error(errors.Wrap(err))
66-
}
60+
m.PostC(m.config.SlackConfig.Channel, txt, params)
6761
}
6862

6963
// PostC posts a message to the given channel
@@ -72,6 +66,9 @@ func (m *Module) PostC(channel, txt string, params *PostMessageParameters) {
7266
if params != nil {
7367
p = *params
7468
}
69+
if m.LogMessages {
70+
m.Logger.Infof("[%s] %s", m.config.SlackConfig.Channel, txt)
71+
}
7572
_, _, err := m.client.PostMessage(channel, txt, p)
7673
if err != nil {
7774
m.Logger.Error(errors.Wrap(err))
@@ -83,6 +80,6 @@ type dummyClient struct {
8380
}
8481

8582
func (d *dummyClient) PostMessage(channel, text string, params slack.PostMessageParameters) (string, string, error) {
86-
d.logger.Infof("[%s] %s", channel, text)
83+
d.logger.Infof("[%s] %s (not sent)", channel, text)
8784
return "", "", nil
8885
}

0 commit comments

Comments
 (0)