1010const 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
1516const 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+
2836app . use ( bodyParser . json ( ) ) ;
2937app . 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 - Z 0 - 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 } ) ;
0 commit comments