-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (105 loc) · 3.5 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//Similar to include statements
//Var: detecting variable
//Let: Let this be a variable for(let i = 0;)
//Const: Constant variables
require("dotenv").config();
var axios = require("axios");
var cheerio = require("cheerio");
var express = require("express");
var schedule = require("node-schedule");
//Constant gif API URL
const giphyURL =
"https://api.giphy.com/v1/gifs/random?api_key=FcOuFsdxtb7cE0iatNEjFMQw1r4NmvHc&tag=animals&rating=pg-13";
//Function to automatically format Slack text to its API standards
const getBlock = text => ({
type: "section",
text: {
type: "mrkdwn",
text: text
}
});
//Function to send random gifs
const getGif = gif => ({
type: "image",
image_url: gif,
alt_text: "Random Gif"
});
//Async indicates promise function
const getNews = async () => {
console.log("Grabbing the top five news of the day");
//new variable waiting for a promise through "await", using axios.get to fetch the website
const response = await axios.get("http://theweek.com/5things");
console.log("got news, prepared to send to slack");
const $ = cheerio.load(response.data);
const newsItemsHTML = $(".five-things-item");
const newsItem = [];
console.log("Retrieving gif from giphy");
const gifData = await axios.get(giphyURL);
const gifID = gifData.data.data.id;
console.log(gifID);
const gifURL = "https://media.giphy.com/media/" + gifID + "/giphy.gif";
console.log(gifURL);
//if(dailyUpdate)
newsItem.push(
getBlock(
"Good evening Alpha Zeta! 🌤 Here's some daily news to keep you informed during the COVID pandemic!"
),
getGif(gifURL)
);
for (let i = 0; i < 5; i++) {
const headline = $(newsItemsHTML[i])
.find(".five-things-headline > a > p")
.text();
const description = $(newsItemsHTML[i])
.find(".five-things-text > p")
.text();
//${}allows you to insert content within a string, use ``to indicate that it's a string
newsItem.push(getBlock(`*${i + 1}. ${headline}*`));
newsItem.push(getBlock(description));
}
//* is slackbot documentation for BOLD
console.log("about to spam");
return newsItem;
};
//Schedule interval for the 18th hour of everyday
console.log("Setting interval...");
schedule.scheduleJob(" * * 18 * * *", async () => {
const news = await getNews().catch(e =>
console.log("Error trying to get the news:", e)
);
axios
.post(process.env.SLACK_WEBHOOK, {
//notifications
text: "Daily News Update! 📰",
blocks: news
})
.then(() => console.log("Suceessfully posted news to Slack"))
.catch(e => console.log("Error sending news to Slack wehook:", e));
});
//Express server
const server = express();
//The port is assigned to 5000, port is encrypted
const port = process.env.PORT || 5000;
//Set the port of the server to the port
server.set("port", port);
//To work with the server
server
//Another get request, so when the server is asked for /news,
//Create new variable 'news' that will
.get("/news", async (req, res) => {
const news = await getNews();
res.json(news);
})
//Get request, when the server 'gets' '/', server will respond "App is running"
//Get request is function with 2 parameters, what you are 'getting', and the function
.get("/", function(request, response) {
var result = "App is running";
response.send(result);
})
//Listen request, to indicate that the server is listening
.listen(server.get("port"), function() {
console.log(
"App is running, server is listening on port ",
server.get("port")
);
});