Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip formatting better, use new SlackTextMessage class #129

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 47 additions & 41 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -122,55 +122,56 @@ class SlackBot extends Adapter

else
# Build message text to respond to, including all attachments
txt = msg.getBody()
rawText = msg.getBody()
text = @removeFormatting rawText

txt = @removeFormatting txt

@robot.logger.debug "Received message: '#{txt}' in channel: #{channel.name}, from: #{user.name}"
@robot.logger.debug "Received message: '#{text}' in channel: #{channel.name}, from: #{user.name}"

# If this is a DM, pretend it was addressed to us
if msg.getChannelType() == 'DM'
txt = "#{@robot.name} #{txt}"
text = "#{@robot.name} #{text}"

@receive new TextMessage user, txt, msg.ts
@receive new SlackTextMessage user, text, rawText, msg.ts

removeFormatting: (txt) ->
removeFormatting: (text) ->
# 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
text = text.replace ///
< # opening angle bracket
([@#!])? # link type (optional)
([^>|]*) # link
(?:\| # start of |label (optional)
([^>]*) # label
)? # end of |label
> # closing angle bracket
///g, (match, type, link, label) =>
return label if label

switch type
when '@'
user = @client.getUserByID id
if user
return "@#{user.name}"
user = @client.getUserByID link
return "@#{user.name}" if user
when '#'
channel = @client.getChannelByID id
if channel
return "\##{channel.name}"
channel = @client.getChannelByID link
return "##{channel.name}" if channel
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) =>
link = link.replace /^mailto:/, ''
if label
"#{label} #{link}"
else
link
txt
return "@#{link}" if link in ['channel','group','everyone']
else
# not a special link at all, this is just a regular link
link = link.replace /^mailto:/, ''
return link
# we'll get here if we have a #@! link that wasn't already handled
# (e.g. an unknown !command or a @/# link with a bad id)
"#{type}#{link}"
# remove entities as well
# we could use the 'entities' package, but slack only uses &lt, &gt, and &amp.
text.replace /&(lt|gt|amp);/g, (match, name) ->
switch name
when 'lt'
"<"
when 'gt'
">"
when 'amp'
"&"

send: (envelope, messages...) ->
channel = @client.getChannelGroupOrDMByName envelope.room
Expand Down Expand Up @@ -224,8 +225,13 @@ class SlackBot extends Adapter
channel = @client.getChannelGroupOrDMByName envelope.room
channel.setTopic strings.join "\n"

exports.use = (robot) ->
new SlackBot robot
class SlackTextMessage extends TextMessage
constructor: (@user, @text, @rawText, @id) ->
super @user, @text, @id

# Export class for unit tests
exports.SlackBot = SlackBot
module.exports = {
SlackBot
SlackTextMessage
use: (robot) ->
new SlackBot robot
}
4 changes: 2 additions & 2 deletions test/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ describe 'Removing message formatting', ->

it 'Should remove formatting around <https> links with a label', ->
foo = slackbot.removeFormatting 'foo <https://www.example.com|label> bar'
foo.should.equal 'foo label https://www.example.com bar'
foo.should.equal 'foo label bar'

it 'Should remove formatting around <mailto> links', ->
foo = slackbot.removeFormatting 'foo <mailto:[email protected]> bar'
foo.should.equal 'foo [email protected] bar'

it 'Should change multiple links at once', ->
foo = slackbot.removeFormatting 'foo <@U123|label> bar <#C123> <!channel> <https://www.example.com|label>'
foo.should.equal 'foo label bar #general @channel label https://www.example.com'
foo.should.equal 'foo label bar #general @channel label'

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