Last active
May 19, 2016 00:40
-
-
Save nickoneill/5275c8ea783a8a58d84c to your computer and use it in GitHub Desktop.
Post to Slack from Parse Cloud Code
This file contains 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
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; |
This file contains 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
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 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome. Thank you!
I only need one channel for now but would love to know how to specify a channel and the other options.