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

Add back support for Slack attachments #148

Merged
merged 1 commit into from
Feb 27, 2015
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@
"test": "mocha --compilers coffee:coffee-script/register --reporter spec"
},
"dependencies": {
"slack-client": "~1.2.0"
"slack-client": "~1.4.0"
}
}
32 changes: 32 additions & 0 deletions src/slack.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class SlackBot extends Adapter
@client.on 'userChange', @.userChange
@robot.brain.on 'loaded', @.brainLoaded

@robot.on 'slack-attachment', @.customMessage
@robot.on 'slack.attachment', @.customMessage

# Start logging in
@client.login()

Expand Down Expand Up @@ -245,4 +248,33 @@ class SlackBot extends Adapter
channel = @client.getChannelGroupOrDMByName envelope.room
channel.setTopic strings.join "\n"

customMessage: (data) =>

channelName = if data.channel
data.channel
else if data.message.envelope
data.message.envelope.room
else data.message.room

channel = @client.getChannelGroupOrDMByName channelName
return unless channel

msg = {}
msg.attachments = data.attachments || data.content
msg.attachments = [msg.attachments] unless Array.isArray msg.attachments

msg.text = data.text

if data.username && data.username != robot.name
msg.as_user = false
if data.icon_url?
msg.icon_url = data.icon_url
else if data.icon_emoji?
msg.icon_emoji = data.icon_emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't seem to actually be adding data.username to the msg here. Surely that's necessary or it won't know what name to post as.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless it's getting the user name from the authentication token, which might make sense, but explicitly setting the username makes even more sense, IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as_user causes the message to get the name from the authentication token.

This is because behind the scenes as_user causes the message to be sent as a standard message instead of a bot_message, which means the message is correctly attributed (among many other things).

else
msg.as_user = true

channel.postMessage msg

# Export class for unit tests
module.exports = SlackBot