This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
152 lines (116 loc) · 3.83 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Receives a custom 'coffee shop closing' event from an AWS API Gateway proxy endpoint.
* Disseminates it to a custom Slack hook and SNS topic.
*
* Designed for use with a Flic button at The Good Food Collective, Canterbury VIC Australia.
*
* @author Tim Malone <[email protected]>
*/
'use strict';
const aws = require( 'aws-sdk' ),
https = require( 'https' ),
lambdaProxyResponse = require( '@tdmalone/lambda-proxy-response' );
// These will be like constants, but we can only set them once we have the context object.
let SLACK_HOOK, SNS_TOPIC;
exports.handler = function( event, context, callback ) {
// Manage separate environment variables per stage.
/* eslint-disable no-process-env */
const environment = 'prod' === context.invokedFunctionArn.split( ':' ).pop() ? 'prod' : 'dev',
envSuffix = '_' + environment.toUpperCase();
SLACK_HOOK = process.env[ 'SLACK_HOOK' + envSuffix ];
SNS_TOPIC = process.env[ 'SNS_TOPIC' + envSuffix ];
console.info( 'Running in ' + environment + ' mode.' );
/* eslint-enable no-process-env */
let message = '';
console.info( event.pathParameters.proxy );
switch ( event.pathParameters.proxy ) {
case 'closing/soon':
message = 'Hey guys, just to let you know, we\'ll be closing soon.';
break;
case 'closing/now':
message = 'We\'re closing! Get down here *now* if you want coffee!';
break;
case 'closing/early':
message = (
'Hey guys, we\'ll be closing early today.. maybe 2, or 3 or something. I dunno. ' +
'This thing doesn\'t let me put in a number.'
);
break;
default:
throw Error( 'Invalid path specified: ' + event.pathParameters.proxy );
}
Promise.all([ sendSnsMessage( message ), sendToSlack( message ) ])
.then( () => {
lambdaProxyResponse( null, 'Message sent, thanks guys!', callback );
})
.catch( ( error ) => {
lambdaProxyResponse( error, null, callback );
});
}; // Exports.handler.
/**
* Sends an SNS message to the topic provided by the environment.
*
* @param {string} message The message to send.
* @return {Promise} A promise to send the message, funnily enough!
*/
function sendSnsMessage( message ) {
return new Promise( ( resolve, reject ) => {
if ( ! SNS_TOPIC ) {
reject( 'No SNS_TOPIC provided.' );
return;
}
const snsMessage = {
Message: JSON.stringify( message ),
TopicArn: SNS_TOPIC
};
const sns = new aws.SNS();
sns.publish( snsMessage, ( error, result ) => {
if ( error ) {
reject( error );
return;
}
resolve( result );
}); // Sns.publish.
}); // Return Promise.
} // Function sendSnsMessage.
/**
* Sends a message to the incoming Slack webhook provided by the environment.
*
* @param {string} message The message to send.
* @return {Promise} A promise to send the message!
*/
function sendToSlack( message ) {
return new Promise( ( resolve, reject ) => {
if ( ! SLACK_HOOK ) {
reject( 'No SLACK_HOOK provided.' );
return;
}
const options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: '/services/' + SLACK_HOOK
};
const data = {
text: message
};
const request = https.request( options, ( response ) => {
let body = '';
response.setEncoding( 'utf8' );
response.on( 'data', ( chunk ) => {
body += chunk;
}).on( 'end', () => {
if ( 'ok' === body ) {
resolve( body );
return;
}
reject( body ? body : 'No response received from Slack.' );
});
});
request.on( 'error', ( error ) => {
reject( 'Error with Slack request: ' + error.message );
});
request.write( JSON.stringify( data ) );
request.end();
}); // Return Promise.
} // Function sendToSlack.