-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_slack_webhook.mjs
45 lines (39 loc) · 1.32 KB
/
test_slack_webhook.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const https = require('https');
const url = require('url');
// Replace YOUR_SLACK_WEBHOOK_URL with your actual Slack webhook URL
const SLACK_WEBHOOK_URL = new IncomingWebhook(process.env.SLACK_WEBHOOK_URL);
exports.handler = async (event) => {
const message = JSON.stringify({
text: "Hello, Slack! This is a test message from my AWS Lambda function.",
});
const slackUrl = url.parse(SLACK_WEBHOOK_URL);
const options = {
hostname: slackUrl.hostname,
port: 443,
path: slackUrl.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(message),
},
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
resolve('Message sent to Slack successfully!');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
reject(e);
});
// write data to request body
req.write(message);
req.end();
});
};