-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
83 lines (77 loc) · 2.46 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const parser = require("./parse_news");
const utils = require("./utils");
const TABOOLA_API =
"https://us-central1-vision-migration.cloudfunctions.net/la_hacks_2019?market_code=0";
const app = express();
const categories = [
"all",
"politics",
"sports",
"society",
"business",
"technology",
"entertainment"
];
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/lahacks", async (req, res) => {
console.log("Received: " + req.body.Body);
let inbMsg = req.body.Body.toLowerCase().trim();
var stt = inbMsg.slice(0, 3);
if (categories.includes(inbMsg)) {
axios
.get(TABOOLA_API)
.then(async response => {
// console.log(response.data.buckets[0].report.rollups[0]);
const news = await parser.parse_buckets(
response.data.buckets,
inbMsg,
5
);
if (news == "") {
res.send(
`<Response><Message>No trending articles for ${inbMsg} in last 24 hours.</Message></Response>`
);
} else {
res.send(`<Response><Message>${news}</Message></Response>`);
}
})
.catch(error => {
console.log(error);
});
} else if (inbMsg == "ootl") {
axios
.get(TABOOLA_API)
.then(async response => {
const toptopic = utils.capital_letter(
response.data.buckets[0].report.rollups[0].name
);
const news = await parser.parse_news(
response.data.buckets[0],
"all",
1
);
res.send(
"<Response><Message>Welcome to OutOfTheLoop!\n" +
`\nTop trending topic: ${toptopic}\n\nTop trending article:\n${news}` +
"Text back a category to explore more:" +
"\n- Politics\n- Sports\n- Society\n- Business\n- Technology\n- Entertainment\n- All</Message></Response>"
);
})
.catch(error => {
console.log(error);
});
} else if (stt == "i'm" || stt == "im " || stt == "i a") {
res.send(
`<Response><Message>Hi,${inbMsg.slice(3)}. I'm dad</Message></Response>`
);
} else {
res.send(
'<Response><Message>Invalid category\n\nEither text "ootl" or a category:' +
"\n- Politics\n- Sports\n- Society\n- Business\n- Technology\n- Entertainment\n- All</Message></Response>"
);
}
});
app.listen(process.env.PORT || 8080, () => console.log("listening"));