-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbot.js
More file actions
159 lines (144 loc) · 4.17 KB
/
bot.js
File metadata and controls
159 lines (144 loc) · 4.17 KB
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
153
154
155
156
157
158
'use strict';
const AWS = require('aws-sdk');
const Slack = require('slack');
/**
* Handles the http request, calls the bot lambda and responds the request with data
* @async
* @param {Object} data
* @return {Object}
*/
module.exports.run = async ( data ) =>
{
const dataObject = JSON.parse( data.body );
// The response we will return to Slack
let response = {
statusCode: 200,
body : {},
// Tell slack we don't want retries, to avoid multiple triggers of this lambda
headers : { 'X-Slack-No-Retry': 1 }
};
try {
// If the Slack retry header is present, ignore the call to avoid triggering the lambda multiple times
if ( !( 'X-Slack-Retry-Num' in data.headers ) )
{
switch ( dataObject.type )
{
case 'url_verification':
response.body = verifyCall( dataObject );
break;
case 'event_callback':
await handleMessage( dataObject.event );
response.body = { ok: true };
break;
default:
response.statusCode = 400,
response.body = 'Empty request';
break;
}
}
}
catch( err )
{
response.statusCode = 500,
response.body = JSON.stringify( err )
}
finally
{
return response;
}
}
/**
* Verifies the URL with a challenge - https://api.slack.com/events/url_verification
* @param {Object} data The event data
*/
function verifyCall( data )
{
if ( data.token === process.env.VERIFICATION_TOKEN )
{
return data.challenge;
}
else {
throw 'Verification failed';
}
}
/**
* Process the message and executes an action based on the message received
* @async
* @param {Object} message The Slack message object
*/
async function handleMessage( message )
{
// Makes sure the bot was actually mentioned
if ( !message.bot_id )
{
// Gets the command from the message
let command = parseMessage( message.text );
// Executes differend commands based in the specified instruction
switch ( command )
{
case 'invalidate_cdn':
const invalidationData = await invalidateDistribution();
await sendSlackMessage( message.channel,
`Sir/Madam, I've just invalidated the cache, this is the invalidation ID. *${invalidationData.Invalidation.Id}*` );
break;
default:
await sendSlackMessage( message.channel,
`Sir/Madam, I don't understand what you need. Please use \`@${process.env.BOT_NAME} invalidate_cdn\` to clear the CDN cache.` );
break;
}
}
}
/**
* Sends a message to Slack
* @param {String} channel
* @param {String} message
* @return {Promise}
*/
function sendSlackMessage( channel, message )
{
const params = {
token : process.env.BOT_TOKEN,
channel: channel,
text : message
};
return Slack.chat.postMessage( params );
}
/**
* Parses the command/intent from the text of a message received by the bot
* @param {String} message
* @return {String}
*/
function parseMessage( message )
{
return message.split( ' ', 2 ).pop();
}
/**
* Creates an invalidation in the configured CloudFront distribution and returns the invalidation ID
* @return {Promise|String}
*/
function invalidateDistribution()
{
const CloudFront = new AWS.CloudFront();
// Invalidation parameters
const params = {
DistributionId: process.env.CDN_DISTRIBUTION,
InvalidationBatch: {
CallerReference: Date.now() + '',
Paths: {
Quantity: '1',
Items: [
'/*'
]
}
}
};
return new Promise( ( resolve, reject ) =>
{
// Call the CloudFront wrapper to invalidate the CDN cache
CloudFront.createInvalidation( params, ( err, data ) =>
{
if ( err ) reject( err );
else resolve( data );
} );
} );
}