From 471e71dea0acdf000f1fb0dfa621156d61148e8e Mon Sep 17 00:00:00 2001 From: Paul Hammond Date: Fri, 30 Jan 2015 14:58:45 -0800 Subject: [PATCH] Add back support for Slack attachments --- package.json | 2 +- src/slack.coffee | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 1349cb09..613b3eda 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,6 @@ "test": "mocha --compilers coffee:coffee-script/register --reporter spec" }, "dependencies": { - "slack-client": "~1.2.0" + "slack-client": "~1.4.0" } } diff --git a/src/slack.coffee b/src/slack.coffee index e1835ada..78e87e2a 100644 --- a/src/slack.coffee +++ b/src/slack.coffee @@ -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() @@ -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 + else + msg.as_user = true + + channel.postMessage msg + +# Export class for unit tests module.exports = SlackBot