oblique-strategies/src/index.js

107 lines
3.0 KiB
JavaScript

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 =
`<!doctype html>
<html lang='en'>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='apple-touch-icon' href='./touch_icon.png'>
<title>
${theStrategy}
</title>
<meta charset='utf-8'>
</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'>
${theStrategy}
</div>
</body>
</html>`;
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`);
});