-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (47 loc) · 1.44 KB
/
index.js
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
46
47
48
49
50
51
52
53
/**
* This lambda function sends a message to slack letting folks know there are snacks in the kitchen.
* The following JSON template shows what is sent as the payload:
{
"serialNumber": "GXXXXXXXXXXXXXXXXX",
"batteryVoltage": "xxmV",
"clickType": "SINGLE" | "DOUBLE" | "LONG"
}
*
* A "LONG" clickType is sent if the first press lasts longer than 1.5 seconds.
* "SINGLE" and "DOUBLE" clickType payloads are sent for short clicks.
*
* For more documentation, follow the link below.
* http://docs.aws.amazon.com/iot/latest/developerguide/iot-lambda-rule.html
*/
const AWS = require('aws-sdk');
const https = require('https');
exports.handler = (event, context, callback) => {
console.log('Received event:', event.clickType);
var options = {
hostname: 'hooks.slack.com',
port: 443,
path: 'NOTIFICATION_PATH',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var req = https.request(options, (res) => {
console.log('statusCode: ', res.statusCode);
console.log('headers: ', res.headers);
res.on('data', (d) => {
console.log('response', d);
if (callback) {
callback();
}
});
});
req.write(JSON.stringify({"text": "There are snacks in the kitchen!"}));
req.end();
req.on('error', (e) => {
console.error(e);
if (callback) {
callback();
}
});
};