Skip to content

Commit

Permalink
Remove all special slack formatting, not just <@U1234>
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhammond committed Dec 16, 2014
1 parent 86163dc commit 274dc28
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
25 changes: 18 additions & 7 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,24 @@ class SlackBot extends Adapter
@receive new TextMessage user, txt, msg.ts

removeFormatting: (txt) ->
txt.replace /\<(@\w+)(?:\|([^>]+))?\>/g, (m, link, label) =>
if label
return label
u = @client.getUserByID link[1..]
if u
return "@#{u.name}"
link
#https://api.slack.com/docs/formatting
txt.replace /\<([\@\#\!])(\w+)(?:\|([^>]+))?\>/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}"

send: (envelope, messages...) ->
channel = @client.getChannelGroupOrDMByName envelope.room
Expand Down
28 changes: 25 additions & 3 deletions test/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ beforeEach ->
stubs =
# Slack client
channel:
name: 'general'
send: (msg) -> msg
client:
getUserByID: (id) ->
{name: 'name', email_address: '[email protected]'}
getChannelByID: (id) ->
stubs.channel
getChannelGroupOrDMByName: () ->
stubs.channel
# Hubot.Robot instance
Expand Down Expand Up @@ -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'
Expand All @@ -63,9 +65,29 @@ 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 <!everyone> links to @everyone', ->
foo = slackbot.removeFormatting 'foo <!everyone> bar'
foo.should.equal 'foo @everyone bar'

it 'Should change <!channel> links to @channel', ->
foo = slackbot.removeFormatting 'foo <!channel> bar'
foo.should.equal 'foo @channel bar'

it 'Should change <!group> links to @group', ->
foo = slackbot.removeFormatting 'foo <!group> bar'
foo.should.equal 'foo @group 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> <!channel>'
foo.should.equal 'foo label bar #general @channel'

describe 'Send Messages', ->
it 'Should send multiple messages', ->
Expand Down

0 comments on commit 274dc28

Please sign in to comment.