summaryrefslogtreecommitdiffstats
path: root/bridge/slack/slack.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-11-20 22:40:09 +0100
committerWim <wim@42.be>2016-11-20 22:40:09 +0100
commitd5845ce9009741734b9f25153ebe534b698934ef (patch)
tree721f6b48ce36744b3116ccd207ac1abb919026f0 /bridge/slack/slack.go
parent85f2cde4c3d25eca09420ee5cbf9ee530fe9a473 (diff)
downloadmatterbridge-msglm-d5845ce9009741734b9f25153ebe534b698934ef.tar.gz
matterbridge-msglm-d5845ce9009741734b9f25153ebe534b698934ef.tar.bz2
matterbridge-msglm-d5845ce9009741734b9f25153ebe534b698934ef.zip
Replace id-mentions to usernames (slack). Closes #86
Diffstat (limited to 'bridge/slack/slack.go')
-rw-r--r--bridge/slack/slack.go23
1 files changed, 23 insertions, 0 deletions
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
+}