forked from cartabinaria/informabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
247 lines (236 loc) · 7.59 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Initial setup
if (process.argv.length != 3) console.log("usage: node index.js <token>");
process.env.NTBA_FIX_319 = 1;
const axios = require("axios"),
fs = require("fs"),
TelegramBot = require("node-telegram-bot-api"),
actions = require("./json/actions.json"),
groups = fs.existsSync("./json/groups.json")
? require("./json/groups.json")
: {},
memes = require("./json/memes.json"),
settings = require("./json/settings.json"),
bot = new TelegramBot(process.argv[2], { polling: true });
// String formatting via placeholders: has troubles with placeholders injections
String.format = function () {
let s = arguments[0].slice();
for (let i = 0; i < arguments.length - 1; ++i)
s = s.replace(new RegExp("\\{" + i + "\\}", "gm"), arguments[i + 1]);
return s;
};
// Returns a new Date object for tomorrow's Date
function tomorrowDate() {
const d = new Date();
d.setDate(d.getDate() + 1);
return d;
}
// Simple messages
function message(msg, text) {
bot
.sendMessage(msg.chat.id, text, settings.messageOptions)
.catch((e) => console.error(e.stack));
}
// Web scraping the timetable -- Lezioni oggi
function timetable(msg, url, date, title, fallbackText) {
axios
.get(url)
.then((res) => {
let lectures = [];
for (let i = 0; i < res.data.length; ++i) {
let start = new Date(res.data[i].start);
if (
start.getFullYear() === date.getFullYear() &&
start.getMonth() === date.getMonth() &&
start.getDate() === date.getDate()
)
lectures.push(res.data[i]);
}
let text = title;
lectures.sort((a, b) => a.start - b.start);
for (let i = 0; i < lectures.length; ++i)
text += ` 🕘 <b><a href="${lectures[i].teams}">${lectures[i].title}</a></b> ${lectures[i].time}
🏢 ${lectures[i].aule[0].des_edificio} - ${lectures[i].aule[0].des_piano}
📍 ${lectures[i].aule[0].des_indirizzo}
〰〰〰〰〰〰〰〰〰〰〰
`;
if (lectures.length !== 0) message(msg, text);
else message(msg, fallbackText);
})
.catch((e) => console.error(e.stack));
}
// Autogenerated courses info
function course(msg, name, virtuale, teams, website, professors) {
const emails = professors.join("@unibo.it\n ") + "@unibo.it";
/* convert a string into kebab case
* useful for GitHub repository
*
* example:
* string = "Logica per l'informatica"
* converted_string = toOurCase(string); = "logica-per-informatica" (sic!)
*/
const toOurCase = (str) =>
str &&
str
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "")
.match(
/(?:[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+)'?/g
)
.filter((value) => !value.endsWith("'"))
.map((x) => x.toLowerCase())
.join("-");
message(
msg,
`<b>${name}</b>
<a href='https://virtuale.unibo.it/course/view.php?id=${virtuale}'>Virtuale</a>
<a href='https://teams.microsoft.com/l/meetup-join/19%3ameeting_${teams}%40thread.v2/0?context=%7b%22Tid%22%3a%22e99647dc-1b08-454a-bf8c-699181b389ab%22%2c%22Oid%22%3a%22080683d2-51aa-4842-aa73-291a43203f71%22%7d'>Videolezione</a>
<a href='https://www.unibo.it/it/didattica/insegnamenti/insegnamento/${website}'>Sito</a>
<a href='https://www.unibo.it/it/didattica/insegnamenti/insegnamento/${website}/orariolezioni'>Orario</a>
${emails}
<a href='https://csunibo.github.io/${toOurCase(
name
)}/'>📚 Risorse: materiali, libri, prove</a>
<a href='https://github.com/csunibo/${toOurCase(
name
)}/'>📂 Repository GitHub delle risorse</a>`
);
}
// Adding a user to a list
function lookingFor(msg, singularText, pluralText, chatError) {
if (
(msg.chat.type !== "group" && msg.chat.type !== "supergroup") ||
settings.lookingForBlackList.includes(msg.chat.id)
)
message(msg, chatError);
else {
const chatId = msg.chat.id,
senderId = msg.from.id;
if (!(chatId in groups)) groups[chatId] = [];
const group = groups[chatId];
if (!group.includes(senderId)) group.push(senderId);
fs.writeFileSync("json/groups.json", JSON.stringify(groups));
const length = group.length.toString(),
promises = Array(length);
group.forEach((e, i) => {
promises[i] = bot
.getChatMember(chatId, e.toString())
.then(
(result) => {
const user = result.user;
return `👤 <a href='tg://user?id=${user.id}'>${user.first_name}${
user.last_name ? " " + user.last_name : ""
}</a>\n`;
},
(reason) => console.error(reason)
)
.catch((error) => console.error(error));
});
Promise.allSettled(promises).then((result) => {
let list = String.format(
length == "1" ? singularText : pluralText,
msg.chat.title,
length
);
result.forEach((e, i) => {
list +=
e.status === "fulfilled" && e.value
? e.value
: `👤 <a href='tg://user?id=${group[i]}'>??? ???</a>\n`;
});
message(msg, list);
});
}
}
// Removing a user from a list
function notLookingFor(msg, text, chatError, notFoundError) {
if (
(msg.chat.type !== "group" && msg.chat.type !== "supergroup") ||
settings.lookingForBlackList.includes(msg.chat.id)
)
message(msg, chatError);
else {
const chatId = msg.chat.id,
title = msg.chat.title;
if (!(chatId in groups)) message(msg, String.format(notFoundError, title));
else {
const group = groups[chatId],
senderId = msg.from.id;
if (!group.includes(senderId))
message(msg, String.format(notFoundError, title));
else {
group.splice(group.indexOf(senderId), 1);
if (group.length == 0) delete groups[chatId];
fs.writeFileSync("json/groups.json", JSON.stringify(groups));
message(msg, String.format(text, title));
}
}
}
}
// Send help message
function giveHelp(msg) {
answer = "";
for (command in actions)
if (actions[command] && actions[command].description)
answer += `/${command} - ${actions[command].description}\n`;
message(msg, answer);
}
// Available actions
function act(msg, action) {
switch (action.type) {
case "alias":
act(msg, actions[action.command]);
break;
case "course":
course(
msg,
action.name,
action.virtuale,
action.teams,
action.website,
action.professors
);
break;
case "help":
giveHelp(msg);
break;
case "lookingFor":
lookingFor(msg, action.singularText, action.pluralText, action.chatError);
break;
case "message":
message(msg, action.text);
break;
case "notLookingFor":
notLookingFor(msg, action.text, action.chatError, action.notFoundError);
break;
case "todayLectures":
timetable(msg, action.url, new Date(), action.title, action.fallbackText);
break;
case "tomorrowLectures":
timetable(
msg,
action.url,
tomorrowDate(),
action.title,
action.fallbackText
);
break;
default:
console.error(`Unknown action type "${action.type}"`);
}
}
// Parsing
function onMessage(msg) {
if (msg.text) {
const text = msg.text.toString();
if (text[0] == "/") {
// '/command@bot param0 ... paramN' -> 'command'
command = text.toLowerCase().split(" ")[0].substring(1);
if (command.includes("@"))
command = command.substring(0, command.indexOf("@"));
if (command in actions) act(msg, actions[command]);
else if (command in memes) message(msg, memes[command]);
}
}
}
bot.on("message", onMessage);
bot.on("polling_error", console.log);