-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
162 lines (152 loc) ยท 4.11 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* Setting things up. */
const path = require("path"),
express = require("express"),
app = express(),
getDayOfYear = require("date-fns/get_day_of_year"),
Twit = require("twit"),
config = {
twitter: {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
}
},
T = new Twit(config.twitter);
app.use(express.static("public"));
const randomFromArray = (number, array) =>
array
.sort(() => 0.5 - Math.random())
.slice(0, number)
.join("");
const yearDay = getDayOfYear(new Date());
const days = [...Array(yearDay + 1).keys()];
// calculators
const saveForward = currentDay => currentDay * 1;
const saveBackward = currentDay => 365 - (currentDay - 1);
const saveForwardReducer = (accumulator, currentDay) =>
accumulator + saveForward(currentDay);
const saveBackwardReducer = (accumulator, currentDay) =>
accumulator + saveBackward(currentDay);
// forwards savings amounts
const saveForwardsAmountToday = (saveForward(yearDay) / 100).toFixed(2);
const saveForwardsAmountTotal = (days.reduce(saveForwardReducer) / 100).toFixed(
2
);
// backwards savings amounts
const saveBackwardsAmountToday = (saveBackward(yearDay) / 100).toFixed(2);
const saveBackwardsAmountTotal = (
days.reduce(saveBackwardReducer) / 100
).toFixed(2);
const excitingEmoji = () =>
randomFromArray(1, [
"๐",
"๐ฐ",
"๐ค",
"๐ธ",
"๐ฅ",
"๐",
"๐ฒ",
"๐ท",
"๐ฑ",
"๐ฅ"
]);
const savingEmoji = () =>
randomFromArray(3, [
"๐พ",
"๐",
"๐",
"๐บ",
"๐ป",
"โท",
"๐
",
"๐คถ",
"๐",
"๐จ",
"๐จ",
"๐",
"๐",
"๐ฅ",
"๐ถ",
"๐",
"๐ฑ",
"๐",
"๐ณ",
"โ๏ธ",
"๐ธ",
"๐ฑ",
"๐ป",
"๐ฅ",
"๐ธ",
"๐ป",
"๐น",
"๐",
"๐",
"๐",
"๐ท",
"๐ฐ",
"๐",
"๐",
"๐ฎ",
"๐ท",
"๐",
"๐",
"๐ญ",
"๐ด",
"๐ก"
]);
const buildStatus = (savingStrategy, amountToSaveToday, totalSaved) =>
`${savingStrategy}:\nSave ยฃ${amountToSaveToday} today. ยฃ${totalSaved} total saved this year! ${excitingEmoji()}\n\n${savingEmoji()} What are you saving for?`;
const postTweet = (status, httpResponse) => {
T.post("statuses/update", { status: status }, function(err, data, resp) {
if (err) {
httpResponse.sendStatus(500);
console.log("Error!");
console.log(err);
} else {
httpResponse.sendStatus(200);
}
});
};
/* Zapier hits /BOT_ENDPOINT every day at 8am to wake up this Twitter bot. */
app.all("/forward/" + process.env.BOT_ENDPOINT, function(request, response) {
console.log("forward", new Date(), yearDay, getDayOfYear(new Date()));
const forwardSavingStatus = buildStatus(
"Forward saving from ยฃ0.01๐",
saveForwardsAmountToday,
saveForwardsAmountTotal
);
postTweet(forwardSavingStatus, response);
});
/* Zapier hits /BOT_ENDPOINT every day at 8am to wake up this Twitter bot. */
app.all("/backward/" + process.env.BOT_ENDPOINT, function(request, response) {
console.log("backward", new Date(), yearDay, getDayOfYear(new Date()));
const backwardSavingStatus = buildStatus(
"Backward saving from ยฃ3.65๐",
saveBackwardsAmountToday,
saveBackwardsAmountTotal
);
if (saveBackwardsAmountToday > 0) {
postTweet(backwardSavingStatus, response);
}
});
app.all("/test/", function(request, response) {
const date = new Date(2020, 12, 31);
console.log("test", date, yearDay);
const forwardSavingStatus = buildStatus(
"Forward saving from ยฃ0.01๐",
saveForwardsAmountToday,
saveForwardsAmountTotal
);
const backwardSavingStatus = buildStatus(
"Backward saving from ยฃ3.65๐",
saveBackwardsAmountToday,
saveBackwardsAmountTotal
);
response.send(
"<p>" + forwardSavingStatus + "</p><p>" + backwardSavingStatus + "</p>"
);
});
var listener = app.listen(process.env.PORT, function() {
console.log("Your bot is running on port " + listener.address().port);
});