Created
July 28, 2020 05:11
-
-
Save tranvansang/d155801d0635f5546182e9f659d8216a to your computer and use it in GitHub Desktop.
Send slack webhook from AWS Lambda on AWS SNS notification (single script without library)
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
// NodeJS v12 | |
const https = require('https') | |
const send = data => { | |
const options = { | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: '/services/<webhook-id>', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': data.length | |
} | |
} | |
return new Promise((resolve, reject) => { | |
const req = https.request(options, res => { | |
console.log('code = ', res.statusCode) | |
console.log('headers = ', res.headers) | |
res.on('data', console.log.bind('[data]')) | |
res.on('end', resolve) | |
res.on('error', reject) | |
}) | |
req.on('error', reject) | |
req.write(data) | |
req.end() | |
}) | |
} | |
exports.handler = async event => { | |
const text = event.Records.map(record => record.Sns.Message).join('\n') | |
await send(JSON.stringify({text})) | |
return { | |
statusCode: 200, | |
body: JSON.stringify(`Sent message ${text}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/ciaranj/node-oauth/blob/a7f8a1e21c362eb4ed2039431fb9ac2ae749f26a/lib/oauth2.js
https://nodejs.org/en/knowledge/HTTP/clients/how-to-create-a-HTTP-request/
https://nodejs.dev/learn/making-http-requests-with-nodejs