2
0
mirror of https://github.com/42wim/matterbridge synced 2024-11-15 06:12:55 +00:00

Replace id-mentions to usernames (slack). Closes #86

This commit is contained in:
Wim 2016-11-20 22:40:09 +01:00
parent 85f2cde4c3
commit d5845ce900

View File

@ -6,6 +6,7 @@ import (
"github.com/42wim/matterbridge/matterhook" "github.com/42wim/matterbridge/matterhook"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/nlopes/slack" "github.com/nlopes/slack"
"regexp"
"strings" "strings"
"time" "time"
) )
@ -191,11 +192,14 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) {
m.Channel = channel.Name m.Channel = channel.Name
m.Text = ev.Text m.Text = ev.Text
m.Raw = ev m.Raw = ev
m.Text = b.replaceMention(m.Text)
mchan <- m mchan <- m
} }
count++ count++
case *slack.OutgoingErrorEvent: case *slack.OutgoingErrorEvent:
flog.Debugf("%#v", ev.Error()) flog.Debugf("%#v", ev.Error())
case *slack.ChannelJoinedEvent:
b.Users, _ = b.sc.GetUsers()
case *slack.ConnectedEvent: case *slack.ConnectedEvent:
b.channels = ev.Info.Channels b.channels = ev.Info.Channels
b.si = ev.Info b.si = ev.Info
@ -214,7 +218,26 @@ func (b *Bslack) handleMatterHook(mchan chan *MMMessage) {
m := &MMMessage{} m := &MMMessage{}
m.Username = message.UserName m.Username = message.UserName
m.Text = message.Text m.Text = message.Text
m.Text = b.replaceMention(m.Text)
m.Channel = message.ChannelName m.Channel = message.ChannelName
mchan <- m 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
}