-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
502 lines (461 loc) · 22 KB
/
main.py
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/python3
import botogram
from bs4 import BeautifulSoup
from base64 import b64encode, b64decode
from time import gmtime, strftime, sleep
import requests
import pickle
import traceback
from class_file import *
import apikey
# bot.api.call("sendMessage", {"chat_id": chat.id, "text": 'Testo Del messaggio', "parse_mode": "HTML", "reply_markup": '{"keyboard": [["text": "Testo"]], "one_time_keyboard": false, "resize_keyboard": true}'})
bot = botogram.create(apikey.botKey)
bot.process_backlog = True
@bot.command('start')
def start_command(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
if students[user_index].password != '' and students[user_index].check_login():
msg = 'Ciao, ' + students[user_index].nome.replace('_', '\_') + ', avevi gia inserito i tuoi dati e sono stati caricati! Inizia a usare il bot!'
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": msg,
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
students[user_index].statusLogin = 0
else:
chat.send(
"Benvenuto " + students[
user_index].nome.replace('_', '\_') + "\nDa ora in poi quando troverò un nuovo voto sul registro ti invierò un messaggio\n\nSe il login dovesse fallire ridai il comando /start\n\nI tuoi dati sono al sicuro! Username e Password vengono criptati appena li inserisci\n\nNel caso qualche materia non venisse abbreviata contattami\n\nBot progettato da Giovanni Casari (@infopz)")
chat.send("Inserisci il tuo username del registro:")
students[user_index].statusLogin = 1
shared['user'] = students
@bot.command('load')
def load_command(chat, message, shared, bot):
# FIXME: guarda l'user ID, mai l'username
if message.sender.username == 'infopz':
load_data(shared)
students = shared['user']
n = 0
for student in students:
print(f"Load {n}: {student.nome} - {student.chat_id}")
n+=1
chat.send(f"Dati Caricati - Utenti: {len(students)}")
else:
chat.send("Solo @infopz e' autorizzato ad eseguire questo comando")
@bot.command('change')
def change_command(chat, message, shared, args):
students = shared['user']
user_index = shared['cUs']
if not args or len(args) != 2:
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": 'Utilizza questo comando se devi cambiare username e password memorizzati nel bot. \n`/change newUser newPassword`',
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
else:
msg = args[0] + 'aaaa'
# FIXME: ti prego togli sta cosa
us = b64encode(msg.encode('ascii')).decode('ascii') # "cripto" il nome utente non appena viene immessa
students[user_index].set_user(us)
pw = b64encode(args[1].encode('ascii')).decode('ascii') # "cripto" la password non appena viene immessa
students[user_index].set_pass(pw)
if students[user_index].check_login():
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id, "text": 'Dati di login corretti, puoi iniziare ad usare il bot!',
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
students[user_index].update_voti()
students[user_index].statusLogin = 0
log_write('CHANGE COMMAND: USER ' + students[user_index].nome, 'Cambio effettuato')
else:
chat.send('Dati di login non corretti')
chat.send('Immetti il tuo username')
students[user_index].statusLogin = 1
log_write('CHANGE COMMAND: USER ' + students[user_index].nome, 'Dati non corretti')
shared['user'] = students
save_data(shared)
@bot.command('timer')
def timer_command(chat, message, shared, bot):
if message.sender.username == 'infopz':
check_updates(bot, shared)
else:
chat.send("Solo @infopz e' autorizzato ad eseguire questo comando")
@bot.command('del')
def del_command(chat, message, shared, args):
if message.sender.username == 'infopz':
students = shared['user']
if args[0].isdigit():
user_id = int(args[0])
print(f"User {user_id} {students[user_id].nome} deleted")
chat.send("User "+user_id+" "+students[user_id].nome.replace('_', '\_')+" deleted")
del students[user_id]
shared['user'] = students
save_data(shared)
log_write('USER DELETED', f'User {user_id}')
else:
chat.send('Ehi, coglione, devi mandarmi un numero, non una stringa\nSacripante!')
else:
students = shared['user']
user_index = shared['sCu']
chat_id = students[user_index].chat_id
bot.api.call("sendMessage", {
"chat_id": chat_id,
"text": "Solo @infopz e' autorizzato ad eseguire questo comando",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
@bot.command('all')
def all_command(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
if message.sender.username != 'infopz':
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": "Solo @infopz e' autorizzato ad eseguire questo comando",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
return
# FIXME: se uno scrive /all@infopzbot qui muore tutto
m = "Nuova comunicazione! \n" + message.text[5:]
for i in students:
try:
bot.api.call("sendMessage", {
"chat_id": i.chat_id,
"text": m,
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
print("Comunicazione inviata a " + str(i.nome))
except Exception:
print("Error with sending the comunication")
@bot.command('help')
def help_command(chat, message):
m = "Ecco i comandi che puoi usare:\n" \
" /start - Fai partire il Bot\n" \
" /change - Cambia lo user e password memorizzati\n" \
" /help - Visualizza questo messaggio\n" \
"\n" \
"Per qualsiasi informazione o problema contatta @infopz"
bot.api.call("sendMessage", {
"chat_id": chat.chat_id,
"text": m,
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
def vote_command(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
set_typing(students[user_index].chat_id)
try:
students[user_index].update_voti(shared)
if shared['badReq']:
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": "Scusa se ci ho messo tanto ma il server del registro e' impallato",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
if len(students[user_index].voti) == 0:
msg = 'Non hai ancora nessun voto nel secondo quadrimestre'
else:
msg = students[user_index].voti_string()
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": msg,
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
except Exception as e:
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": "Errore nel login\n"
"Dai il comando /start e riprova\n"
"Nel caso il problema si dovesse ripresentare contatta @infopz",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
print(f"Error voteCommand - User {students[user_index].nome} {e}")
log_write(f"VOTE COMMAND: USER: {students[user_index].nome}", traceback.format_exc())
shared['user'] = students
save_data(shared)
def voti_materia(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
set_typing(students[user_index].chat_id)
try:
voti = students[user_index].voti_per_materia()
if len(voti) == 0:
msg = 'Non hai ancora nessun voto nel secondo quadrimestre'
else:
msg = "Ecco i tuoi Voti\n"
mat = ''
for i in voti:
if i.materia != mat:
msg += '\n'
mat = i.materia
msg += shorten_name(i.materia.upper()) + " - " + i.tipo + " - " + i.data + " - *" + i.voto + '*\n'
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id, "text": msg, "parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
except Exception as e:
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": "Errore nel login\nDai il comando /start e riprova\nNel caso il problema si dovesse ripresentare contatta @infopz",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
print(f"Error voteCommand - User {students[user_index].nome} -")
log_write(f"MATERIE COMMAND: USER: {students[user_index].nome}", traceback.format_exc())
def medie_command(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
set_typing(students[user_index].chat_id)
try:
m = students[user_index].find_averages()
msg = str()
if len(m) == 0:
msg = "Non hai ancora nessun voto nel secondo quadrimestre"
else:
for i in m:
msg += shorten_name(i.materia[1:]) + " - " + i.tipo + " - *" + i.voto + '*\n'
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": msg,
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
except Exception as e:
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": "Errore nel login\nDai il comando /start e riprova\nNel caso il problema si dovesse ripresentare contatta @infopz",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
print(f"Error voteCommand - User {students[user_index].nome} - {e}")
log_write(f"MEDIE COMMAND: USER: {students[user_index].nome}", traceback.format_exc())
def voti_primo_quadrimestre(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
try:
set_typing(students[user_index].chat_id)
voti = students[user_index].voti_primo_quadrimestre()
msg = "Ecco i tuoi voti del primo quadrimestre\n"
materia = ''
for voto in voti:
if voto.materia != materia:
msg += '\n'
materia = voto.materia
msg += f"{shorten_name(voto.materia.upper())} - {voto.tipo} - {voto.data} - *{voto.voto}*\n"
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": msg,
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
except Exception as e:
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id,
"text": "Errore nel login\n"
"Dai il comando /start e riprova\n"
"Nel caso il problema si dovesse ripresentare contatta @infopz",
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
print(f"Error voteCommand - User {students[user_index].nome} - {e}")
log_write(f"1 QUAD COMMAND: USER: {students[user_index].nome}", traceback.format_exc())
def start1(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
msg = message.text + 'aaaa'
if msg.isalnum():
# FIXME: pls
username = b64encode(msg.encode('ascii')).decode('ascii')
students[user_index].set_user(username)
students[user_index].statusLogin = 2
shared['user'] = students
chat.send('Ora inserisci la password')
log_write('START1: USER ' + students[user_index].nome)
else:
chat.send('Username non valido, riprova')
def start2(chat, message, shared):
students = shared['user']
user_index = shared['cUs']
if message.text.isprintable():
# FIXME: dai
password = b64encode(message.text.encode('ascii')).decode('ascii')
students[user_index].set_pass(password)
if students[user_index].check_login():
bot.api.call("sendMessage", {
"chat_id": students[user_index].chat_id, "text": 'Dati di login corretti, puoi iniziare ad usare il bot!',
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
students[user_index].update_voti(shared)
students[user_index].statusLogin = 0
else:
chat.send('Dati di login non corretti')
chat.send('Immetti il tuo username')
students[user_index].statusLogin = 1
shared['user'] = students
save_data(shared)
else:
chat.send('Password non valida, riprova')
def save_data(shared):
s = shared['user']
f = open('user.txt', 'wb')
pickle.dump(s, f)
f.close()
shared['user'] = s
def load_data(shared):
s = shared['user']
f = open('user.txt', 'rb')
s = pickle.load(f)
shared['user'] = s
print("LoadDati")
def sendManyFucks(bot, chat_id):
bot.api.call("sendMessage", {
"chat_id": chat_id, "text": 'Ciao,\n ho notato che con i dati da te immessi, il registro mi dice tutte le volte che sono sbagliati.\nNel caso tu abbia cambiato la password, utilizza il comando /change per cambiare anche quella memorizzata dal bot, in caso contrario, non preoccuparti, sara uno strano suo errore.\nSe ti dovesse arrivare anche domani questo avviso, contatta @infopz che risolvera il problema',
"parse_mode": "Markdown",
"reply_markup": '{"keyboard": [[{"text": "Voti per materia"}, {"text":"Voti per data"}], [{"text": "Medie"}, {"text": "Voti 1°Quad"}]], "one_time_keyboard": false, "resize_keyboard": true}'
})
@bot.timer(7200) #FIXME la lista copiata
def check_updates(bot, shared):
badLogin = shared['badLogin']
hour = int(strftime("%H", gmtime())) + 1
if hour >= 20 or hour <= 5:
print(f"NO Time {hour}")
return
if hour <= 9:
badLogin = dict() #reset al mattino
print("Time " + str(hour))
students = shared['user']
if shared['reverseTimer']: # reverso la lista cosi caronte non mi risponde sempre su quei 5 utenti
students.reverse()
log_write(f"Timer Start {str(shared['reverseTimer'])}")
number_badRes = 0
for i, student in enumerate(students):
try:
old_voti = student.voti
student.update_voti(shared)
if shared['badReq']:
if student.chat_id in badLogin: #registro a quali utenti da CaronteFuck
badLogin[student.chat_id] += 1
else:
badLogin[student.chat_id] = 1
number_badRes += 1
continue
new_voti = voti_diff(old_voti, student.voti)
if len(new_voti) > 0:
print(f"NewVotesFound {student.nome} {len(new_voti)}")
msg = "Ehi, "+student.nome.replace('_', '\_')+", hai dei nuovi voti sul registro:\n"
for voto in new_voti:
msg += f"Hai preso *{voto.voto}* in {voto.materia} {voto.tipo}\n"
bot.chat(student.chat_id).send(msg)
except Exception as e:
print(f"Error NewVoti - User {student.nome} {e}")
log_write('TIMER ERROR: USER ' + student.nome, traceback.format_exc())
if i % 5 == 4: # per non sovraccaricare il server
sleep(30)
sleep(2)
if number_badRes != 0:
bot.chat(20403805).send(f'Salve Smilzo, {str(number_badRes)} Bad Response da caronte')
if shared['reverseTimer']:
students.reverse()
shared['reverseTimer'] = not shared['reverseTimer']
if hour >= 18: #controllo all'ultimo timer
for i in badLogin:
if badLogin[i]>=5:
sendManyFucks(bot, i)
for j in students: #stampa il nome dell'user
if i == j.chat_id:
print(f'ManyFuck sent to {j.nome}')
log_write(f'MANYFUCKS SENT TO {j.nome}')
break
else:
print(f'ManyFuck sent to {i}')
log_write(f'MANYFUCKS SENT TO {i}')
shared['user'] = students
if not shared['firstTimer']:
save_data(shared)
log_write("Timer End")
shared['firstTimer'] = False
shared['badLogin'] = badLogin
@bot.timer(300)
def reset_blocked(shared):
b = shared['lock']
if b:
print(f'User {str(b)} Unlocked')
b = list()
shared['lock'] = b
@bot.timer(5)
def reset_control_number(shared):
shared['maxMess']=0
@bot.before_processing
def before_processing(chat, message, shared):
b = shared['lock']
if chat.id in b:
return True
if message.sender.username is not None:
name = message.sender.username
else:
name = message.sender.first_name
print("Message from: " + name)
students = shared['user']
user_index = shared['cUs']
shared['maxMess'] += 1
if shared['maxMess'] > 4:
chat.send('Hai mandato troppi messaggi, sei stato bloccato per un po di tempo')
b = shared['lock']
b.append(chat.id)
shared['lock'] = b
print(f'Too Many Messages - User {name} Blocked')
shared['maxMess'] = 0
return True
if message.text != '/load':
if chat.id != students[user_index].chat_id:
for student in range(0, len(students)):
if chat.id == students[student].chat_id:
user_index = student
break
else: # inserimento nuovo current user
new_user = Utente(chat.id, name)
print('NewUser: ' + new_user.nome)
students.append(new_user)
user_index = len(students) - 1
log_write('NEW USER ' + new_user.nome)
if message.text != '/start': #nuovo user che scrive un messaggio alla cazzo, lo rimanda a start
start_command(chat, message, shared)
shared['user'] = students
shared['cUs'] = user_index
if students[user_index].statusLogin == 1:
start1(chat, message, shared)
elif students[user_index].statusLogin == 2:
start2(chat, message, shared)
elif message.text == 'Voti per materia':
voti_materia(chat, message, shared)
elif message.text == 'Voti per data':
vote_command(chat, message, shared)
elif message.text == 'Medie':
medie_command(chat, message, shared)
elif message.text == 'Voti 1°Quad':
voti_primo_quadrimestre(chat, message, shared)
@bot.prepare_memory
def prepare_memory(shared):
shared['firstTimer'] = True
shared['cUs'] = 0
shared['user'] = list()
shared['maxMess'] = 0
shared['badReq'] = list()
shared['lock'] = list()
shared['reverseTimer'] = False
shared['badLogin']=dict()
load_data(shared)
print("User Loaded")
if __name__ == "__main__":
bot.run()