From bea0bbd0c2921343c70d4a0ef78df61ce884cbc2 Mon Sep 17 00:00:00 2001 From: Patrick Connolly Date: Fri, 3 Nov 2017 15:32:28 -0400 Subject: [PATCH] Allow slack messages with variables (eg. @here) to be formatted correctly. (slack) (#288) --- bridge/slack/slack.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bridge/slack/slack.go b/bridge/slack/slack.go index c09d6f90..11a84a12 100644 --- a/bridge/slack/slack.go +++ b/bridge/slack/slack.go @@ -340,6 +340,7 @@ func (b *Bslack) handleSlackClient(mchan chan *MMMessage) { } m.Raw = ev m.Text = b.replaceMention(m.Text) + m.Text = b.replaceVariable(m.Text) m.Text = b.replaceChannel(m.Text) // when using webhookURL we can't check if it's our webhook or not for now if ev.BotID != "" && b.Config.WebhookURL == "" { @@ -387,6 +388,7 @@ func (b *Bslack) handleMatterHook(mchan chan *MMMessage) { m.Username = message.UserName m.Text = message.Text m.Text = b.replaceMention(m.Text) + m.Text = b.replaceVariable(m.Text) m.Text = b.replaceChannel(m.Text) m.Channel = message.ChannelName if m.Username == "slackbot" { @@ -408,6 +410,7 @@ func (b *Bslack) userName(id string) string { return "" } +// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users func (b *Bslack) replaceMention(text string) string { results := regexp.MustCompile(`<@([a-zA-z0-9]+)>`).FindAllStringSubmatch(text, -1) for _, r := range results { @@ -416,6 +419,7 @@ func (b *Bslack) replaceMention(text string) string { return text } +// @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users func (b *Bslack) replaceChannel(text string) string { results := regexp.MustCompile(`<#[a-zA-Z0-9]+\|(.+?)>`).FindAllStringSubmatch(text, -1) for _, r := range results { @@ -424,6 +428,16 @@ func (b *Bslack) replaceChannel(text string) string { return text } +// @see https://api.slack.com/docs/message-formatting#variables +func (b *Bslack) replaceVariable(text string) string { + results := regexp.MustCompile(``).FindAllStringSubmatch(text, -1) + for _, r := range results { + text = strings.Replace(text, r[0], "@"+r[1], -1) + } + return text +} + +// @see https://api.slack.com/docs/message-formatting#linking_to_urls func (b *Bslack) replaceURL(text string) string { results := regexp.MustCompile(`<(.*?)(\|.*?)?>`).FindAllStringSubmatch(text, -1) for _, r := range results {