Last active
July 15, 2018 00:33
-
-
Save munky69rock/29880e5117f6724defed73a535a92ba5 to your computer and use it in GitHub Desktop.
Lambda + API Gatewayを利用してherokuのDeploy HooksをSlackに通知する (要Lambda Proxy Integration)
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 querystring = require('querystring'); | |
const url = require('url'); | |
const SLACK_URL = url.parse('__YOUR_SLACK_WEBHOOK_URL__'); | |
const slackOptions = { | |
channel: '#heroku', | |
username: 'heroku', | |
icon_emoji: ':heroku:' | |
}; | |
exports.handler = (event, context, callback) => { | |
const data = querystring.parse(event.body); | |
const done = (err, res) => callback(null, { | |
statusCode: err ? '400' : '200', | |
body: err ? err.message : JSON.stringify(res), | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
}); | |
const postData = JSON.stringify(Object.assign({}, slackOptions, { | |
attachments: [{ | |
color: "good", | |
pretext: `${data.user} deployed version ${data.head} of ${data.app}`, | |
text: data.git_log, | |
fields: [{ | |
title: "URL", | |
value: data.url, | |
short: false | |
}] | |
}] | |
})); | |
const httpOptions = { | |
host: SLACK_URL.host, | |
port: 443, | |
path: SLACK_URL.path, | |
method: "POST", | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': postData.length | |
} | |
}; | |
const req = https.request(httpOptions, (res) => { | |
res.setEncoding('utf8'); | |
let responseBody = ''; | |
res.on('data', (chunk) => { | |
responseBody += chunk; | |
}) | |
res.on('end', () => { | |
done(null, { message: responseBody }); | |
}); | |
}) | |
req.on('error', (e) => { | |
console.log(`problem with request : ${e.message}`); | |
done(e); | |
}); | |
req.write(postData); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment