Last active
January 19, 2016 00:12
-
-
Save ijin/f83e33a6ae0acd83902a to your computer and use it in GitHub Desktop.
aws lambda function to post slack
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
var aws = require('aws-sdk'); | |
var kms = new aws.KMS({ region: 'us-east-1' }); | |
var encrypted_slack_hook = 'CiD0R0tv46w7LNpO0GlZpLfZk2O0Oy66IF83rG6olDY7yBK0AQEBAgB49EdL\nb+OsOyzaTtBpWaS32ZNjtDsuuiBfN6xuqJQ2O8gAAACLMIGIBgkqhkiG9w0B\nBwagezB5AgEAMHQGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMWk63DEyG\ne+3v8STHAgEQgEfYBkRk3roZCsirvAsWbPhGN15PqfGA58M/Vh3ZqHFFCkf9\nceSWkIii4SZz3tlpHu3AXjk3x2AWTCMmVTX2EUkawF5sTNsNZA==\n'; | |
//console.log('Loading function'); | |
exports.handler = function(event, context) { | |
console.log('Received event:', JSON.stringify(event, null, 2)); | |
var p = {CiphertextBlob: new Buffer(encrypted_slack_hook, 'base64')}; | |
kms.decrypt(p, function(err, data) { | |
if (err) context.fail(err); | |
else { | |
var slack_hook = data.Plaintext.toString(); | |
//console.log('slack_hook: ' + slack_hook); | |
var http = require('https'); | |
var postData = JSON.stringify({ | |
username: event.username || 'lambda', | |
icon_url: event.icon_url || 'https://raw.githubusercontent.com/donnemartin/dev-setup-resources/master/res/aws_lambda.png', | |
text: event.text, | |
channel: event.channel || '#test' | |
}); | |
var headers = { | |
'Content-Type': 'application/json', | |
'Content-Length': postData.length | |
}; | |
var options = { | |
host: 'hooks.slack.com', | |
port: 443, | |
path: '/services/' + slack_hook, | |
headers: headers, | |
method: 'POST' | |
}; | |
callback = function(response) { | |
var status = response.statusCode; | |
console.log(status); | |
var str = ''; | |
var msg = ''; | |
response.on('data', function (chunk) { | |
str += chunk; | |
}); | |
response.on('end', function () { | |
console.log(str); | |
if (status == 200){ | |
msg = 'Successfully posted to Slack!'; | |
console.log(msg); | |
context.succeed(msg); | |
} else { | |
msg = 'Failed to post to Slack...'; | |
console.log(msg); | |
context.fail(msg); | |
} | |
}); | |
response.on('error', function(e) { | |
console.log('problem with req: ' + e.message); | |
}); | |
}; | |
var req = http.request(options, callback); | |
req.write(postData); | |
req.end(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment