Skip to content

Commit 8fd17ac

Browse files
committed
slack user name to user if translation
1 parent 0b21918 commit 8fd17ac

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

bot/actions/slack/slack.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package slack
22

33
import (
44
"fmt"
5+
"strings"
6+
"text/template"
7+
58
"github.com/bivas/rivi/bot"
9+
"github.com/bivas/rivi/util"
610
"github.com/bivas/rivi/util/log"
711
"github.com/mitchellh/mapstructure"
812
api "github.com/nlopes/slack"
9-
"text/template"
1013
)
1114

1215
type action struct {
@@ -83,13 +86,13 @@ func (a *action) sendChannelMessage(config bot.Configuration, meta bot.EventData
8386

8487
func (a *action) sendPrivateMessage(config bot.Configuration, meta bot.EventData) {
8588
targets := a.getMessageRecipients(config, meta)
86-
for _, slacker := range targets {
89+
for _, slacker := range a.toSlackUserId(targets, meta) {
8790
_, _, id, err := a.client.OpenIMChannel(slacker)
8891
if err != nil {
8992
a.logger.WarningWith(log.MetaFields{
9093
log.F("issue", meta.GetShortName()),
9194
log.E(err),
92-
log.F("user", slacker)}, "Unable to open IM channel")
95+
log.F("user.id", slacker)}, "Unable to open IM channel")
9396
continue
9497
}
9598
if err := a.postMessage(id, targets, config, meta); err != nil {
@@ -98,6 +101,26 @@ func (a *action) sendPrivateMessage(config bot.Configuration, meta bot.EventData
98101
}
99102
}
100103

104+
func (a *action) toSlackUserId(users []string, meta bot.EventData) []string {
105+
result := make([]string, 0)
106+
slackers, err := a.client.GetUsers()
107+
if err != nil {
108+
a.logger.WarningWith(log.MetaFields{
109+
log.F("issue", meta.GetShortName()),
110+
log.E(err)}, "Unable to get users")
111+
return result
112+
}
113+
userSet := util.StringSet{Transformer: strings.ToLower}
114+
userSet.AddAll(users)
115+
for _, slacker := range slackers {
116+
search := strings.ToLower(slacker.Name)
117+
if userSet.Contains(search) {
118+
result = append(result, slacker.ID)
119+
}
120+
}
121+
return result
122+
}
123+
101124
func (a *action) getMessageRecipients(config bot.Configuration, meta bot.EventData) []string {
102125
var result []string
103126
if a.rule.Notify == "assignees" {
@@ -115,7 +138,6 @@ func (a *action) getMessageRecipients(config bot.Configuration, meta bot.EventDa
115138
return result
116139
}
117140

118-
119141
func (a *action) compileMessage() error {
120142
t, err := template.New("slack-action").Parse(a.rule.Message)
121143
if err != nil {

log.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import (
77
)
88

99
func isDebug() bool {
10-
if len(os.Getenv("BOT_DEBUG")) > 0 {
11-
return true
12-
}
13-
return false
10+
return len(os.Getenv("BOT_DEBUG")) > 0
1411
}
1512

1613
func logSetup() {

0 commit comments

Comments
 (0)