require('dotenv').config(); const https = require('https'); const request = require('request'); const express = require('express'); 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()); const output = fs.createWriteStream('./access.log', {flags: 'a'}); 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); } } app.get('/', (req, res) => { const theStrategy = strategies[ Math.floor(Math.random()*strategies.length) ]; const theTree = ` ${theStrategy.toLowerCase()}
${theStrategy.toLowerCase()}
`; res.write(theTree); logger.log('\non '+moment().format('dddd, MMMM Do YYYY, h:mma').toLowerCase()+' a card was drawn'); res.end(); }); app.post('/', (req, res, next) => { 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(); } }); app.listen(4242, () => { console.log('oblique strategies are being served') });