Created
March 2, 2015 08:14
-
-
Save spoike/6483e2044e70dbb3182a to your computer and use it in GitHub Desktop.
Simple Slack Incoming Webhook script for hubot to post useful messages (in pretty format) on channels/rooms. Also works without having hubot invited on the channel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* # Post messages using Slack's Webhook | |
* | |
* Use this in combination with node-cron to Schedule messages. | |
* No need to have the hubot invited on the actual channel to post. | |
* | |
* ## Dependencies | |
* | |
* superagent | |
* lodash | |
* | |
* ## How to use | |
* | |
* 1. Enable Incoming Webhook integration on slack. | |
* 2. Set SLACK_WEBHOOK_URL with the webhook url from the enabled Slack integration | |
* 3. Usage (in coffeescript): | |
* | |
* # on init | |
* slack = require('slack_post') | |
* icon_emoji: ':calendar:' | |
* channel: '#another_slack_room' | |
* username: 'Calendar Post (via Hubot)' | |
* | |
* # on message | |
* slack.post "Message" | |
*/ | |
var request = require('superagent'), | |
_ = require('lodash'); | |
var defaultOptions = { | |
channel: "#general", | |
icon_emoji: ':ghost:', | |
username: 'Hubot' | |
}; | |
function ensureChannelName(channel) { | |
return channel[0] === "#" ? channel : "#" + channel; | |
} | |
module.exports = function(options) { | |
var url = process.env.SLACK_WEBHOOK_URL; | |
return { | |
post: function(text, attachments) { | |
var postOptions = _.defaults({}, options, defaultOptions); | |
postOptions.text = text; | |
if (_.isArray(attachments) && attachments.length > 0) { | |
postOptions.attachments = attachments; | |
} | |
request.post(url) | |
.send(postOptions) | |
.end(function (err, res) { | |
console.error(err); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment