Created
November 13, 2020 13:41
-
-
Save olivatooo/c8f2f5b60d35a9cddfe01e8e046602f4 to your computer and use it in GitHub Desktop.
Send message from AWS Lamdba to discord bot webhook
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 zlib = require("zlib"); | |
const https = require("https"); | |
exports.handler = function (input, context) { | |
console.log(input) | |
var payload = Buffer.from(input.awslogs.data, "base64"); | |
zlib.gunzip(payload, function (e, result) { | |
if (e) { | |
context.fail(e); | |
} else { | |
result = result.toString(); | |
if ( result.toLowerCase().indexOf("error") !== -1) { | |
const data = JSON.stringify({ | |
username: "BotName", | |
content: result, | |
}); | |
const options = { | |
hostname: "discord.com", | |
port: 443, | |
path: "$DISCORD_WEBHOOK_KEY", | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
"Content-Length": data.length, | |
}, | |
}; | |
const req = https.request(options, (res) => { | |
console.log(`statusCode: ${res.statusCode}`); | |
res.on("data", (d) => { | |
process.stdout.write(d); | |
}); | |
}); | |
req.on("error", (error) => { | |
console.error(error); | |
}); | |
req.write(data); | |
req.end(); | |
} | |
context.succeed(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment