Last active
April 14, 2016 14:53
-
-
Save jarosite/c127f2063a9b3edfad604aaa340ccdfc to your computer and use it in GitHub Desktop.
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
const https = require('https'); | |
const url = require('url'); | |
// to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration | |
const slack_url = 'https://hooks.slack.com/services/XXXX/XXXXX/XXXXXX'; | |
const slack_req_opts = url.parse(slack_url); | |
const slack_channel = '@user_name'; //or channel name | |
slack_req_opts.method = 'POST'; | |
slack_req_opts.headers = {'Content-Type': 'application/json'}; | |
exports.handler = function(event, context, callback) { | |
(event.Records || []).forEach(function (rec) { | |
if (rec.Sns) { | |
var req = https.request(slack_req_opts, function (res) { | |
if (res.statusCode !== 200) { | |
callback('failed to post message, status code: ' + res.statusCode); | |
} | |
}); | |
req.on('error', function(e) { | |
callback(e); | |
}); | |
req.write(JSON.stringify( | |
{ | |
channel: slack_channel, | |
username: 'AWS', | |
attachments: [{ | |
fallback: JSON.stringify(rec.Sns.Subject, null, ' '), | |
pretext: rec.Sns.Subject, | |
text: rec.Sns.Message | |
}] | |
})); | |
req.end(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment