diff --git a/src/slack.coffee b/src/slack.coffee index d9f218a8..f62f6f92 100644 --- a/src/slack.coffee +++ b/src/slack.coffee @@ -125,13 +125,41 @@ class SlackBot extends Adapter @receive new TextMessage user, txt, msg.ts removeFormatting: (txt) -> - txt.replace /\<(@\w+)(?:\|([^>]+))?\>/g, (m, link, label) => + # https://api.slack.com/docs/formatting + txt = txt.replace /// + < # opening angle bracket + ([\@\#\!]) # link type + (\w+) # id + (?:\|([^>]+))? # |label (optional) + > # closing angle bracket + ///g, (m, type, id, label) => + if label then return label + + switch type + when '@' + user = @client.getUserByID id + if user + return "@#{user.name}" + when '#' + channel = @client.getChannelByID id + if channel + return "\##{channel.name}" + when '!' + if id in ['channel','group','everyone'] + return "@#{id}" + "#{type}#{id}" + + txt = txt.replace /// + < # opening angle bracket + ([^>\|]+) # link + (?:\|([^>]+))? # label + > # closing angle bracket + ///g, (m, link, label) => if label - return label - u = @client.getUserByID link[1..] - if u - return "@#{u.name}" - link + "#{label} #{link}" + else + link + txt send: (envelope, messages...) -> channel = @client.getChannelGroupOrDMByName envelope.room diff --git a/test/slack.coffee b/test/slack.coffee index ec87f38a..ad4c51fa 100644 --- a/test/slack.coffee +++ b/test/slack.coffee @@ -14,10 +14,13 @@ beforeEach -> stubs = # Slack client channel: + name: 'general' send: (msg) -> msg client: getUserByID: (id) -> {name: 'name', email_address: 'email@example.com'} + getChannelByID: (id) -> + stubs.channel getChannelGroupOrDMByName: () -> stubs.channel # Hubot.Robot instance @@ -50,7 +53,6 @@ describe 'Login', -> slackbot.robot.name.should.equal 'bot' describe 'Removing message formatting', -> - it 'Should do nothing if there are no user links', -> foo = slackbot.removeFormatting 'foo' foo.should.equal 'foo' @@ -63,9 +65,45 @@ describe 'Removing message formatting', -> foo = slackbot.removeFormatting 'foo <@U123|label> bar' foo.should.equal 'foo label bar' + it 'Should change <#C1234> links to #general', -> + foo = slackbot.removeFormatting 'foo <#C123> bar' + foo.should.equal 'foo #general bar' + + it 'Should change <#C1234|label> links to label', -> + foo = slackbot.removeFormatting 'foo <#C123|label> bar' + foo.should.equal 'foo label bar' + + it 'Should change links to @everyone', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo @everyone bar' + + it 'Should change links to @channel', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo @channel bar' + + it 'Should change links to @group', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo @group bar' + + it 'Should remove formatting around links', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo http://www.example.com bar' + + it 'Should remove formatting around links', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo https://www.example.com bar' + + it 'Should remove formatting around links', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo mailto:name@example.com bar' + + it 'Should remove formatting around links with a label', -> + foo = slackbot.removeFormatting 'foo bar' + foo.should.equal 'foo label https://www.example.com bar' + it 'Should change multiple links at once', -> - foo = slackbot.removeFormatting 'foo <@U123|label> bar <@U123>' - foo.should.equal 'foo label bar @name' + foo = slackbot.removeFormatting 'foo <@U123|label> bar <#C123> ' + foo.should.equal 'foo label bar #general @channel label https://www.example.com' describe 'Send Messages', -> it 'Should send multiple messages', ->