forked from UNICT-DMI/Telegram-DMI-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
executable file
·883 lines (692 loc) · 39.1 KB
/
functions.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# -*- coding: utf-8 -*-
# Telegram
import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, Bot
from telegram.ext import Updater, Filters, MessageHandler, CommandHandler, CallbackQueryHandler, RegexHandler, CallbackContext
from telegram.error import (TelegramError, Unauthorized, BadRequest, TimedOut, ChatMigrated, NetworkError)
# Drive
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
# Custom classes
from classes.EasterEgg import EasterEgg
from classes.StringParser import StringParser
# System libraries
from datetime import date, datetime, timedelta
import json
import datetime
import re
import random
import os
import sys
import requests
import sqlite3
import yaml
import logging
from urllib.request import urlopen
from bs4 import BeautifulSoup
from module.lezioni import lezioni_cmd
from module.esami import esami_cmd
from module.professori import prof_cmd
from module.scraper_exams import scrape_exams
from module.scraper_lessons import scrape_lessons
from module.scraper_professors import scrape_prof
from module.scraper_notices import scrape_notices
from module.gitlab import gitlab_handler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
# config
with open('config/settings.yaml', 'r') as yaml_config:
config_map = yaml.load(yaml_config, Loader=yaml.SafeLoader)
# Token of your telegram bot that you created from @BotFather, write it on settings.yml
TOKEN = config_map["token"]
news = ""
def send_message(update: Update, context: CallbackContext, messaggio):
msg = ""
righe = messaggio.split('\n')
for riga in righe:
if riga.strip() == "" and len(msg) > 3000:
context.bot.sendMessage(chat_id=update.message.chat_id, text=msg, parse_mode='Markdown')
msg = ""
else:
msg += riga + "\n"
context.bot.sendMessage(chat_id=update.message.chat_id, text=msg, parse_mode='Markdown')
def lezioni(update: Update, context: CallbackContext, *m):
check_log(update, context, "lezioni")
message_text = lezioni_cmd(update, context, context.args)
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text, parse_mode='Markdown')
def esami(update: Update, context: CallbackContext):
check_log(update, context, "esami")
message_text = esami_cmd(context.args)
if len(message_text) > 4096:
send_message(update, context, message_text)
else:
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text, parse_mode='Markdown')
def forum(sezione):
response = urlopen("http://forum.informatica.unict.it/")
html_doc = response.read()
s = BeautifulSoup(html_doc, 'html.parser')
s.prettify()
dictionary = {}
for range_limit, main_table in enumerate(s.findAll("div", class_="tborder")):
if(range_limit >= 3): # If che limita le sezioni a quelle interessate, evitando di stampare sottosezioni come "News" della categoria "Software"
break
for td_of_table in main_table.findAll("td", class_="windowbg3"):
for span_under in td_of_table.findAll("span", class_="smalltext"):
for anchor_tags in span_under.find_all('a'):
anchor_tags_splitted = anchor_tags.string.split(",")
anchor_tags_without_cfu = StringParser.remove_cfu(anchor_tags_splitted[0])
if(sezione == anchor_tags_without_cfu.lower()):
dictionary[anchor_tags_without_cfu.lower()] = anchor_tags['href']
return dictionary
return False # Redefine with @Veeenz API
# Commands
CUSicon = {0: "🏋",
1: "⚽️",
2: "🏀",
3: "🏈",
4: "🏐",
5: "🏊",
}
def help_cmd():
output = "@DMI_Bot risponde ai seguenti comandi: \n\n"
output += "📖 /esami - linka il calendario degli esami\n"
output += "🗓 /aulario - linka l\'aulario\n"
output += "👔 /prof <nome> - es. /prof Barbanera\n"
output += "👥 /rappresentanti - elenco dei rappresentanti del DMI\n"
output += "📚 /biblioteca - orario biblioteca DMI\n"
output += CUSicon[random.randint(0, 5)] + " /cus sede e contatti\n"
output += " /cloud - linka le cartelle condivise su cloud\n\n"
output += "Segreteria orari e contatti:\n"
output += "/sdidattica - segreteria didattica\n"
output += "/sstudenti - segreteria studenti\n"
output += "/cea - CEA\n"
output += "\nERSU orari e contatti\n"
output += "/ersu - sede centrale\n"
output += "/ufficioersu - (ufficio tesserini)\n"
output += "/urp - URP studenti\n\n"
output += "~Bot~\n"
output += "📂 /drive - accedi a drive\n"
output += "📂 /git - /gitlab - accedi a gitlab\n"
output += "/disablenews \n"
output += "/enablenews\n"
output += "/contributors"
return output
def read_md(namefile):
in_file = open("data/markdown/" + namefile + ".md", "r")
text = in_file.read()
in_file.close()
return text
def informative_callback(update: Update, context: CallbackContext, cmd):
check_log(update, context, cmd)
message_text = read_md(cmd)
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text, parse_mode='Markdown')
def exit_cmd():
output = "."
return output
def esami_button():
output = "Scrivi /esami inserendo almeno uno dei seguenti parametri: giorno, materia, sessione (prima, seconda, terza, straordinaria)"
return output
def lezioni_button():
output = "Scrivi /lezioni inserendo almeno uno dei seguenti parametri: giorno, materia"
return output
def mesami_url():
url = "http://web.dmi.unict.it/Didattica/Laurea%20Magistrale%20in%20Informatica%20LM-18/Calendario%20degli%20Esami"
return url
def aulario_url():
url = 'http://aule.dmi.unict.it/aulario/roschedule.php'
return url
# Easter egg
def prof_sticker_id():
db = sqlite3.connect('data/DMI_DB.db')
i = db.execute("SELECT * FROM 'stickers' ORDER BY RANDOM() LIMIT 1").fetchone()[0]
db.close()
return i
def forum_cmd(text):
text = text.replace("/forum ", "")
dict_url_sezioni = forum(text)
if not (dict_url_sezioni == False):
for titoli in dict_url_sezioni:
output = StringParser.starts_with_upper(titoli) + ": " + str(dict_url_sezioni[titoli])
else:
output = "La sezione non e' stata trovata."
return output
def callback(update: Update, context: CallbackContext):
conn = sqlite3.connect('data/DMI_DB.db')
keyboard2 = [[]]
icona = ""
number_row = 0
number_array = 0
update.callback_query.data = update.callback_query.data.replace("Drive_", "")
if len(update.callback_query.data) < 13: # "Accetta" (/request command)
array_value = update['callback_query']['message']['text'].split(" ")
try:
if len(array_value) == 5:
conn.execute("INSERT INTO 'Chat_id_List' VALUES ("+update.callback_query.data+",'" + array_value[4] + "','" + array_value[1] + "','" + array_value[2] + "','" + array_value[3] + "') ")
context.bot.sendMessage(chat_id=update.callback_query.data, text="🔓 La tua richiesta è stata accettata. Leggi il file README")
context.bot.sendDocument(chat_id=update.callback_query.data, document=open('data/README.pdf', 'rb'))
request_elimination_text = "Richiesta di " + str(array_value[1]) + " " + str(array_value[2]) + " estinta"
context.bot.editMessageText(text=request_elimination_text, chat_id=config_map['dev_group_chatid'], message_id=update.callback_query.message.message_id)
context.bot.sendMessage(chat_id=config_map['dev_group_chatid'], text=str(array_value[1]) + " " + str(array_value[2] + str(" è stato inserito nel database")))
elif len(array_value) == 4:
conn.execute("INSERT INTO 'Chat_id_List'('Chat_id','Nome','Cognome','Email') VALUES (" + update.callback_query.data + ",'" + array_value[1] + "','" + array_value[2] + "','" + array_value[3] + "')")
context.bot.sendMessage(chat_id=update.callback_query.data, text="🔓 La tua richiesta è stata accettata. Leggi il file README")
context.bot.sendDocument(chat_id=update.callback_query.data, document=open('data/README.pdf', 'rb'))
request_elimination_text = "Richiesta di " + str(array_value[1]) + " " + str(array_value[2]) + " estinta"
context.bot.editMessageText(text=request_elimination_text, chat_id=config_map['dev_group_chatid'], message_id=update.callback_query.message.message_id)
else:
context.bot.sendMessage(chat_id=config_map['dev_group_chatid'], text=str("ERRORE INSERIMENTO: ") + str(update['callback_query']['message']['text']) + " " + str(update['callback_query']['data']))
conn.commit()
except Exception as error:
print(error)
context.bot.sendMessage(chat_id=config_map['dev_group_chatid'], text=str("ERRORE INSERIMENTO: ") + str(update['callback_query']['message']['text']) + " " + str(update['callback_query']['data']))
text = ""
else:
pid = os.fork()
if (pid == 0):
settings_file = "config/settings.yaml"
gauth2 = GoogleAuth(settings_file=settings_file)
gauth2.CommandLineAuth()
# gauth2.LocalWebserverAuth()
drive2 = GoogleDrive(gauth2)
bot2 = telegram.Bot(TOKEN)
file1 = drive2.CreateFile({'id': update.callback_query.data})
file1.FetchMetadata()
if file1['mimeType'] == "application/vnd.google-apps.folder":
file_list2 = None
try:
istance_file = drive2.ListFile({'q': "'"+file1['id']+"' in parents and trashed=false", 'orderBy': 'folder,title'})
file_list2 = istance_file.GetList()
with open("./logs/debugDrive.txt", "a") as debugfile:
debugfile.write("- Log time:\n {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
debugfile.write("- File:\n {}".format(str(json.dumps(file1))))
debugfile.write("- IstanceFile:\n {}".format(str(json.dumps(istance_file))))
debugfile.write("- FileList:\n {}".format(str(json.dumps(file_list2))))
debugfile.write("\n------------\n")
except Exception as e:
with open("./logs/debugDrive.txt", "a") as debugfile:
debugfile.write("- Log time:\n {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
debugfile.write("- Error:\n {}".format(e))
debugfile.write("\n------------\n")
print("- Drive error: {}".format(e))
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text="Si è verificato un errore, ci scusiamo per il disagio. Contatta i devs. /help")
sys.exit(0)
formats = {
** { "pdf" : "📕 " },
** dict.fromkeys([' a', 'b', 'c'], 10),
** dict.fromkeys(["doc", "docx", "txt"], "📘 "),
** dict.fromkeys(["jpg", "png", "gif"], "📷 "),
** dict.fromkeys(["rar", "zip"], "🗄 "),
** dict.fromkeys(["out", "exe"], "⚙ "),
** dict.fromkeys(["c", "cpp", "h", "py", "java", "js", "html", "php"], "💻 ")
}
for file2 in file_list2:
file2.FetchMetadata()
if file2['mimeType'] == "application/vnd.google-apps.folder":
if number_row >= 1:
keyboard2.append([InlineKeyboardButton("🗂 "+file2['title'], callback_data="Drive_" + file2['id'])])
number_row = 0
number_array += 1
else:
keyboard2[number_array].append(InlineKeyboardButton("🗂 "+file2['title'], callback_data="Drive_" + file2['id']))
number_row += 1
else:
file_format = file2['title'][-5:] # get last 5 characters of strings
file_format = file_format.split(".") # split file_format per "."
file_format = file_format[len(file_format)-1] # get last element of file_format
icona = "📄 "
if file_format in formats.keys():
icona = formats[file_format]
if number_row >= 1:
keyboard2.append([InlineKeyboardButton(icona+file2['title'], callback_data="Drive_" + file2['id'])])
number_row = 0
number_array += 1
else:
keyboard2[number_array].append(InlineKeyboardButton(icona+file2['title'], callback_data="Drive_" + file2['id']))
number_row += 1
if len(file1['parents']) > 0 and file1['parents'][0]['id'] != '0ADXK_Yx5406vUk9PVA':
keyboard2.append([InlineKeyboardButton("🔙", callback_data="Drive_" + file1['parents'][0]['id'])])
reply_markup3 = InlineKeyboardMarkup(keyboard2)
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text=file1['title']+":", reply_markup=reply_markup3)
elif file1['mimeType'] == "application/vnd.google-apps.document":
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text="Impossibile scaricare questo file poichè esso è un google document, Andare sul seguente link")
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text=file1['exportLinks']['application/pdf'])
else:
try:
file_d = drive2.CreateFile({'id': file1['id']})
file_d.FetchMetadata()
if int(file_d['fileSize']) < 5e+7:
file_d.GetContentFile('file/'+file1['title'])
file_s = file1['title']
filex = open(str("file/" + file_s), "rb")
bot2.sendChatAction(chat_id=update['callback_query']['from_user']['id'], action="UPLOAD_DOCUMENT")
bot2.sendDocument(chat_id=update['callback_query']['from_user']['id'], document=filex)
os.remove(str("file/" + file_s))
else:
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text="File troppo grande per il download diretto, scarica dal seguente link")
# file_d['downloadUrl']
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text=file_d['alternateLink'])
except Exception as e:
print("- Drive error: {}".format(e))
bot2.sendMessage(chat_id=update['callback_query']['from_user']['id'], text="Impossibile scaricare questo file, contattare gli sviluppatori del bot")
open("logs/errors.txt", "a+").write(str(e) + str(file_d['title'])+"\n")
sys.exit(0)
os.waitpid(pid, 0)
conn.close()
def request(update: Update, context: CallbackContext):
conn = sqlite3.connect('data/DMI_DB.db')
chat_id = update.message.chat_id
if chat_id > 0:
# if we do not find any chat_id in the db
if not conn.execute("SELECT Chat_id FROM Chat_id_List WHERE Chat_id = " + str(chat_id)).fetchone():
message_text = "✉️ Richiesta inviata"
keyboard = [[]]
username = ""
if update['message']['from_user']['username']:
username = update['message']['from_user']['username']
update.message.text = re.sub('<|>', '', update.message.text)
if len(update.message.text.split(" ")) == 4 and "@" in update.message.text.split(" ")[3] and "." in update.message.text.split()[3]:
text_send = str(update.message.text) + " " + username
keyboard.append([InlineKeyboardButton("Accetta", callback_data="Drive_"+str(chat_id))])
reply_markup2 = InlineKeyboardMarkup(keyboard)
context.bot.sendMessage(chat_id=config_map['dev_group_chatid'], text=text_send, reply_markup=reply_markup2)
else:
message_text = "Errore compilazione /request:\n Forma esatta: /request <nome> <cognome> <e-mail> (il nome e il cognome devono essere scritti uniti Es: Di mauro -> Dimauro)"
else:
message_text = "Hai già effettuato la richiesta di accesso"
else:
message_text = "Non è possibile utilizzare /request in un gruppo"
context.bot.sendMessage(chat_id=chat_id, text=message_text)
conn.close()
def add_db(update: Update, context: CallbackContext):
conn = sqlite3.connect('data/DMI_DB.db')
chat_id = update.message.chat_id
if (config_map['dev_group_chatid'] != 0 and chat_id == config_map['dev_group_chatid']):
# /add nome cognome e-mail username chatid
array_value = update.message.text.split(" ")
if len(array_value) == 6:
conn.execute("INSERT INTO 'Chat_id_List' VALUES (" + array_value[5] + ",'" + array_value[4] + "','" + array_value[1] + "','" + array_value[2] + "','" + array_value[3] + "') ")
context.bot.sendMessage(chat_id=array_value[5], text="🔓 La tua richiesta è stata accettata. Leggi il file README")
context.bot.sendDocument(chat_id=array_value[5], document=open('data/README.pdf', 'rb'))
conn.commit()
elif len(array_value) == 5:
conn.execute("INSERT INTO 'Chat_id_List'('Chat_id','Nome','Cognome','Email') VALUES (" + array_value[4] + ",'" + array_value[1] + "','" + array_value[2] + "','" + array_value[3] + "')")
context.bot.sendMessage(chat_id=int(array_value[4]), text="🔓 La tua richiesta è stata accettata. Leggi il file README")
context.bot.sendDocument(chat_id=int(array_value[4]), document=open('data/README.pdf', 'rb'))
conn.commit()
else:
context.bot.sendMessage(chat_id=chat_id, text="/adddb <nome> <cognome> <e-mail> <username> <chat_id>")
conn.close()
def drive(update: Update, context: CallbackContext):
check_log(update, context, "drive")
conn = sqlite3.connect('data/DMI_DB.db')
settings_file = "config/settings.yaml"
gauth = GoogleAuth(settings_file=settings_file)
gauth.CommandLineAuth()
# gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
chat_id = update.message.chat_id
id_drive = '0B7-Gi4nb88hremEzWnh3QmN3ZlU'
if chat_id < 0:
context.bot.sendMessage(chat_id=chat_id, text="La funzione /drive non è ammessa nei gruppi")
else:
if conn.execute("SELECT Chat_id FROM 'Chat_id_List' WHERE Chat_id = " + str(chat_id)).fetchone():
keyboard2 = [[]]
try:
file_list = drive.ListFile({'q': "'" + id_drive + "' in parents and trashed=false", 'orderBy': 'folder,title'}).GetList()
except Exception as error:
print (str(error))
number_row = 0
number_array = 0
for file1 in file_list:
if file1['mimeType'] == "application/vnd.google-apps.folder":
if number_row >= 3:
keyboard2.append([InlineKeyboardButton("🗂 "+file1['title'], callback_data="Drive_" + file1['id'])])
number_row = 0
number_array += 1
else:
keyboard2[number_array].append(InlineKeyboardButton("🗂 "+file1['title'], callback_data="Drive_" + file1['id']))
number_row += 1
else:
if number_row >= 3:
keyboard2.append([InlineKeyboardButton("📃 "+file1['title'], callback_data="Drive_" + file1['id'])])
number_row = 0
number_array += 1
else:
keyboard2[number_array].append(InlineKeyboardButton("📃 "+file1['title'], callback_data="Drive_" + file1['id']))
number_row += 1
reply_markup3 = InlineKeyboardMarkup(keyboard2)
context.bot.sendMessage(chat_id=chat_id, text="DMI UNICT - Appunti & Risorse:", reply_markup=reply_markup3)
else:
context.bot.sendMessage(chat_id=chat_id, text="🔒 Non hai i permessi per utilizzare la funzione /drive,\n Utilizzare il comando /request <nome> <cognome> <e-mail> (il nome e il cognome devono essere scritti uniti Es: Di mauro -> Dimauro) ")
conn.close()
# CallbackQueryHandler
def button_handler(update: Update, context: CallbackContext):
query = update.callback_query
chat_id = query.message.chat_id
message_id = query.message.message_id
data = query.data
# Submenu
if data.startswith("sm_"):
func_name = data[3:len(data)]
globals()[func_name](query, context, chat_id, message_id)
elif data == "esami_button" or data == "lezioni_button" or data == "help_cmd" or data == "exit_cmd":
message_text = globals()[data]()
context.bot.editMessageText(
text=message_text, chat_id=chat_id, message_id=message_id)
elif data.startswith("Drive_"):
callback(update, context)
elif data.startswith('git_'):
gitlab_handler(update, context, data.replace('git_', ''))
elif data == "enablenews" or data == "disablenews":
globals()[data](query, context)
# Simple text
elif data != "_div":
message_text = read_md(data)
check_log(update, context, data, 1)
context.bot.editMessageText(
text=message_text, chat_id=chat_id, message_id=message_id)
def help(update: Update, context: CallbackContext):
check_log(update, context, "help")
chat_id = update.message.chat_id
keyboard = [[]]
message_text = "@DMI_Bot risponde ai seguenti comandi:"
keyboard.append([InlineKeyboardButton(" ~ Dipartimento e CdL ~ ", callback_data="_div")])
keyboard.append(
[
InlineKeyboardButton("📖 Esami (Triennale)", callback_data="esami_button"),
InlineKeyboardButton("📖 Esami (Magistrale)", url=mesami_url()),
InlineKeyboardButton("🗓 Aulario", url=aulario_url()),
InlineKeyboardButton("Lezioni", callback_data="lezioni_button")
]
)
keyboard.append(
[
InlineKeyboardButton("👥 Rappresentanti", callback_data="sm_rapp_menu"),
InlineKeyboardButton("📚 Biblioteca", callback_data="biblioteca"),
InlineKeyboardButton(CUSicon[random.randint(0, 5)] + " CUS", callback_data="cus"),
InlineKeyboardButton("☁️ Cloud", callback_data="cloud")
]
)
keyboard.append([InlineKeyboardButton(" ~ Segreteria orari e contatti ~ ", callback_data="_div")])
keyboard.append(
[
InlineKeyboardButton("Seg. Didattica", callback_data="sdidattica"),
InlineKeyboardButton("Seg. Studenti", callback_data="sstudenti"),
InlineKeyboardButton("CEA", callback_data="cea")
]
)
keyboard.append([InlineKeyboardButton(" ~ ERSU orari e contatti ~ ", callback_data="_div")])
keyboard.append(
[
InlineKeyboardButton("ERSU", callback_data="ersu"),
InlineKeyboardButton("Ufficio ERSU", callback_data="ufficioersu"),
InlineKeyboardButton("URP", callback_data="urp")
]
)
keyboard.append([InlineKeyboardButton(" ~ Bot e varie ~ ", callback_data="_div")])
keyboard.append(
[
InlineKeyboardButton("Iscriviti alle news", callback_data="enablenews"),
InlineKeyboardButton("Disiscriviti dalle news", callback_data="disablenews")
]
)
keyboard.append(
[
InlineKeyboardButton("📂 Drive", callback_data="drive"),
InlineKeyboardButton("📂 GitLab", callback_data="gitlab"),
InlineKeyboardButton("Contributors", callback_data="contributors"),
]
)
keyboard.append(
[
InlineKeyboardButton("Tutti i comandi", callback_data="help_cmd"),
InlineKeyboardButton("Chiudi", callback_data="exit_cmd")
]
)
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.sendMessage(chat_id=chat_id, text=message_text, reply_markup=reply_markup)
def rapp_menu(update: Update, context: CallbackContext, chat_id, message_id):
keyboard = [[]]
message_text = "Quali rappresentanti vuoi contattare?"
keyboard.append(
[
InlineKeyboardButton("Rapp. DMI", callback_data="rappresentanti_dmi"),
InlineKeyboardButton("Rapp. Informatica", callback_data="rappresentanti_informatica"),
InlineKeyboardButton("Rapp. Matematica", callback_data="rappresentanti_matematica"),
]
)
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.editMessageText(text=message_text, chat_id=chat_id, message_id=message_id, reply_markup=reply_markup)
def smonta_portoni(update: Update, context: CallbackContext):
check_log(update, context, "smonta_portoni")
message_text = EasterEgg.get_smonta_portoni()
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text)
def santino(update: Update, context: CallbackContext):
chat_id = update.message.chat_id
if (chat_id == -1001031103640 or chat_id == config_map['dev_group_chatid']):
check_log(update, context, "santino")
message_text = EasterEgg.get_santino()
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text)
def bladrim(update: Update, context: CallbackContext):
check_log(update, context, "bladrim")
message_text = EasterEgg.get_bladrim()
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text)
def prof_sticker(update: Update, context: CallbackContext):
check_log(update, context, "prof_sticker")
context.bot.sendSticker(chat_id=update.message.chat_id, sticker=prof_sticker_id())
def lei_che_ne_pensa_signorina(update: Update, context: CallbackContext):
check_log(update, context, "leiCheNePensaSignorina")
message_text = EasterEgg.get_lei_che_ne_pensa_signorina()
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text)
def prof(update: Update, context: CallbackContext):
check_log(update, context, "prof")
message_text = prof_cmd(context.args)
if len(message_text) > 4096:
send_message(update, context, message_text)
else:
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text, parse_mode='Markdown')
def forum_bot(update: Update, context: CallbackContext):
check_log(update, context, "forum_bot")
message_text = forum_cmd(update.message.text)
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text)
def get_short_link(url):
post_url = 'https://www.googleapis.com/urlshortener/v1/url?key={}'.format(config_map['shortener_key'])
payload = {'longUrl': url}
headers = {'content-type': 'application/json'}
r = requests.post(post_url, data=json.dumps(payload), headers=headers)
return r.json()['id']
def shortit(message):
url_regex = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', message)
news = url_regex.sub('{URL}', message)
urls.reverse()
updated_message = ''
for word in news.split(" "):
if word == '{URL}':
word = get_short_link(urls.pop())
updated_message += word+' '
return updated_message
def news_(update: Update, context: CallbackContext):
if (config_map['spamnews_news_chatid'] != 0 and update.message.chat_id == config_map['spamnews_news_chatid']):
global news
news = update.message.text.replace("/news ", "")
news = update.message.text.replace("/news", "")
# news = shortit(news)
context.bot.sendMessage(chat_id=update.message.chat_id, text="News Aggiornata!")
def spamnews(update: Update, context: CallbackContext):
if (config_map['spamnews_news_chatid'] != 0 and update.message.chat_id == config_map['spamnews_news_chatid']):
chat_ids = open('logs/chatid.txt', 'r').read()
chat_ids = chat_ids.split("\n")
list_chat_ids = chat_ids
for chat_id in chat_ids:
try:
# This command is rarely used. Only for IMPORTANT news. It is right to spam in groups
if not "+" in chat_id:
bot.sendMessage(chat_id=chat_id, text=news)
except Unauthorized:
logger.error('Unauthorized id. Trying to remove from the chat_id list...' + str(chat_id))
list_chat_ids.remove(chat_id)
except Exception as error:
if "Group migrated to supergroup" in str(error):
logger.error(str(error) + " (" + str(chat_id) + ")")
new_chat_id = str(error).replace("Group migrated to supergroup. New chat id: ", "")
new_chat_id = new_chat_id.replace("\n", "")
list_chat_ids.remove(chat_id)
list_chat_ids.append(new_chat_id)
else:
open("logs/errors.txt", "a+").write(str(error) + " " + str(chat_id)+"\n")
list_chat_ids = '\n'.join(list_chat_ids)
list_chat_ids = list_chat_ids.replace("\n\n", "\n")
open('logs/chatid.txt', 'w').write(list_chat_ids)
spam_channel(bot, news)
context.bot.sendMessage(chat_id=update.message.chat_id, text="News spammata!")
def disablenews(update: Update, context: CallbackContext):
check_log(update, context, "disablenews")
chat_ids = open('logs/chatid.txt', 'r').read()
chat_id = update.message.chat_id
if not ("+"+str(chat_id)) in chat_ids:
chat_ids = chat_ids.replace(str(chat_id), "+"+str(chat_id))
message_test = "News disabilitate!"
open('logs/chatid.txt', 'w').write(chat_ids)
else:
message_test = "News già disabilitate!"
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_test)
def enablenews(update: Update, context: CallbackContext):
check_log(update, context, "enablenews")
chat_ids = open('logs/chatid.txt', 'r').read()
chat_id = update.message.chat_id
if ("+"+str(chat_id)) in chat_ids:
chat_ids = chat_ids.replace("+"+str(chat_id), str(chat_id))
message_text = "News abilitate!"
open('logs/chatid.txt', 'w').write(chat_ids)
else:
message_text = "News già abilitate!"
context.bot.sendMessage(chat_id=update.message.chat_id, text=message_text)
def stats_gen(update: Update, context: CallbackContext, days):
conn = sqlite3.connect('data/DMI_DB.db')
chat_id = update.message.chat_id
query = ""
text = ""
if days == 0:
text += "Record Globale:\n"
query = "SELECT Type, count(chat_id) FROM stat_list GROUP BY Type ORDER BY Type;"
else:
text += "Record di "+str(days)+" giorni:\n"
query = "SELECT Type, count(chat_id) FROM stat_list WHERE DateCommand > '"+ str(date.today()-timedelta(days=days)) + "' GROUP BY Type ORDER BY Type;"
for row in conn.execute(query):
if str(row[0]) != "leiCheNePensaSignorina" and str(row[0]) != "smonta_portoni" and str(row[0]) != "santino" and str(row[0]) != "bladrim" and str(row[0]) != "prof_sticker":
text += str(row[1]) + ": " + str(row[0]) + "\n"
context.bot.sendMessage(chat_id=chat_id, text=text)
conn.close()
def stats(update: Update, context: CallbackContext):
if(len(update['message']['text'].split(' ')) == 2):
days = int(update['message']['text'].split(' ')[1])
if(days <= 0):
days = 30
else:
days = 30
stats_gen(update, context, days)
def stats_tot(update: Update, context: CallbackContext):
stats_gen(update, context, 0)
def check_log(update: Update, context: CallbackContext, type, callback=0):
if callback:
update = update.callback_query
chat_id = update.message.chat_id
if (config_map['debug']['disable_db'] == 0):
today = str(date.today())
conn = sqlite3.connect('data/DMI_DB.db')
conn.execute("INSERT INTO stat_list VALUES ('"+ str(type) + "',"+str(chat_id)+",'"+str(today)+" ')")
conn.commit()
conn.close()
if (config_map['debug']['disable_chatid_logs'] == 0):
a_log = open("logs/chatid.txt", "a+")
r_log = open("logs/chatid.txt", "r+")
if not str(chat_id) in r_log.read():
a_log.write(str(chat_id)+"\n")
def give_chat_id(update: Update, context: CallbackContext):
update.message.reply_text(str(update.message.chat_id))
def send_log(update: Update, context: CallbackContext):
if(config_map['dev_group_chatid'] != 0 and update.message.chat_id == config_map['dev_group_chatid']):
context.bot.sendDocument(chat_id=config_map['dev_group_chatid'], document=open('logs/logs.txt', 'rb'))
def send_chat_ids(update: Update, context: CallbackContext):
if(config_map['dev_group_chatid'] != 0 and update.message.chat_id == config_map['dev_group_chatid']):
context.bot.sendDocument(chat_id=config_map['dev_group_chatid'], document=open('logs/chatid.txt', 'rb'))
def send_errors(update: Update, context: CallbackContext):
if(config_map['dev_group_chatid'] != 0 and update.message.chat_id == config_map['dev_group_chatid']):
context.bot.sendDocument(chat_id=config_map['dev_group_chatid'], document=open('logs/errors.txt', 'rb'))
def spam_channel(update: Update, context: CallbackContext):
try:
context.bot.sendMessage(chat_id=config_map['news_channel'], text=message, parse_mode='HTML')
except Exception as error:
open("logs/errors.txt", "a+").write("{} {}\n".format(error, config_map['news_channel']))
def avviso(context):
job = context.job
scrape_notices()
if os.path.isfile("data/avviso.dat"):
testo = open("data/avviso.dat").read()
if testo != "":
chat_ids = open("logs/chatid.txt", "r").read()
chat_ids = chat_ids.split("\n")
for chat_id in chat_ids:
try:
# Ignore chat_id with disabled news. Ignore chat_id groups.
if not chat_id.startswith(("+", "-")):
context.bot.sendMessage(chat_id=chat_id, text=testo, parse_mode='HTML')
except Exception as error:
open("logs/errors.txt", "a+").write(str(error) + " " + str(chat_id)+"\n")
spam_channel(bot, testo)
os.remove("data/avviso.dat")
def updater_lep(context):
job = context.job
scrape_lessons()
scrape_exams()
scrape_prof()
def start(update: Update, context: CallbackContext):
context.bot.sendMessage(chat_id=update.message.chat_id, text="Benvenuto! Questo bot è stato realizzato dagli studenti del Corso di Laurea in Informatica al fine di suppotare gli studenti del DMI! Per scoprire cosa puoi fare usa /help")
def newscommand(update: Update, context: CallbackContext):
check_log(update, context, "avvisi")
global news
if news == "":
context.bot.sendMessage(chat_id=update.message.chat_id, text="Non ho nulla da mostrarti.")
else:
context.bot.sendMessage(chat_id=update.message.chat_id, text=news)
def git(update: Update, context: CallbackContext):
check_log(update, context, "gitlab")
chat_id = update.message.chat_id
executed_command = update.message.text.split(' ')[0]
if chat_id < 0:
context.bot.sendMessage(chat_id=chat_id, text="❗️ La funzione %s non è ammessa nei gruppi" % executed_command)
else:
db = sqlite3.connect('data/DMI_DB.db')
if db.execute("SELECT Chat_id FROM 'Chat_id_List' WHERE Chat_id = %s" % chat_id).fetchone():
gitlab_handler(update, context)
else:
context.bot.sendMessage(chat_id=chat_id, text="🔒 Non hai i permessi per utilizzare la funzione %s\nUtilizzare il comando /request <nome> <cognome> <e-mail> (il nome e il cognome devono essere scritti uniti Es: Di Mauro -> DiMauro)" % executed_command)
db.close()
def report(update: Update, context: CallbackContext):
check_log(update, context, "report")
chat_id = update.message.chat_id
chat_username = update.message.from_user.username
executed_command = update.message.text.split(' ')[0]
if chat_id < 0:
context.bot.sendMessage(chat_id=chat_id, text="! La funzione %s non è ammessa nei gruppi" % executed_command)
elif not chat_username:
context.bot.sendMessage(chat_id=chat_id, text="La funzione %s non è ammessa se non si dispone di un username." % executed_command)
else:
if context.args:
db = sqlite3.connect('data/DMI_DB.db')
message = "⚠️Segnalazione⚠️\n"
if db.execute("SELECT Chat_id FROM 'Chat_id_List' WHERE Chat_id = %s" %chat_id).fetchone():
name = db.execute("SELECT Username,Nome,Cognome FROM 'Chat_id_List' WHERE Chat_id = %s" %chat_id)
row = name.fetchone()
if row[0] is None:
message += "Nome: " + row[1] + "\n" + "Cognome: " + row[2] + "\n" + " ".join(args)
else:
message += "Username: @" + row[0] + "\n" + "Nome: " + row[1] + "\n" + "Cognome: " + row[2] + "\n" + " ".join(args)
context.bot.sendMessage(chat_id = config_map['representatives_group'], text = message)
context.bot.sendMessage(chat_id = chat_id, text = "Resoconto segnalazione: \n" + message + "\n Grazie per la segnalazione, un rappresentante ti contatterà nel minor tempo possibile.")
db.close()
else:
context.bot.sendMessage(chat_id=chat_id, text="🔒 Non hai i permessi per utilizzare la funzione %s\nUtilizzare il comando /request <nome> <cognome> <e-mail> (il nome e il cognome devono essere scritti uniti Es: Di Mauro -> DiMauro)" % executed_command)
else:
context.bot.sendMessage(chat_id = chat_id, text="Errore. Inserisci la tua segnalazione dopo /report (Ad esempio /report Invasione ingegneri in corso.)")