-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (144 loc) · 4.66 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
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
163
164
165
166
167
168
169
170
171
172
173
import dotenv from 'dotenv';
import fs from 'fs';
dotenv.config();
let cache = null;
const writeCache = (newCache) => {
cache = newCache;
fs.writeFileSync("cache.json", JSON.stringify(newCache, null, 4), "utf-8");
};
const resetCache = () => writeCache({
lastThreadIds: {
'CS107': []
},
lastNotificationIds: []
});
const courseIds = {
'CS107': '1526'
};
const discordWebhooks = {};
let firstRestart = true;
let disabled = false;
const edstemSynchronization = async () => {
if (disabled) return;
const lastThreadIds = cache.lastThreadIds;
let worked = false;
for (const [course, id] of Object.entries(courseIds)) {
console.log(course, id);
const threads = (await (await fetch(`https://eu.edstem.org/api/courses/${id}/threads?limit=5&sort=new&filter=unresolved`, {
headers: {
'X-Token': process.env.EDSTEM_TOKEN
}
})).json()).threads;
if (!threads) {
sendNotification({
title: `${course} Error`,
message: 'An error occurred while fetching the threads',
priority: 1
}, process.env[`${course}_GROUP_TOKEN`], course);
setInterval(() => {}, 1 << 30);
disabled = true;
break;
}
for (const thread of threads) {
worked = true;
if (!cache.lastThreadIds[course].includes(thread.id)) {
cache.lastThreadIds[course].push(thread.id);
sendNotification({
title: `${course} Question`,
message: thread.title,
url: `https://edstem.org/eu/courses/${id}/discussion/${thread.id}`,
url_title: 'Let\'s Edstem this question!',
priority: 0
}, process.env[`${course}_GROUP_TOKEN`], course);
}
}
}
if (disabled) return;
const notifications = (await (await fetch('https://notes.eu.edstem.org/api/browser/before?id=678580', {
headers: {
'X-Token': process.env.EDSTEM_TOKEN
}
})).json()).notes;
let lastNotification = notifications[0];
if (lastNotification.id !== cache.lastNotificationId) {
sendNotification({
title: `ED Notification`,
message: JSON.parse(lastNotification.body).thread.title,
url: `https://edstem.org/eu/courses/1101/discussion/${JSON.parse(lastNotification.body).thread.id}`,
url_title: 'Let\'s answer this guy!',
priority: 0
}, process.env.NOTIFICATIONS_GROUP_TOKEN);
}
writeCache({
...cache,
lastNotificationId: lastNotification.id,
lastThreadIds
});
if (firstRestart) {
firstRestart = false;
sendNotification({
title: `ED Notification`,
message: 'The bot has been restarted (worked=' + worked + ')',
priority: 0
}, process.env.NOTIFICATIONS_GROUP_TOKEN);
}
};
// Si le fichier cache n'existe pas, on le créé
if (!fs.existsSync("cache.json")) {
resetCache();
} else {
// S'il existe, on essaie de le parser et si ça échoue on le reset pour éviter les erreurs
try {
cache = JSON.parse(fs.readFileSync("cache.json", "utf-8"));
} catch {
resetCache();
}
}
const sendNotification = async (notification, groupToken, course) => {
if (firstRestart) {
return;
}
const appToken = process.env.APP_TOKEN;
const pushUrl = 'https://api.pushover.net/1/messages.json';
const response = await fetch(pushUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: appToken,
user: groupToken,
...notification
})
}).catch(() => {});
const content = await response.json();
console.log(content);
if (!course) return;
if (!discordWebhooks[course]) return;
await fetch(discordWebhooks[course], {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
embeds: [
{
title: notification.message,
url: notification.url,
color: 0x874fdb,
timestamp: new Date().toISOString(),
}
]
})
}).catch(() => {});
/*
if (content.status !== 1) {
throw new Error('Pushover API error');
}*/
}
edstemSynchronization();
setInterval(() => edstemSynchronization(), 60_000);
// catch all errors
process.on('uncaughtException', (err) => {
console.error(err);
});