import './dotenv-module';
import https from 'https';
import fetch from 'node-fetch';
import express from 'express';
import bodyParser from 'body-parser';
import moment from 'moment';
import { Strategies } from './draw';
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const strategies = new Strategies();
app.get('/', async (req, res) => {
const theStrategy = strategies.draw();
const theTree =
`
${theStrategy}
${theStrategy}
`;
res.write(theTree);
console.log('\non '+moment().format('dddd, MMMM Do YYYY, h:mma').toLowerCase()+' a card was drawn');
res.end();
});
app.get('/test', async (req, res) => {
const endpoint = 'https://slack.com/api/auth.test';
const payload = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': `Bearer ${process.env.BOTACCESSTOKEN}`,
},
};
const response = await fetch(endpoint, payload)
.then(res => res.json());
console.log(response);
res.end();
});
app.post('/', async (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);
const theStrategy = strategies.draw();
const endpoint = 'https://slack.com/api/chat.postMessage';
const payload = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Authorization': `Bearer ${process.env.BOTACCESSTOKEN}`,
},
body: JSON.stringify({
channel: channel,
text: theStrategy,
}),
};
const response = await fetch(endpoint, payload)
.then(res => res.json());
console.log(response);
if (!response.ok) console.log(response);
if (response.ok) console.log('\non '+moment().format('dddd, MMMM Do YYYY, h:mma').toLowerCase()+' a card was drawn from Slack');
return res.status(200).end();
}
});
app.listen(4242, () => {
console.log(`oblique strategies are being served`);
});