-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (38 loc) · 1.33 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import bodyParser from 'body-parser';
import { fetchUrl } from 'fetch';
import express from 'express';
const app = express();
const slackToken = process.env.SLACK_TOKEN;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/define', (req, res) => {
const { text, token, user_name } = req.body;
if (!text) return res.sendStatus(400);
if (token !== slackToken) return res.sendStatus(401);
console.log(`${user_name} asked to define '${text}'`);
fetchUrl(`https://owlbot.info/api/v1/dictionary/${text}?format=json`, (error, meta, body) => {
const parsedBody = JSON.parse(body.toString()) || [];
if (!parsedBody.length) return res.sendStatus(400);
const attachments = parsedBody.map((definition, index) => {
// the person who wrote this api spelled 'definition' wrong :(
return {
title: `${text} (${definition.type})`,
title_link: `https://www.merriam-webster.com/dictionary/${text}`,
text: ': ' + definition.defenition,
footer: definition.example || '',
color: "#c33f47",
};
});
res.send({
response_type: 'in_channel',
attachments: attachments || null,
"ts": new Date(),
});
});
});
const port = process.env.PORT || 3700;
app.listen(port, () => {
console.info(`server running at localhost:${port}`);
});