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 41
/
Copy pathindex.js
233 lines (187 loc) · 7.49 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Working PlusPlus++
* Like plusplus.chat, but one that actually works, because you can host it yourself! 😉
*
* @see https://github.com/tdmalone/working-plusplus
* @see https://api.slack.com/events-api
* @author Tim Malone <tdmalone@gmail.com>
*/
'use strict';
const express = require( 'express' ),
bodyParser = require( 'body-parser' ),
slackClient = require( '@slack/client' ),
pg = require( 'pg' ),
{ getRandomMessage } = require( './messages' );
// Get environment variables.
/* eslint-disable no-process-env, no-magic-numbers */
const SLACK_OAUTH_ACCESS_TOKEN = process.env.SLACK_BOT_USER_OAUTH_ACCESS_TOKEN,
SLACK_VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN,
DATABASE_URL = process.env.DATABASE_URL,
PORT = process.env.PORT || 80; // Let Heroku set the port.
/* eslint-enable no-process-env, no-magic-numbers */
const HTTP_403 = 403,
HTTP_500 = 500,
scoresTableName = 'scores',
postgresPoolConfig = {
connectionString: DATABASE_URL,
ssl: true
};
const app = express(),
postgres = new pg.Pool( postgresPoolConfig ),
slack = new slackClient.WebClient( SLACK_OAUTH_ACCESS_TOKEN );
/** Determines whether or not events sent from Slack can be handled by this app. */
const isValidEvent = ( event ) =>{
// If the event has no type, something has gone wrong.
if ( 'undefined' === typeof event.type ) {
console.warn( 'Event data missing' );
return false;
}
// We only support the 'message' event.
if ( 'message' !== event.type ) {
console.warn( 'Invalid event received: ' + event.type );
return false;
}
// If the event has a subtype, we don't support it.
if ( 'undefined' !== typeof event.subtype ) {
console.warn( 'Unsupported event subtype: ' + event.subtype );
return false;
}
// If there's no text with the message, there's not a lot we can do.
if ( 'undefined' === typeof event.text || ! event.text.trim() ) {
console.warn( 'Message text missing' );
return false;
}
return true;
}; // IsValidEvent.
/** Handles events sent from Slack. */
const handleEvent = async( event ) => {
// Drop events where the text that doesn't mention anybody/anything.
if ( -1 === event.text.indexOf( '@' ) ) {
return false;
}
// Drop events where the text doesn't include a valid operation.
const validOperations = [
'++',
'--',
'—' // Supports iOS' automatic replacement of --.
];
if ( ! validOperations.some( element => -1 !== event.text.indexOf( element ) ) ) {
return false;
}
// If we're still here, it's a message to deal with!
// Get the user or 'thing' that is being spoken about, and the 'operation' being done on it.
// We take the operation down to one character, and also support — due to iOS' replacement of --.
const data = event.text.match( /@([A-Za-z0-9.\-_]*?)>?\s*([-+]{2}|—{1})/ );
const item = data[1];
const operation = data[2].substring( 0, 1 ).replace( '—', '-' );
// If we somehow didn't get anything, drop it. This can happen when eg. @++ is typed.
if ( ! item.trim() ) {
return false;
}
// If the user is trying to ++ themselves...
if ( item === event.user && '+' === operation ) {
const message = getRandomMessage( 'selfPlus' );
slack.chat.postMessage({
channel: event.channel,
text: '<@' + event.user + '> ' + message
}).then( ( data ) => {
console.log(
data.ok ?
item + ' tried to alter their own score.' :
'Error occurred posting response to user altering their own score.'
);
});
return false;
} // If self ++.
// Connect to the DB, and create a table if it's not yet there.
// We also set up the citext extension, so that we can easily be case insensitive.
const dbClient = await postgres.connect();
await dbClient.query( '\
CREATE EXTENSION IF NOT EXISTS citext; \
CREATE TABLE IF NOT EXISTS ' + scoresTableName + ' (item CITEXT PRIMARY KEY, score INTEGER); \
' );
// Atomically record the action.
// TODO: Fix potential SQL injection issues here, even though we know the input should be safe.
await dbClient.query( '\
INSERT INTO ' + scoresTableName + ' VALUES (\'' + item + '\', ' + operation + '1) \
ON CONFLICT (item) DO UPDATE SET score = ' + scoresTableName + '.score ' + operation + ' 1; \
' );
// Get the new value.
// TODO: Fix potential SQL injection issues here, even though we know the input should be safe.
const dbSelect = await dbClient.query( '\
SELECT score FROM ' + scoresTableName + ' WHERE item = \'' + item + '\'; \
' );
const score = dbSelect.rows[0].score;
dbClient.release();
// Respond.
const itemMaybeLinked = item.match( /U[A-Z0-9]{8}/ ) ? '<@' + item + '>' : item;
const pluralise = 1 === score ? '' : 's';
const message = getRandomMessage( operation );
slack.chat.postMessage({
channel: event.channel,
text: (
message + ' ' +
'*' + itemMaybeLinked + '* is now on ' + score + ' point' + pluralise + '.'
)
}).then( ( data ) => {
console.log( data.ok ? item + ' now on ' + score : 'Error occurred posting response.' );
});
}; // HandleEvent.
/** Handles GET requests to the app. */
const handleGet = ( request, response ) => {
response.send( 'It works! However, this app only accepts POST requests for now.' );
};
/** Handles POST requests to the app. */
const handlePost = ( request, response ) => {
// Simple logging of requests.
console.log(
request.ip + ' ' + request.method + ' ' + request.path + ' ' + request.headers['user-agent']
);
// Respond to challenge sent by Slack during event subscription set up.
if ( request.body.challenge ) {
response.send( request.body.challenge );
console.info( '200 Challenge response sent' );
return;
}
// Sanity check for bad verification values - empty, or still set to the default.
if ( ! SLACK_VERIFICATION_TOKEN || 'xxxxxxxxxxxxxxxxxxxxxxxx' === SLACK_VERIFICATION_TOKEN ) {
response.status( HTTP_500 ).send( 'Internal server error.' );
console.error( '500 Internal server error - bad verification value' );
return;
}
// Check that this is Slack making the request.
// TODO: Move to calculating the signature instead (newer, more secure method).
if ( SLACK_VERIFICATION_TOKEN !== request.body.token ) {
response.status( HTTP_403 ).send( 'Access denied.' );
console.error( '403 Access denied - incorrect verification token' );
return;
}
// Send back a 200 OK now so Slack doesn't get upset.
response.send( '' );
// Drop retries. This is controversial. But, because we're mainly gonna be running on free Heroku
// dynos, we'll be sleeping after inactivity. It takes longer than Slack's 3 second limit to start
// back up again, so Slack will retry immediately and then again in a minute - which will result
// in the action being carried out 3 times if we listen to it!
// @see https://api.slack.com/events-api#graceful_retries
if ( request.headers['x-slack-retry-num']) {
console.log( 'Skipping Slack retry.' );
return;
}
// Handle the event now, if it's valid.
if ( isValidEvent( request.body.event ) ) {
handleEvent( request.body.event );
}
}; // HandlePost.
app.use( bodyParser.json() );
app.enable( 'trust proxy' );
app.get( '/', handleGet );
app.post( '/', handlePost );
app.listen( PORT, () => {
console.log( 'Listening on port ' + PORT + '.' );
});
module.exports = {
isValidEvent: isValidEvent,
handleEvent: handleEvent,
handleGet: handleGet,
handlePost: handlePost
};