From d5845ce9009741734b9f25153ebe534b698934ef Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 20 Nov 2016 22:40:09 +0100 Subject: [PATCH] Replace id-mentions to usernames (slack). Closes #86 --- bridge/slack/slack.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index 159204d0..db8885c2 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -6,6 +6,7 @@ import ( "github.com/42wim/matterbridge/matterhook" log "github.com/Sirupsen/logrus" "github.com/nlopes/slack" + "regexp" "strings" "time" ) @@ -191,11 +192,14 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) { m.Channel = channel.Name m.Text = ev.Text m.Raw = ev + m.Text = b.replaceMention(m.Text) mchan <- m } count++ case *slack.OutgoingErrorEvent: flog.Debugf("%#v", ev.Error()) + case *slack.ChannelJoinedEvent: + b.Users, _ = b.sc.GetUsers() case *slack.ConnectedEvent: b.channels = ev.Info.Channels b.si = ev.Info @@ -214,7 +218,26 @@ func (b *Bslack) handleMatterHook(mchan chan *MMMessage) { m := &MMMessage{} m.Username = message.UserName m.Text = message.Text + m.Text = b.replaceMention(m.Text) m.Channel = message.ChannelName mchan <- m } } + +func (b *Bslack) userName(id string) string { + for _, u := range b.Users { + if u.ID == id { + return u.Name + } + } + return "" +} + +func (b *Bslack) replaceMention(text string) string { + results := regexp.MustCompile(`<@([a-zA-z0-9]+)>`).FindAllStringSubmatch(text, -1) + for _, r := range results { + text = strings.Replace(text, "<@"+r[1]+">", "@"+b.userName(r[1]), -1) + + } + return text +}