Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Commit f0a8097

Browse files
committed
Add a small selection of random response messages
1 parent af6bfab commit f0a8097

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
const express = require( 'express' ),
1111
bodyParser = require( 'body-parser' ),
1212
slackClient = require('@slack/client'),
13-
pg = require( 'pg' );
13+
pg = require( 'pg' ),
14+
messages = require( './messages' );
1415

1516
const SLACK_BOT_USER_OAUTH_ACCESS_TOKEN = process.env.SLACK_BOT_USER_OAUTH_ACCESS_TOKEN,
1617
SLACK_VERIFICATION_TOKEN = process.env.SLACK_VERIFICATION_TOKEN,
@@ -25,6 +26,13 @@ const app = express(),
2526
postgres = new pg.Pool({ connectionString: DATABASE_URL, ssl: true }),
2627
slack = new slackClient.WebClient( SLACK_BOT_USER_OAUTH_ACCESS_TOKEN );
2728

29+
const getRandomMessage = ( operation ) => {
30+
operation = operation.replace( '+', 'plus' ).replace( '-', 'minus' );
31+
max = messages[ operation ].length - 1;
32+
random = Math.floor( Math.random() * max );
33+
return messages[ operation ][ random ];
34+
};
35+
2836
app.use( bodyParser.json() );
2937
app.enable( 'trust proxy' );
3038

@@ -109,9 +117,11 @@ app.post( '/', async ( request, response ) => {
109117
// If the user is trying to ++ themselves...
110118
if ( item === event.user && '+' === operation ) {
111119

120+
const message = getRandomMessage( 'selfPlus' );
121+
112122
slack.chat.postMessage({
113123
channel: event.channel,
114-
text: 'Ah nope! Not cool <@' + event.user + '>!'
124+
text: '<@' + event.user + '> ' + message,
115125
}).then( ( data ) => {
116126
console.log(
117127
data.ok ?
@@ -140,12 +150,15 @@ app.post( '/', async ( request, response ) => {
140150
dbClient.release();
141151

142152
// Respond.
143-
// TODO: Add some much better messages here! And also the ability to customise them - probably
144-
// via JSON objects in environment variables.
145153
const itemMaybeLinked = item.match( /U[A-Z0-9]{8}/ ) ? '<@' + item + '>' : item;
154+
const pluralise = score === 1 ? '' : 's';
155+
const message = getRandomMessage( operation );
146156
slack.chat.postMessage({
147157
channel: event.channel,
148-
text: 'Heard ya loud and clear! *' + itemMaybeLinked + '* is now on ' + score + '.'
158+
text: (
159+
message + ' ' +
160+
'*' + itemMaybeLinked + '* is now on ' + score + ' point' + pluralise + '.'
161+
)
149162
}).then( ( data ) => {
150163
console.log( data.ok ? item + ' now on ' + score : 'Error occurred posting response.' );
151164
});

messages.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Provides messages for random selection.
3+
*
4+
* TODO: Add the ability to customise these messages - probably via JSON objects in environment
5+
* variables.
6+
*/
7+
8+
module.exports = {
9+
10+
plus: [
11+
"Congrats!",
12+
"Got it!",
13+
"Bravo.",
14+
"Oh well done.",
15+
"Nice work!",
16+
"Well done.",
17+
"Exquisite.",
18+
"Lovely.",
19+
"Superb.",
20+
"Classic!",
21+
"Charming.",
22+
"Noted.",
23+
"Well, well!",
24+
"Well played.",
25+
"Sincerest congratulations.",
26+
"Delicious."
27+
],
28+
29+
minus: [
30+
"Oh RLY?",
31+
"Oh, really?",
32+
"Oh :slightly_frowning_face:.",
33+
"I see.",
34+
"Ouch.",
35+
"Oh là là.",
36+
"Oh.",
37+
"Condolences."
38+
],
39+
40+
selfPlus: [
41+
"Hahahahahahaha no.",
42+
"Nope.",
43+
"No. Just no.",
44+
"Not cool!"
45+
]
46+
47+
};

0 commit comments

Comments
 (0)