@@ -2,11 +2,14 @@ package slack
22
33import (
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
1215type action struct {
@@ -83,13 +86,13 @@ func (a *action) sendChannelMessage(config bot.Configuration, meta bot.EventData
8386
8487func (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+
101124func (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-
119141func (a * action ) compileMessage () error {
120142 t , err := template .New ("slack-action" ).Parse (a .rule .Message )
121143 if err != nil {
0 commit comments