Skip to content

Instantly share code, notes, and snippets.

@veganstraightedge
Forked from spoike/slack_post.js
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veganstraightedge/c5b5954be4897c0aa093 to your computer and use it in GitHub Desktop.
Save veganstraightedge/c5b5954be4897c0aa093 to your computer and use it in GitHub Desktop.
/**
* # 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