Refactor for Slack slash commands instead of mentions.

Switch to a 4-wide tab.
This commit is contained in:
Gitea 2019-03-13 22:32:35 -04:00
parent 6351d69026
commit 06500b3653
3 changed files with 176 additions and 48 deletions

View File

@ -6,10 +6,10 @@ const bodyParser = require('body-parser');
const moment = require('moment');
const { Console } = require('console');
const fs = require('fs');
const strategies = require('./strategies');
const app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
@ -18,62 +18,64 @@ const errorOutput = fs.createWriteStream('./error.log', {flags: 'a'});
const fileLog = new Console(output, errorOutput);
const logger = {
log: message => {
console.log(message);
fileLog.log(message);
},
error: message => {
console.error(message);
fileLog.error(message);
}
log: message => {
console.log(message);
fileLog.log(message);
},
error: message => {
console.error(message);
fileLog.error(message);
}
}
app.get('/', (req, res) => {
const theStrategy = strategies[ Math.floor(Math.random()*strategies.length) ];
res.write('<head><meta name="viewport" content="width=device-width, initial-scale=1"></head><body style="background-color:#111; margin:0; padding:0; display:flex;align-items:center;justify-content:center;min-height:24em;"><div style="color:#eee;margin:0 auto; max-width:80%;flex:1;text-align:center;font-family:Futura;font-size:30px;line-height:40px">');
res.write(theStrategy.toLowerCase());
logger.log('\non '+moment().format("dddd, MMMM Do YYYY, h:mma").toLowerCase()+' a card was drawn');
res.write('</div></body>');
res.end();
const theStrategy = strategies[ Math.floor(Math.random()*strategies.length) ];
res.write('<head><meta name="viewport" content="width=device-width, initial-scale=1"></head><body style="background-color:#111; margin:0; padding:0; display:flex;align-items:center;justify-content:center;min-height:24em;"><div style="color:#eee;margin:0 auto; max-width:80%;flex:1;text-align:center;font-family:Futura;font-size:30px;line-height:40px">');
res.write(theStrategy.toLowerCase());
logger.log('\non '+moment().format("dddd, MMMM Do YYYY, h:mma").toLowerCase()+' a card was drawn');
res.write('</div></body>');
res.end();
});
app.post('/', (req, res, next) => {
if (req.body.type === 'url_verification') {
return res.status(200).json(req.body.challenge);
}
if (req.body.type ==='event_callback') {
res.sendStatus(200);
if (req.body.event.type === 'app_mention') {
var theResponse = {
hostname: 'slack.com',
path: '/api/chat.postMessage?token='+process.env.BOTACCESSTOKEN,
port: 443,
method: 'POST',
channel: 'CETLNV30Q',
headers: {
'Content-Type': 'application/json',
},
}
const theStrategy = encodeURIComponent(strategies[ Math.floor(Math.random()*strategies.length) ].toLowerCase());
var theResponseRequest = https.request(
{
host: 'slack.com',
path: `/api/chat.postMessage?token=${process.env.BOTACCESSTOKEN}&channel=CETLNV30Q&text=${theStrategy}`,
},
response => response.on('data', b => {
logger.log('\non '+moment().format("dddd, MMMM Do YYYY, h:mma").toLowerCase()+' a card was drawn');
logger.log(' via slack: '+b);
})
).end();
if (req.body.type === 'url_verification') {
return res.status(200).json(req.body.challenge);
}
// slash command
if (req.body.command) {
const channel = req.body.channel_id;
res.sendStatus(200);
var theResponse = {
hostname: 'slack.com',
path: '/api/chat.postMessage?token='+process.env.BOTACCESSTOKEN,
port: 443,
method: 'POST',
channel: channel,
headers: {
'Content-Type': 'application/json',
},
}
const theStrategy = encodeURIComponent(strategies[ Math.floor(Math.random()*strategies.length) ].toLowerCase());
var theResponseRequest = https.request(
{
host: 'slack.com',
path: `/api/chat.postMessage?token=${process.env.BOTACCESSTOKEN}&channel=${channel}&text=${theStrategy}`,
},
response => response.on('data', b => {
logger.log('\non '+moment().format("dddd, MMMM Do YYYY, h:mma").toLowerCase()+' a card was drawn');
logger.log(' via slack: '+b);
})
).end();
return res.status(200).end();
}
}
return res.status(200).end();
});
app.listen(4242, () => {
console.log('oblique strategies are being served')
console.log('oblique strategies are being served')
});

125
package-lock.json generated
View File

@ -87,6 +87,11 @@
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
"integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg="
},
"camelize": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/camelize/-/camelize-1.0.0.tgz",
"integrity": "sha1-FkpUg+Yw+kMh5a8HAg5TGDGyYJs="
},
"caseless": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/caseless/-/caseless-0.12.0.tgz",
@ -105,6 +110,11 @@
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz",
"integrity": "sha1-DPaLud318r55YcOoUXjLhdunjLQ="
},
"content-security-policy-builder": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/content-security-policy-builder/-/content-security-policy-builder-2.0.0.tgz",
"integrity": "sha512-j+Nhmj1yfZAikJLImCvPJFE29x/UuBi+/MWqggGGc515JKaZrjuei2RhULJmy0MsstW3E3htl002bwmBNMKr7w=="
},
"content-type": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
@ -133,6 +143,11 @@
"assert-plus": "^1.0.0"
}
},
"dasherize": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/dasherize/-/dasherize-2.0.0.tgz",
"integrity": "sha1-bYCcnNDPe7iVLYD8hPoT1H3bEwg="
},
"debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
@ -156,6 +171,16 @@
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz",
"integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA="
},
"dns-prefetch-control": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/dns-prefetch-control/-/dns-prefetch-control-0.1.0.tgz",
"integrity": "sha1-YN20V3dOF48flBXwyrsOhbCzALI="
},
"dont-sniff-mimetype": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/dont-sniff-mimetype/-/dont-sniff-mimetype-1.0.0.tgz",
"integrity": "sha1-WTKJDcn04vGeXrAqIAJuXl78j1g="
},
"dotenv": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-6.2.0.tgz",
@ -190,6 +215,11 @@
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc="
},
"expect-ct": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/expect-ct/-/expect-ct-0.1.1.tgz",
"integrity": "sha512-ngXzTfoRGG7fYens3/RMb6yYoVLvLMfmsSllP/mZPxNHgFq41TmPSLF/nLY7fwoclI2vElvAmILFWGUYqdjfCg=="
},
"express": {
"version": "4.16.4",
"resolved": "https://registry.npmjs.org/express/-/express-4.16.4.tgz",
@ -247,6 +277,11 @@
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.0.0.tgz",
"integrity": "sha1-1RQsDK7msRifh9OnYREGT4bIu/I="
},
"feature-policy": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/feature-policy/-/feature-policy-0.2.0.tgz",
"integrity": "sha512-2hGrlv6efG4hscYVZeaYjpzpT6I2OZgYqE2yDUzeAcKj2D1SH0AsEzqJNXzdoglEddcIXQQYop3lD97XpG75Jw=="
},
"finalhandler": {
"version": "1.1.1",
"resolved": "http://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz",
@ -281,6 +316,11 @@
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz",
"integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ="
},
"frameguard": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/frameguard/-/frameguard-3.0.0.tgz",
"integrity": "sha1-e8rUae57lukdEs6zlZx4I1qScuk="
},
"fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
@ -308,6 +348,66 @@
"har-schema": "^2.0.0"
}
},
"helmet": {
"version": "3.15.1",
"resolved": "https://registry.npmjs.org/helmet/-/helmet-3.15.1.tgz",
"integrity": "sha512-hgoNe/sjKlKNvJ3g9Gz149H14BjMMWOCmW/DTXl7IfyKGtIK37GePwZrHNfr4aPXdKVyXcTj26RgRFbPKDy9lw==",
"requires": {
"depd": "2.0.0",
"dns-prefetch-control": "0.1.0",
"dont-sniff-mimetype": "1.0.0",
"expect-ct": "0.1.1",
"feature-policy": "0.2.0",
"frameguard": "3.0.0",
"helmet-crossdomain": "0.3.0",
"helmet-csp": "2.7.1",
"hide-powered-by": "1.0.0",
"hpkp": "2.0.0",
"hsts": "2.1.0",
"ienoopen": "1.0.0",
"nocache": "2.0.0",
"referrer-policy": "1.1.0",
"x-xss-protection": "1.1.0"
},
"dependencies": {
"depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw=="
}
}
},
"helmet-crossdomain": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/helmet-crossdomain/-/helmet-crossdomain-0.3.0.tgz",
"integrity": "sha512-YiXhj0E35nC4Na5EPE4mTfoXMf9JTGpN4OtB4aLqShKuH9d2HNaJX5MQoglO6STVka0uMsHyG5lCut5Kzsy7Lg=="
},
"helmet-csp": {
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/helmet-csp/-/helmet-csp-2.7.1.tgz",
"integrity": "sha512-sCHwywg4daQ2mY0YYwXSZRsgcCeerUwxMwNixGA7aMLkVmPTYBl7gJoZDHOZyXkqPrtuDT3s2B1A+RLI7WxSdQ==",
"requires": {
"camelize": "1.0.0",
"content-security-policy-builder": "2.0.0",
"dasherize": "2.0.0",
"platform": "1.3.5"
}
},
"hide-powered-by": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/hide-powered-by/-/hide-powered-by-1.0.0.tgz",
"integrity": "sha1-SoWtZYgfYoV/xwr3F0oRhNzM4ys="
},
"hpkp": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/hpkp/-/hpkp-2.0.0.tgz",
"integrity": "sha1-EOFCJk52IVpdMMROxD3mTe5tFnI="
},
"hsts": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/hsts/-/hsts-2.1.0.tgz",
"integrity": "sha512-zXhh/DqgrTXJ7erTN6Fh5k/xjMhDGXCqdYN3wvxUvGUQvnxcFfUd8E+6vLg/nk3ss1TYMb+DhRl25fYABioTvA=="
},
"http-errors": {
"version": "1.6.3",
"resolved": "http://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz",
@ -337,6 +437,11 @@
"safer-buffer": ">= 2.1.2 < 3"
}
},
"ienoopen": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/ienoopen/-/ienoopen-1.0.0.tgz",
"integrity": "sha1-NGpCj0dKrI9QzzeE6i0PFvYr2ms="
},
"inherits": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
@ -436,6 +541,11 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz",
"integrity": "sha1-KzJxhOiZIQEXeyhWP7XnECrNDKk="
},
"nocache": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/nocache/-/nocache-2.0.0.tgz",
"integrity": "sha1-ICtIAhoMTL3i34DeFaF0Q8i0OYA="
},
"oauth-sign": {
"version": "0.9.0",
"resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz",
@ -464,6 +574,11 @@
"resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz",
"integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns="
},
"platform": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/platform/-/platform-1.3.5.tgz",
"integrity": "sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q=="
},
"proxy-addr": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.4.tgz",
@ -504,6 +619,11 @@
"unpipe": "1.0.0"
}
},
"referrer-policy": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/referrer-policy/-/referrer-policy-1.1.0.tgz",
"integrity": "sha1-NXdOtzW/UPtsB46DM0tHI1AgfXk="
},
"request": {
"version": "2.88.0",
"resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz",
@ -673,6 +793,11 @@
"core-util-is": "1.0.2",
"extsprintf": "^1.2.0"
}
},
"x-xss-protection": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/x-xss-protection/-/x-xss-protection-1.1.0.tgz",
"integrity": "sha512-rx3GzJlgEeZ08MIcDsU2vY2B1QEriUKJTSiNHHUIem6eg9pzVOr2TL3Y4Pd6TMAM5D5azGjcxqI62piITBDHVg=="
}
}
}

View File

@ -13,6 +13,7 @@
"body-parser": "^1.18.3",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"helmet": "^3.15.1",
"moment": "^2.23.0",
"request": "^2.88.0"
}