Skip to content

Instantly share code, notes, and snippets.

@nickoneill
Last active May 19, 2016 00:40
Show Gist options
  • Save nickoneill/5275c8ea783a8a58d84c to your computer and use it in GitHub Desktop.
Save nickoneill/5275c8ea783a8a58d84c to your computer and use it in GitHub Desktop.
Post to Slack from Parse Cloud Code
function Slack(hook_url) {
this.hook_url = hook_url;
}
Slack.prototype.send = function(message) {
if (!message.text) {
return;
}
if (!message.channel) { message.channel = '#general'; }
var command = this.hook_url;
var body = {
channel: message.channel,
text: message.text,
username: message.username
};
if (message.icon_url) { body.icon_url = message.icon_url; }
if (message.icon_emoji) { body.icon_emoji = message.icon_emoji; }
if (message.attachments) { body.attachments = message.attachments; }
if (message.unfurl_links) { body.unfurl_links = message.unfurl_links; }
if (message.link_names) { body.link_names = message.link_names; }
return Parse.Cloud.httpRequest({
method: 'POST',
url: command,
body: JSON.stringify(body)
});
};
module.exports = Slack;
var Slack = require('cloud/slack.js');
var slack = new Slack('https://hooks.slack.com/services/INCOMING_SLACK_WEBHOOK_URL');
return slack.send({text: msg}).then(function(){
// success
}, function(error){
// error
});
@seancrowe5
Copy link

Thanks so much for this :) Works perfectly!!

@stevecarlton
Copy link

Awesome. Thank you!

I only need one channel for now but would love to know how to specify a channel and the other options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment