-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
478 lines (423 loc) · 14.4 KB
/
bot.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
const token = process.env.TOKEN;
const Bot = require('node-telegram-bot-api')
const rp = require('request-promise')
const cheerio = require('cheerio')
const fs = require('fs')
const mongoose = require('mongoose')
const Subscriber = require('./model/Subscriber')
const TelegramError = require('./Handlers/TelegramError')
const TelegramResolve = require('./Handlers/TelegramResolve')
// Todo remove it
const json = 'responses.json'
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true });
const db = mongoose.connection;
const minutes = (minutes) => ((1000 * 60) * minutes)
const bot = new Bot(token, { polling: true });
let chatid;
db.on('error', console.error.bind(console, 'connection error: '));
db.once('open', function () {
// Connected
})
/**
*
* Riceve come valore l'istanza TelegramResolve.
* Al momento handleError fa la stessa cosa però ho preferito dividerle per un futuro handler migliore.
* @param new TelegramResolve()
*/
const handleSuccesses = (success) => {
if (success.type === 'telegram-success-handler') {
console.log(success);
bot.sendMessage(chatid, success.message);
}
}
/**
*
* Riceve come valore l'istanza TelegramResolve.
* Al momento handleSuccess fa la stessa cosa però ho preferito dividerle per un futuro handler migliore.
* @param new TelegramError()
*/
const handleError = (error) => {
if (error.type === 'telegram-error-handler') {
console.log(error);
bot.sendMessage(chatid, error.message);
}
}
const checkForUpdates = async () => {
let currentSubscriberIndex = 0;
const subscribers = await Subscriber.find({}, (err, result) => {
return result;
});
/**
*
* Funzione per gestire gli abbonati al bot.
*
* 1. Esegue un loop di tutti gli abbonati. Salva subito l'id e inizia a fetchare ogni risorsa.
* 2. Prende tutte le risorse e controlla quali sono nuove.
* 3. Se ne esiste almeno una nuova invia il messaggio all'utente.
*
* @TODO rimuovere il messaggi di avviso quando non c'è nessuna nuova news.
* @param object subscriber
*/
const handleSubscriber = async (subscriber, currentSubIndex) => {
/**
* Costante che definisce l'index del subscriber corrente.
*/
const currentSubscriberIndex = currentSubIndex;
/**
* Costante che definisce l'id della chat del subscriber.
*/
const chatid = subscriber.telegramID;
/**
* Variabile che segnala l'array del topic corrente.
*/
let currentTopicIndex = -1;
/**
* Variabile che contiene tutto l'html della pagina di Subito.it
*/
let html = '';
/**
*
* Funzione per effettuare lo scraping degli elementi di subito.it
*
* Banale, non c'è bisogno di spiegarla.
* @param document html
* @param int limit
*
* @return array of objects
*/
const fetchFromSubito = (html, limit = 5) => {
const $ = cheerio.load(html)
const itemsResponse = $('.items__item.item-card');
const items = [];
let count = 0;
$(itemsResponse).each((i, elem) => {
if ($(elem).find('.index-module_sbt-text-atom__ifYVU').length) {
if (count < limit) {
const url = $(elem).find('.SmallCard-module_link__hOkzY').attr('href');
const id = url.substring(url.lastIndexOf('-') + 1, url.indexOf('.htm'));
const item = {
id: id,
url: url,
title: $(elem).find('h2.index-module_sbt-text-atom__ifYVU').text(),
image: $(elem).find('.CardImage-module_photo__WMsiO').attr('src'),
location: $(elem).find('.PostingTimeAndPlace-module_date-location__1Owcv').text(),
price: $(elem).find('.index-module_price__N7M2x').text() || false
}
items.push(item);
count++;
}
}
})
return items;
}
/**
*
* Funzione che definisce quali sono i nuovi item nella ricerca di Subito.it
*
* 1. Prende l'array contenente tutti gli item già visionati prima.
* 2. Controlla che quelli appena presi dall'HTML di Subito non siano
* già stati salvati prima d'ora.
* 3. Ritorna la parte dell'array che ancora non è stato segnalato. Se l'array torna vuoto
* vuol dire che gli elementi non sono ancora stati aggiornati.
*
* @param array items
*
* @return array newestItems
*/
const grabNewestArticles = (items) => {
const savedIds = subscriber.topic[currentTopicIndex].items;
let indexOfLastNewest = -1;
for (let j = 0; j < items.length; j++) {
const item = items[j];
for (let i = 0; i < savedIds.length; i++) {
const { id } = savedIds[i];
if (item.id === id) {
indexOfLastNewest = j;
break;
}
}
if (indexOfLastNewest !== -1) {
break;
}
}
if (indexOfLastNewest !== -1) {
return items.splice(0, indexOfLastNewest);
} else {
return items;
}
}
/**
*
* Funzione finale. Salva l'array di items.
*
* @param array newestItems
*/
const saveSentItems = (newestItems) => {
subscriber.topic[currentTopicIndex].items = newestItems;
subscriber.save();
}
/**
*
* Invia per messaggio i nuovi items.
*
* Non ritorna niente ma al termine fa partire una seconda funzione
* che salva gli items appena inviati.
*
* @param array newestItems
*/
const sendMessages = (newestItems) => {
for (item of newestItems) {
var options = {
caption: `${item.title}\n\n${item.location.replace(' - ', '\n')}\n\n${item.price}`,
reply_markup: JSON.stringify({
inline_keyboard: [
[{ text: "Vedi l'articolo", url: item.url }]
]
})
};
bot.sendPhoto(chatid, item.image, options)
.catch(err => {
console.error('NEW ERROR:', err);
})
}
saveSentItems(newestItems);
}
for (topic of subscriber.topic) {
const { url } = topic;
html = await rp(url);
currentTopicIndex = currentTopicIndex + 1;
const items = fetchFromSubito(html);
if (!items.length)
return;
const newestItems = grabNewestArticles(items);
if (newestItems.length > 0) {
sendMessages(newestItems);
} else {
//bot.sendMessage(chatid, `Nessun nuovo articolo per il topic: ${topic.url}`);
}
}
}
for (subscriber of subscribers) {
handleSubscriber(subscriber, currentSubscriberIndex);
currentSubscriberIndex++;
}
}
/**
* Controlla se l'utente è un nuovo utente.
* In caso non lo sia prova ad aggiungere all'utente corrente il topic scelto.
*/
const subscribeToBot = (subito_url) => {
const subscribeNewUser = new Promise((resolve, reject) => {
Subscriber.findOne({ telegramID: chatid }, (error, result) => {
if (!result) {
// Nessun utente trovato. Aggiungerlo.
const subscriber = new Subscriber({ telegramID: chatid });
subscriber.topic.push({ url: subito_url, item: [] });
subscriber.save();
resolve(new TelegramResolve(0));
} else {
// Utente trovato. Aggiungere il topic.
const succesfull = addNewTopicToUser(result, subito_url);
if (succesfull) {
resolve(new TelegramResolve(1))
} else {
reject(new TelegramError(1))
}
}
})
})
subscribeNewUser
.then(response => {
handleSuccesses(response);
})
.catch(error => {
handleError(error);
})
}
/**
*
* L'utente esiste già nel database, quindi aggiungo il topic alla sua lista di interessi.
* In caso il topic sia già presente ritorno false per gestire l'errore.
*/
const addNewTopicToUser = (user, subito_url) => {
const equalTopic = user.topic.filter(t => t.url === subito_url);
if (equalTopic.length > 0) {
return false;
} else {
user.topic.push({ url: subito_url, item: [] });
user.save();
return true;
}
}
/**
*
* Se l'utente esiste faccio una lista di tutti i suoi topic.
*/
const listSubscriptions = () => {
const response = new Promise((resolve, reject) => {
Subscriber.findOne({ telegramID: chatid }, (error, result) => {
if (result) {
let message = '';
let index = 1;
if (result.topic.length > 0) {
for (topic of result.topic) {
message += `${index}. ${topic.url} \n\n`
index++;
}
}
resolve(new TelegramResolve(2, message));
} else {
reject(new TelegramError(0));
}
})
})
response
.then(message => handleSuccesses(message))
.catch(error => handleError(error))
}
/**
*
* L'utente ha richiesto di rimuovere un topic
*/
const unsubscribeFromBot = (subito_url) => {
const response = new Promise((resolve, reject) => {
Subscriber.findOne({ telegramID: chatid }, (error, result) => {
if (result) {
const oldLength = result.topic.length;
result.topic = result.topic.filter(t => t.url !== subito_url);
console.log(oldLength + '!=' + result.topic.length);
if (result.topic.length != oldLength) {
// Topic rimosso correttamente
result.save();
resolve(new TelegramResolve(1))
} else {
reject(new TelegramError(2))
}
} else {
reject(new TelegramError(0))
}
})
})
response
.then(success => handleSuccesses(success))
.catch(error => handleError(error))
}
bot.on("polling_error", (err) => console.log(err));
const commands = {
help: () => {
const helpMessage = `
<b>Lista comandi disponibili</b>
/start subito.it/url - Traccia i nuovi annunci.
/list - Lista di tutte le sottoscrizioni.
`;
bot.sendMessage(dedent(helpMessage), {
parse_mode: "HTML"
});
},
start: (msg) => {
const url = msg.text.toLowerCase().replace('/start ', '');
if (validURL(url) && url.indexOf('subito.it') !== -1) {
subscribeToBot(url);
} else {
// TODO: aggiungi un valido messaggio di risposta. Spiegazione di come compilare correttamente il messaggio di start
bot.sendMessage(chatid, 'Comando start errato');
}
},
list: () => {
listSubscriptions();
},
cancel: (msg) => {
const url = msg.text.toLowerCase().replace('/cancel ', '');
if (validURL(url) && url.indexOf('subito.it') !== -1) {
unsubscribeFromBot(url)
} else {
// TODO: aggiungi un valido messaggio di risposta. Spiegazione di come compilare correttamente il messaggio di rimuovi
bot.sendMessage(chatid, 'Comando rimuovi errato')
}
}
}
bot.on('message', (msg) => {
/**
*
* start - Iscriviti a un topic
* list - Lista di tutti i topic a cui sei iscritto
* cancel - Disiscriviti da un topic
*
*/
chatid = msg.chat.id
/**
* HELP COMMAND
*
* Return every other commands available
*/
if (isValidCommand(msg, '/help')) {
commands.help(chatid);
return true;
}
/**
* START COMMAND
*
* Iscrive un utente
*/
if (isValidCommand(msg, '/start')) {
commands.start(msg);
return true;
}
if (isValidCommand(msg, '/list')) {
commands.list();
return true;
}
/**
* CANCEL COMMAND
*
* Disiscrive un utente dalla lista.
*/
if (isValidCommand(msg, '/cancel')) {
commands.cancel(msg)
return true;
}
/**
* DEFAULT COMMAND
*
* Dato il return messo in ogni listener del comando. Se arrivo fino a qui vuol dire che nessun comando dato era valido. Invio il comando HELP.
*/
commands.help();
return true;
});
const isValidCommand = (msg, command) => {
if (msg.text.toLowerCase().indexOf(command) === 0) {
return true;
} else {
return false;
}
}
setInterval(checkForUpdates, minutes(5));
const validURL = (str) => {
var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
return !!pattern.test(str);
}
function dedent(callSite, ...args) {
function format(str) {
let size = -1;
return str.replace(/\n(\s+)/g, (m, m1) => {
if (size < 0)
size = m1.replace(/\t/g, " ").length;
return "\n" + m1.slice(Math.min(m1.length, size));
});
}
if (typeof callSite === "string")
return format(callSite);
if (typeof callSite === "function")
return (...args) => format(callSite(...args));
let output = callSite
.slice(0, args.length + 1)
.map((text, i) => (i === 0 ? "" : args[i - 1]) + text)
.join("");
return format(output);
}
module.exports = bot;