-
-
Save veganstraightedge/c5b5954be4897c0aa093 to your computer and use it in GitHub Desktop.
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