/
scienzati_bot.py
913 lines (804 loc) · 37.4 KB
/
scienzati_bot.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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
import telebot
#pip3 install PyTelegramBotAPI
import time
import sqlite3
import random
import re
#Settings class
class Settings:
TelegramApiKey = "676490981:AAELlmTlQLD4_1HojhzWIX4yISDrVU5qDmA"
SupremeAdmins = [14092073]
ITGroup = 0
OTGroup = -1001176680738
subscriptionRows = 7
###
## Bot Inizialization
###
#Create the bot instance
bot = telebot.TeleBot(Settings.TelegramApiKey)
botInfo = bot.get_me()
print("Authorized on @" + botInfo.username)
###
## Database Inizialization
###
#Initialize the database connection
dbConnection = sqlite3.connect('database.sqlitedb', check_same_thread=False)
#Set the resulting array to be associative
# https://stackoverflow.com/a/2526294
dbConnection.row_factory = sqlite3.Row
#Sets the database cursor.
#It is used to submit queires to the DB and manage it
# https://docs.python.org/2/library/sqlite3.html
dbC = dbConnection.cursor()
# Remember to close the connection at the end of the program with conn.close()
#This part of the code is used to initalize the database.
#It runs the "seed" query
#This is the query that is used to initialize the SQLite3 database
initQuery= """CREATE TABLE IF NOT EXISTS `Users` (
`ID` INTEGER NOT NULL UNIQUE,
`Nickname` TEXT NOT NULL,
`Biography` TEXT,
`Status` INTEGER NOT NULL DEFAULT 0,
`Permissions` INTEGER DEFAULT 0,
`ITMessageNumber` INTEGER DEFAULT 0,
`OTMessageNumber` INTEGER DEFAULT 0,
`LastSeen` TEXT DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY(`ID`)
);
CREATE TABLE IF NOT EXISTS `Lists` (
`ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`Name` TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS `Subscriptions` (
`ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`List` INTEGER NOT NULL,
`User` INTEGER NOT NULL,
FOREIGN KEY(`User`) REFERENCES `Users`(`ID`),
FOREIGN KEY(`List`) REFERENCES `Lists`(`ID`)
);"""
# Seeds the databse - Executes the inital query
#I use executescript instead of excut to permit to do multiple queries
dbC.executescript(initQuery)
# Save (commit) the changes
dbConnection.commit()
###
# Constant message values
###
class constResources:
intro_mex = """Questo e' il bot del gruppo @scienza,
/iscrivi iscriviti al database di utenti e a liste di interessi
/modifica visiona e modifica la propria descrizione
/liste consulta le attuali liste di interessi
/nuovalista crea nuove liste
/privs elenca i privilegi utente"""
privs_mex = """privs =-1 -> utente non registrato
= 0 -> utente normale
= 1 -> utente abituale
= 2 -> utente assiduo
= 3 -> utente storico (puo' inoltrare al canale, puo' creare nuove liste)
= 4 -> amministratore
= 5 -> fondatore"""
gdpr_message = "Raccogliamo il numero di messaggi, nickname, ID e ultima volta che l'utente ha scritto. Per richiedere l'eliminazione dei propri dati contattare un amministratore ed uscire dal gruppo"
###
# Constant values
# Those values are static and are used to represent the user's state/permission
# The Permission funtion are used to execute bitwise operations, and the status are simply used to compare the statuses
###
# Status legend
# -2 - Waiting for biograhy
# -1 - User just created - needs to insert bio
# 0 - User created
# 15 - Banned
class UserStatus: #Enum emulator
WAITING_FOR_LIST = -5
WAITING_FOR_BIOGRAPHY = -2
USER_JUST_CREATED = -1
ACTIVE = 0
BANNED = 15
#Dummy functions - Those functions are "dummy": they are just used to compare a given input to the value in the class
def IsWaitingForBio(status):
if status == UserStatus.WAITING_FOR_BIOGRAPHY:
return True
return False
def IsWaitingForListName(status):
if status == UserStatus.WAITING_FOR_LIST:
return True
return False
def IsJustCreated(status):
if status == UserStatus.USER_JUST_CREATED:
return True
return False
def IsActive(status):
if status == UserStatus.ACTIVE:
return True
return False
def IsBanned(status):
if status == UserStatus.BANNED:
return True
return False
# Complex functions
#CanEnterBio Is used whern checking if a user has privileges to edit its Biography
def CanEnterBio(status):
if status == UserStatus.BANNED:
return False
return True
# Permissions legend
#
# xxx0 - Admin flag - 1 = admin
# xx0x - Channed flag - 1 = can post to channel
# x0xx - flag - 1 = can post to channel
#
class UserPermission: #Siply do an AND with the permission
ADMIN=int('1', 2)
CHANNEL=int('10', 2)
CREATE_LIST=int('100', 2)
def IsAdmin(permission):
if (permission & UserPermission.ADMIN) == UserPermission.ADMIN:
return True
return False
def CanForwardToChannel(permission):
if (permission & UserPermission.CHANNEL) == UserPermission.CHANNEL:
return True
return False
def CanCreateList(permission):
if (permission & UserPermission.CREATE_LIST) == UserPermission.CREATE_LIST:
return True
return False
###
# Helper functions
# Those functions will be used as support functions for the bot.
# Those function are "database wrappers"
###
#GetUser is used to return the row corresponding to the user in the database.
#It went introduced because the same query repeted over and over
def GetUser(userID):
#Create a database cursor
dbC = dbConnection.cursor()
#Selects the users
dbC.execute('SELECT * FROM Users WHERE ID=?', (userID,))
#Fetch the results
rows = dbC.fetchall()
#Check if the users exists
if len(rows) > 0:
if len(rows) > 1:
#something's wrong here, the ID shouln't be greater than one
raise Exception('The user exceed 1. Something could be wrong with the database. Code error #S658')
else:
#The users exists, returns the permission
return rows[0]
else:
#No record found - ID could be erroneous
#TODO: Throw error?
return False
#UpdateBio is a helper function to update the biography of a user.
#It returns true in case of success, otherwise it returns false
def UpdateBio(userdID, bio):
dbC = dbConnection.cursor()
res = dbC.execute('INSERT INTO Users (ID, Nickname, Status) VALUES (?,?,?)', (userdID, bio, UserStatus.USER_JUST_CREATED,) )
if res:
CommitDb()
return True
return False
# GetUserPermissionsValue takes the userID as input and returns the permission value (int) direclty from the database
def GetUserPermissionsValue(userID):
user = GetUser(userID)
if user != False:
return user["Permissions"]
#No user exist, returning Flase for now
return False
def GetUserStatusValue(userID):
user = GetUser(userID)
if user != False:
return user["Status"]
#No user exist, returning Flase for now
return False
#IncrITGroupMessagesCount increments the number of messages in the IT group
def IncrITGroupMessagesCount(userID):
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET ITMessageNumber = ITMessageNumber + 1 WHERE ID = ?', (userID,) )
if res:
CommitDb()
return True
return False
#IncrOTGroupMessagesCount increments the number of messages in the OT group
def IncrOTGroupMessagesCount(userID):
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET OTMessageNumber = OTMessageNumber + 1 WHERE ID = ?', (userID,) )
if res:
CommitDb()
return True
return False
def UpdateLastSeen(userID, date):
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET LastSeen=? WHERE ID = ?', (date, userID,) )
if res:
CommitDb()
return True
return False
def CommitDb():
dbConnection.commit()
def GetUserNickname(userID):
dbC = dbConnection.cursor()
dbC.execute('SELECT `Nickname` FROM Users WHERE `ID`=?;', (userID,))
res = dbC.fetchone()
if res != None:
return res[0]
return False
#CreateNewListWithoutDesc creates a new list :O
def CreateNewList(name):
dbC = dbConnection.cursor()
try:
res = dbC.execute('INSERT INTO Lists (Name) VALUES (?)', (name,) )
if res:
return True
return False
except:
return False
def GetLists():
dbC = dbConnection.cursor()
dbC.execute('SELECT `Name` FROM Lists')
return dbC.fetchall()
def SubscribeUserToList(userID, listID):
#If user is not in the list
dbC = dbConnection.cursor()
dbC.execute('SELECT * FROM Subscriptions WHERE User=? AND List=?', (userID, listID))
res = dbC.fetchall()
if len(res) >0:
#User already subscribed
return False
#Create subscription
dbC = dbConnection.cursor()
dbC.execute('INSERT INTO Subscriptions (User, List) VALUES (?,?)', (userID, listID))
res = dbC.fetchall()
if res != None:
return True
return False
def UnubscribeUserFromList(userID, listID):
#If user is not in the list
dbC = dbConnection.cursor()
dbC.execute('DELETE FROM Subscriptions WHERE User=? AND List=?', (userID, listID))
res = dbC.fetchall()
if res != None:
#User already subscribed
return True
return False
def AvailableListsToUser(userID, limit=Settings.subscriptionRows-1, offset=0):
#If user is not in the list
dbC = dbConnection.cursor()
dbC.execute('SELECT ID, Name FROM Lists WHERE Lists.ID NOT IN (SELECT List FROM Subscriptions WHERE User=?) LIMIT ? OFFSET ?', (userID, limit,offset))
res = dbC.fetchall()
if len(res) >0:
#User already subscribed
return res
return False
def SubscribedLists(userID, limit=Settings.subscriptionRows-1, offset=0):
#If user is not in the list
dbC = dbConnection.cursor()
dbC.execute('SELECT Lists.ID, Lists.Name FROM Lists INNER JOIN Subscriptions ON Subscriptions.List = Lists.ID WHERE Subscriptions.User=? LIMIT ? OFFSET ?', (userID, limit,offset))
res = dbC.fetchall()
if len(res) >0:
#User already subscribed
return res
return False
def GetListID(listName):
dbC = dbConnection.cursor()
dbC.execute('SELECT `ID` FROM Lists WHERE `Name`=?;', [listName])
res = dbC.fetchone()
if res != None:
return res[0]
return False
def GetListName(listID):
dbC = dbConnection.cursor()
dbC.execute('SELECT `Name` FROM Lists WHERE `ID`=?;', (listID,))
res = dbC.fetchone()
if res != None:
return res[0]
return False
def ListExists(listName):
dbC = dbConnection.cursor()
dbC.execute('SELECT `ID` FROM Lists WHERE `Name`=?', (listName,))
res = dbC.fetchall()
if len(res) >0:
#User already subscribed
return True
return False
def GetListSubscribers(listID):
dbC = dbConnection.cursor()
dbC.execute('SELECT `User` FROM Subscriptions WHERE `List`=?', (listID,))
res = dbC.fetchall()
if len(res) >0:
#User already subscribed
return res
return False
def UpdateNickname(userID, nickname):
dbC = dbConnection.cursor()
dbC.execute('UPDATE Users SET Nickname=? WHERE ID = ?', (nickname, userID, ))
#Abort the inserting process of a new Bio
#WARNING: CHECK IF USER IS BANNED BEFORE, OR HE WILL GET UNBANNED
def abortNewBio(userID):
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET Status=? WHERE ID = ?', (UserStatus.ACTIVE, userID,) )
if res:
CommitDb()
return True
return False
def GetUserBio(userID):
dbC = dbConnection.cursor()
dbC.execute('SELECT `Biography` FROM Users WHERE `ID`=?;', (userID,))
res = dbC.fetchone()
if res != None:
return res[0]
return False
def abortNewList(userID):
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET Status=? WHERE ID = ?', (UserStatus.ACTIVE, userID,) )
if res:
CommitDb()
return True
return False
def getUsersIdLike(userNick):
dbC = dbConnection.cursor()
dbC.execute('SELECT `ID` FROM Users WHERE `Nickname` LIKE ?;', ("%"+userNick.replace("%", ":%")+"%",))
res = dbC.fetchall()
if res != None:
return res
return False
###
# Bot functions
###
#Start command.
# This is the function called when the bot is started or the help commands are sent
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, constResources.intro_mex)
# Replies with the static message before
@bot.message_handler(commands=['privs'])
def send_privs(message):
bot.reply_to(message, constResources.privs_mex)
# Replies with the static message before
@bot.message_handler(commands=['gdpr'])
def send_gdrp(message):
bot.reply_to(message, constResources.gdpr_message)
### Messaggio di Iscrizione
@bot.message_handler(commands=['iscrivi'])
def start_user_registration(message):
if not message.from_user.is_bot and message.text != "" :
# Tries to see
dbC = dbConnection.cursor()
dbC.execute('SELECT * FROM Users WHERE ID=?', (message.from_user.id,))
rows = dbC.fetchall()
#if database.get(str(message.from_user.id),None) is None:
if len(rows) > 0:
#The user exists in database
bot.reply_to(message, "Sei già registrato in database. se desideri modificare la tua biografia puoi farlo mediante il comando /bio")
else:
#The user needs to be created
#bot.reply_to(message, "creazione nuovo record utente...")
#Insert
dbC = dbConnection.cursor()
res = dbC.execute('INSERT INTO Users (ID, Nickname, Status) VALUES (?,?,?)', (message.from_user.id, message.from_user.username, UserStatus.USER_JUST_CREATED,) )
dbConnection.commit()
if res:
msg = bot.reply_to(message, "Congratulazioni, ti sei registrato correttamente! Ora puoi procedere ad inserire la tua biografia attraverso il comando /bio")
else:
msg = bot.reply_to(message, "Errore nella creazione del record")
# this is to define step-by-step subscription
#bot.register_next_step_handler(msg, first_registration)
### Aggioramento/ impostaizone bio
@bot.message_handler(commands=['bio'])
def setBio(message):
if not message.from_user.is_bot and message.text != "" :
# Gets info about the user
user = GetUser(message.from_user.id)
#Check if the user exists
if user == False:
#the user does not exist
msg = bot.reply_to(message, "Non sei ancora registrato. Puoi registrarti attraverso il comando /iscrivi ")
#Check its status
else:
#There's only one user, as it's supposed to be
#Check if the user needs to set a biography
if UserStatus.CanEnterBio(user["Status"]):
#Asks for the bio
dbC = dbConnection.cursor()
#res = dbC.execute('UPDATE Users SET Status=? WHERE ID = ?;', (UserStatus.WAITING_FOR_BIOGRAPHY , message.from_user.id,) )
res = dbC.execute('UPDATE Users SET Status=? WHERE ID = ?', (UserStatus.WAITING_FOR_BIOGRAPHY , message.from_user.id,) )
#res = dbC.execute('UPDATE Users SET Status=?, Biography=? WHERE employeeid = ?;', (0, message.text, message.from_user.id,) )
#Tries to force the user to reply to the message
#markup = telebot.types.ForceReply(selective=False)
markup = telebot.types.InlineKeyboardMarkup()
markup.row_width = 1
markup.add(telebot.types.InlineKeyboardButton('❌ Annulla', callback_data=f"aBio"))
currentBioMsg = GetUserBio(message.from_user.id)
if currentBioMsg != None and currentBioMsg != "":
currentBioMsg = "La tua attuale biografia è \"" + currentBioMsg + "\".\n"
else:
currentBioMsg = ""
msg = bot.reply_to(message, currentBioMsg + "Per impostare una nuova biografia, scrivimela in chat privata o rispondendomi", reply_markup=markup)
dbConnection.commit()
else:
#Nothing to do here
msg = bot.reply_to(message, "You can't enter a bio.")
#Creazione di una nuova lista
@bot.message_handler(commands=['newlist', 'nuovalista'])
def newList(message):
if message.from_user.id in Settings.SupremeAdmins or UserPermission.IsAdmin(GetUserPermissionsValue(message.from_user.id)) or UserPermission.CanCreateList(GetUserPermissionsValue(message.from_user.id)):
if not message.from_user.is_bot and message.text != "" :
# Gets info about the user
user = GetUser(message.from_user.id)
#Check if the user exists
if user == False:
#the user does not exist
msg = bot.reply_to(message, "Something's wrong here. error code: #R747")
else:
#Asks for the bio
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET Status=? WHERE ID = ?', (UserStatus.WAITING_FOR_LIST , message.from_user.id,) )
markup = telebot.types.InlineKeyboardMarkup()
markup.row_width = 1
markup.add(telebot.types.InlineKeyboardButton('❌ Annulla', callback_data=f"aList"))
msg = bot.reply_to(message, "Per creare una nuova lista, scrivi il nome in chat privata o in un messaggio che mi risponda rispondendomi", reply_markup=markup)
dbConnection.commit()
else:
msg = bot.reply_to(message, "Error 403 - ❌ Unauthorized")
#Lista delle liste
@bot.message_handler(commands=['lists', 'liste'])
def showLists(message):
msg = "Ecco le liste esistenti al momento:\n"
for list in GetLists():
msg = msg + list[0] + "\n"
bot.reply_to(message, msg)
@bot.message_handler(commands=['subscribe', 'join', 'registrati'])
def subscribeUserListHandler(message):
user = GetUser(message.from_user.id)
if user != False:
#The user is registred in DB
userStatus = GetUserStatusValue(message.from_user.id)
if UserStatus.IsActive(userStatus):
#Add to list
msg = "Ecco un elenco delle liste attualmente disponibili:\n(Per sottoscriverti ad una lista, è sufficiente \"tapparla\")"
#Get available lists
lists = AvailableListsToUser(message.from_user.id)
markup = telebot.types.InlineKeyboardMarkup()
#Print the lists as inline buttons
if len(lists) == 0:
msg = "Al momento non è presente nessuna lista.\nSi prega di riprovare in seguito."
for ulist in lists:
# sub-{id} => subscript to list {id}
markup.row(telebot.types.InlineKeyboardButton(ulist["Name"], callback_data="sub-"+str(ulist["ID"])))
#If there are still lists, print the page delimiter
#if len(lists) > Settings.subscriptionRows-1:
if AvailableListsToUser(message.from_user.id, limit=1, offset=int(Settings.subscriptionRows)) != False:
# osub-{n} => offest subscription, needed for pagination,
#Teels the offset to set to correctly display the pages
markup.row(telebot.types.InlineKeyboardButton(" ", callback_data="ignore"), telebot.types.InlineKeyboardButton(f"➡️", callback_data=f"osub-"+str(Settings.subscriptionRows-1)))
#⬅️ ➡️
msg = bot.reply_to(message, msg, reply_markup=markup)
#SubscribeUserToList()
elif UserStatus.IsBanned(userStatus):
#banned, not much you can do right now
bot.reply_to(message, "Error 403 - ❌ Unauthorized")
else:
#User in another activity (like creating list)
bot.reply_to(message, "Sembra che tu sia occupato in un'altra azione (come impostare una biografia).\n Sarebbe opportuno terminare quell'azione prima di cercare di intraprenderne altre")
else:
bot.reply_to(message, "Sarebbe opportuno registrarsi prima, tu non credi?\nPuoi farlo attraverso il comando /iscrivi")
@bot.message_handler(commands=['subscribedto', 'joinedto', 'inscriptions', 'iscrizioni', 'disiscriviti', 'rimuovi'])
def unsubscribeUserListHandler(message):
user = GetUser(message.from_user.id)
if user != False:
#The user is registred in DB
userStatus = GetUserStatusValue(message.from_user.id)
if UserStatus.IsActive(userStatus):
#Add to list
msg = "Ecco un elenco delle liste alle quali sei attualmente iscritto:\n(Per rimuovere la sottoscrizione, è sufficiente \"tapparla\" e confermare)"
#Get available lists
lists = SubscribedLists(message.from_user.id)
markup = telebot.types.InlineKeyboardMarkup()
#Print the lists as inline buttons
if lists == False:
msg="Al momento non sei iscritto a nessuna lista.\nPuoi iscriverti ad una lista attraverso il comando /registrati."
else:
for ulist in lists:
# sub-{id} => unsubscribe to list {id}
markup.row(telebot.types.InlineKeyboardButton(ulist["Name"], callback_data="usub-"+str(ulist["ID"])))
#If there are still lists, print the page delimiter
#if len(lists) > Settings.subscriptionRows-1:
if SubscribedLists(message.from_user.id, limit=1, offset=int(Settings.subscriptionRows)) != False:
# osub-{n} => offest subscription, needed for pagination,
#Teels the offset to set to correctly display the pages
markup.row(telebot.types.InlineKeyboardButton(" ", callback_data="ignore"), telebot.types.InlineKeyboardButton(f"➡️", callback_data=f"ousub-"+str(Settings.subscriptionRows-1)))
#⬅️ ➡️
msg = bot.reply_to(message, msg, reply_markup=markup)
#SubscribeUserToList()
elif UserStatus.IsBanned(userStatus):
#banned, not much you can do right now
bot.reply_to(message, "Error 403 - ❌ Unauthorized")
else:
#User in another activity (like creating list)
bot.reply_to(message, "Sembra che tu sia occupato in un'altra azione (come impostare una biografia).\n Sarebbe opportuno terminare quell'azione prima di cercare di intraprenderne altre")
else:
bot.reply_to(message, "Sarebbe opportuno registrarsi prima, tu non credi?\nPuoi farlo attraverso il comando /iscrivi")
@bot.message_handler(func=lambda m: True)
def genericMessageHandler(message):
#get info about the user
user = GetUser(message.from_user.id)
if user != False:
#The user is registred in DB
UpdateNickname(message.from_user.id, message.from_user.username.lower())
#Check for biography
if user["Status"] == UserStatus.WAITING_FOR_BIOGRAPHY:
#User is setting the Bio
if message.chat.type == "private":
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET Status=?, Biography=? WHERE ID = ?', (UserStatus.ACTIVE, message.text, message.from_user.id,) )
msg = bot.reply_to(message, "✅ Biografia impostata con successo!")
#Tries to force the user to reply to the message
#TODO: Not sure about the order - needs to be checked
elif message.chat.type == "group" or message.chat.type == "supergroup" and message.reply_to_message != None and message.reply_to_message.from_user.id == botInfo.id:
dbC = dbConnection.cursor()
res = dbC.execute('UPDATE Users SET Status=?, Biography=? WHERE ID = ?', (UserStatus.ACTIVE, message.text, message.from_user.id,) )
msg = bot.reply_to(message, "✅ Biografia impostata con successo!")
#Check for list
elif user["Status"] == UserStatus.WAITING_FOR_LIST:
#User is creating a new list
#TODO check for ASCII ONLY (RegEx?), replace spaces with underscores,
listName = message.text.lower()
p = re.compile(r'[a-z0-9_\-]+', re.IGNORECASE)
if not p.match(listName):
bot.reply_to(message, "Qualcosa è andato storto :c\n Il nome sembra contenre caratteri non permessi. Sono permesse solo lettere, numeri, underscores(_) e trattini")
return
if message.chat.type == "private":
success = CreateNewList(listName)
if success:
msg = bot.reply_to(message, "Lista creata con successo!")
else:
msg = bot.reply_to(message, "Qualcosa è andato storto :c\n Sei sicuro che non esista già una lista con lo stesso nome?")
#Tries to force the user to reply to the message
#TODO: Not sure about the order - needs to be checked
elif message.chat.type == "group" or message.chat.type == "supergroup" and message.reply_to_message != None and message.reply_to_message.from_user.id == botInfo.id:
success = CreateNewList(listName)
if success:
msg = bot.reply_to(message, "Lista creata con successo!")
else:
msg = bot.reply_to(message, "Qualcosa è andato storto :c\n Sei sicuro che non esista già una lista con lo stesso nome?")
else:
#Normal message, increment message counter
#update lastseen
UpdateLastSeen(message.from_user.id, time.strftime('%Y-%m-%d %H:%M:%S',
#Telegram sends the date in a epoch format
#https://core.telegram.org/bots/api#message
# Need to convert it
#https://stackoverflow.com/a/12400584
time.localtime(message.date)))
if message.chat.type == "group" or message.chat.type == "supergroup" and not message.from_user.is_bot and message.text != "":
if message.text[0] == "#" or message.text[0] == "@" or message.text[0] == "." or message.text[0] == "!":
listName = message.text.strip()[1:].lower()
if ListExists(listName):
users = GetListSubscribers(GetListID(listName))
if users != False:
variations = ["alla riscossa!", "all'attacco!", "che la conoscenza sia con voi!", "il mondo confida in voi!",
"che la vostra conoscenza possa illuminare la via!", "possa la vostra conoscenza aprire nuove vie!"]
msg = "Gente di " + listName + ", " + random.choice(variations) + "\n"
for user in users:
msg = msg + "@"+GetUserNickname(user[0]) + ", "
msg = msg[:len(msg)-2]
else:
msg = "La lista " + listName + " non ha ancora nessun iscritto :c"
bot.reply_to(message, msg)
#Message counter
if message.chat.id == Settings.ITGroup:
#Increment IT group messages cunt
IncrITGroupMessagesCount(message.from_user.id)
elif message.chat.id == Settings.OTGroup:
#Increment OT group messages cunt
IncrOTGroupMessagesCount(message.from_user.id)
dbConnection.commit()
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
#Sample response
# {'game_short_name': None, 'chat_instance': '5537587246343980605', 'id': '60524995438318427', 'from_user': {'id': 14092073, 'is_bot': False, 'first_name': 'Pandry', 'username': 'Pandry', 'last_name': None, 'language_code': 'en-US'}, 'message': {'content_type': 'text', 'message_id': 2910, 'from_user': <telebot.types.User object at 0x040BBB30>, 'date': 1541000520, 'chat': <telebot.types.Chat object at 0x040BBB10>, 'forward_from_chat': None, 'forward_from': None, 'forward_date': None, 'reply_to_message': <telebot.types.Message object at 0x040BBFB0>, 'edit_date': None, 'media_group_id': None, 'author_signature': None, 'text': 'Per impostare una biografia, scrivila in chat privata o rispondendomi', 'entities': None, 'caption_entities': None, 'audio': None, 'document': None, 'photo': None, 'sticker': None, 'video': None, 'video_note': None, 'voice': None, 'caption': None, 'contact': None, 'location': None, 'venue': None, 'new_chat_member': None, 'new_chat_members': None, 'left_chat_member': None, 'new_chat_title': None, 'new_chat_photo': None, 'delete_chat_photo': None, 'group_chat_created': None, 'supergroup_chat_created': None, 'channel_chat_created': None, 'migrate_to_chat_id': None, 'migrate_from_chat_id': None, 'pinned_message': None, 'invoice': None, 'successful_payment': None, 'connected_website': None, 'json': {'message_id': 2910, 'from': {'id': 676490981, 'is_bot': True, 'first_name': 'ScienzaBot', 'username': 'scienzati_bot'}, 'chat': {'id': -1001176680738, 'title': '@Scienza World Domination', 'type': 'supergroup'}, 'date': 1541000520, 'reply_to_message': {'message_id': 2909, 'from': {'id': 14092073,
# 'is_bot': False, 'first_name': 'Pandry', 'username': 'Pandry', 'language_code': 'en-US'}, 'chat': {'id': -1001176680738, 'title': '@Scienza World Domination', 'type': 'supergroup'}, 'date': 1541000520, 'text': '/bio', 'entities': [{'offset': 0, 'length': 4, 'type': 'bot_command'}]}, 'text': 'Per impostare una biografia, scrivila in chat privata o rispondendomi'}}, 'data': 'annulla', 'inline_message_id': None}
#
#The call data can be edited, checks are needed
user = GetUser(call.from_user.id)
if user != False:
#Check if is to abort bio
if call.data == "aBio":
#Check if the guy who pressed is the same who asked to set the bio
if call.from_user.id == call.message.reply_to_message.from_user.id:
#Check that the user needs to set the bio
if user["Status"] == UserStatus.WAITING_FOR_BIOGRAPHY :
success = abortNewBio(call.from_user.id)
if success:
markup = telebot.types.InlineKeyboardMarkup()
bot.edit_message_text("Annullato." , call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
else:
bot.delete_message(call.message.chat.id , call.message.message_id)
#Check if is to abort list creation
elif call.data == "aList":
#Check if the guy who pressed is the same who asked to set the bio
if call.from_user.id == call.message.reply_to_message.from_user.id:
#Check that the user needs to set the bio
if user["Status"] == UserStatus.WAITING_FOR_LIST :
success = abortNewList(call.from_user.id)
if success:
markup = telebot.types.InlineKeyboardMarkup()
bot.edit_message_text("Annullato." , call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
else:
bot.delete_message(call.message.chat.id , call.message.message_id)
elif "ousub-" in call.data:
if call.from_user.id == call.message.reply_to_message.from_user.id:
if user["Status"] == UserStatus.ACTIVE :
#Show next n rows + offset, osub-{offset}
#Safe data checks
splittedString = call.data.split('-')
if len(splittedString) == 2:
if splittedString[1].isdigit():
actualOffset=int(splittedString[1])
if actualOffset%(Settings.subscriptionRows-1) == 0:
lists = SubscribedLists(call.from_user.id, offset=int(actualOffset))
markup = telebot.types.InlineKeyboardMarkup()
if len(lists) == 0:
bot.edit_message_text("Non sei iscritto a nessuna lista" , call.message.chat.id , call.message.message_id, call.id)
return
#Print the lists as inline buttons
for ulist in lists:
# sub-{id} => subscript to list {id}
markup.row(telebot.types.InlineKeyboardButton(ulist["Name"], callback_data="usub-"+str(ulist["ID"])))
#If there are still lists, print the page delimiter
if len(lists) > Settings.subscriptionRows-1:
previousArrow = telebot.types.InlineKeyboardButton(f"⬅️", callback_data=f"ousub-"+str(int(actualOffset) - Settings.subscriptionRows+1))
nextArrow = telebot.types.InlineKeyboardButton(f"➡️", callback_data=f"ousub-"+str(int(actualOffset) + Settings.subscriptionRows-1))
emptyArrow = telebot.types.InlineKeyboardButton(" ", callback_data="ignore")
leftButton, rightButton = emptyArrow,emptyArrow
#Check if there are more list
if SubscribedLists(call.from_user.id, limit=1, offset=int(actualOffset+Settings.subscriptionRows)) != False:
rightButton = nextArrow
if actualOffset - Settings.subscriptionRows +2 > 0:
leftButton = previousArrow
markup.row(leftButton, rightButton)
#msg = bot.reply_to(message, msg, reply_markup=markup)
bot.edit_message_text("Ecco un elenco delle liste attualmente alle quali sei iscritto al momento:\n(Per rimuovere la sottoscrizione, è sufficiente \"tapparla\" e confermare)" , call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
return
#Just go away
bot.answer_callback_query(call.id, text="Just go away", show_alert=False, cache_time=999999)
elif "cusub-" in call.data:
if call.from_user.id == call.message.reply_to_message.from_user.id:
if user["Status"] == UserStatus.ACTIVE :
splittedString = call.data.split('-')
if len(splittedString) == 2:
if splittedString[1].isdigit():
listID=int(splittedString[1])
#Remove subscription
success = UnubscribeUserFromList(call.from_user.id, listID)
if success:
bot.answer_callback_query(call.id, text="✅ Disiscritto", show_alert=False)
else:
bot.answer_callback_query(call.id, text="❌ Si è verificato un errore", show_alert=False)
#Edit message back to list
lists = SubscribedLists(call.from_user.id)
markup = telebot.types.InlineKeyboardMarkup()
#Print the lists as inline buttons
if len(lists) == 0:
bot.edit_message_reply_markup(call.message.chat.id , call.message.message_id, call.id)
for ulist in lists:
# sub-{id} => subscript to list {id}
markup.row(telebot.types.InlineKeyboardButton(ulist["Name"], callback_data="usub-"+str(ulist["ID"])))
#If there are still lists, print the page delimiter
if len(lists) > Settings.subscriptionRows-1:
previousArrow = telebot.types.InlineKeyboardButton(f"⬅️", callback_data=f"ousub-"+str(int(actualOffset) - Settings.subscriptionRows+1))
nextArrow = telebot.types.InlineKeyboardButton(f"➡️", callback_data=f"ousub-"+str(int(actualOffset) + Settings.subscriptionRows-1))
emptyArrow = telebot.types.InlineKeyboardButton(" ", callback_data="ignore")
leftButton, rightButton = emptyArrow,emptyArrow
#Check if there are more list
if SubscribedLists(call.from_user.id, limit=1, offset=int(Settings.subscriptionRows)) != False:
rightButton = nextArrow
markup.row(leftButton, rightButton)
#msg = bot.reply_to(message, msg, reply_markup=markup)
bot.edit_message_reply_markup(call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
return
bot.answer_callback_query(call.id, text="Just go away", show_alert=False, cache_time=999999)
elif "usub-" in call.data:
if call.from_user.id == call.message.reply_to_message.from_user.id:
if user["Status"] == UserStatus.ACTIVE :
#Show next n rows + offset, osub-{offset}
#Safe data checks
splittedString = call.data.split('-')
if len(splittedString) == 2:
if splittedString[1].isdigit():
listID=int(splittedString[1])
msg="Sei sicuro di volerti disiscrivere dalla lista \"" + GetListName(listID) + "\"?"
markup = telebot.types.InlineKeyboardMarkup()
markup.row(
telebot.types.InlineKeyboardButton(f"⬅️ No", callback_data=f"ousub-0"),
telebot.types.InlineKeyboardButton(f"⚠️ Disiscriviti", callback_data=f"cusub-"+str(listID))
)
bot.edit_message_text(msg , call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
return
bot.answer_callback_query(call.id, text="Just go away", show_alert=False, cache_time=999999)
elif "osub-" in call.data:
if call.from_user.id == call.message.reply_to_message.from_user.id:
if user["Status"] == UserStatus.ACTIVE :
#Show next n rows + offset, osub-{offset}
#Safe data checks
splittedString = call.data.split('-')
if len(splittedString) == 2:
if splittedString[1].isdigit():
actualOffset=int(splittedString[1])
if actualOffset%(Settings.subscriptionRows-1) == 0:
lists = AvailableListsToUser(call.from_user.id, offset=int(actualOffset))
markup = telebot.types.InlineKeyboardMarkup()
#Print the lists as inline buttons
if len(lists) == 0:
bot.edit_message_text("Al momento non è presente nessuna lista" , call.message.chat.id , call.message.message_id, call.id)
return
for ulist in lists:
# sub-{id} => subscript to list {id}
markup.row(telebot.types.InlineKeyboardButton(ulist["Name"], callback_data="sub-"+str(ulist["ID"])))
#If there are still lists, print the page delimiter
if len(lists) > Settings.subscriptionRows-1:
previousArrow = telebot.types.InlineKeyboardButton(f"⬅️", callback_data=f"osub-"+str(int(actualOffset) - Settings.subscriptionRows+1))
nextArrow = telebot.types.InlineKeyboardButton(f"➡️", callback_data=f"osub-"+str(int(actualOffset) + Settings.subscriptionRows-1))
emptyArrow = telebot.types.InlineKeyboardButton(" ", callback_data="ignore")
leftButton, rightButton = emptyArrow,emptyArrow
#Check if there are more list
if AvailableListsToUser(call.from_user.id, limit=1, offset=int(actualOffset+Settings.subscriptionRows)) != False:
rightButton = nextArrow
if actualOffset - Settings.subscriptionRows +2 > 0:
leftButton = previousArrow
markup.row(leftButton, rightButton)
#msg = bot.reply_to(message, msg, reply_markup=markup)
bot.edit_message_reply_markup(call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
return
#Just go away
bot.answer_callback_query(call.id, text="Just go away", show_alert=False, cache_time=999999)
elif "sub-" in call.data:
#Subscribe to list sub-{id}
if call.from_user.id == call.message.reply_to_message.from_user.id:
if user["Status"] == UserStatus.ACTIVE :
#Show next n rows + offset, osub-{offset}
#Safe data checks
splittedString = call.data.split('-')
if len(splittedString) == 2:
if splittedString[1].isdigit():
listID=int(splittedString[1])
success = SubscribeUserToList(call.from_user.id, listID)
if success:
bot.answer_callback_query(call.id, text="✅ Sottoscritto", show_alert=False)
else:
bot.answer_callback_query(call.id, text="❌ Si è verificato un errore", show_alert=False)
#update message
lists = AvailableListsToUser(call.from_user.id)
markup = telebot.types.InlineKeyboardMarkup()
#Print the lists as inline buttons
for ulist in lists:
# sub-{id} => subscript to list {id}
markup.row(telebot.types.InlineKeyboardButton(ulist["Name"], callback_data="sub-"+str(ulist["ID"])))
#If there are still lists, print the page delimiter
nextArrow = telebot.types.InlineKeyboardButton(f"➡️", callback_data=f"osub-"+str(Settings.subscriptionRows-1))
emptyArrow = telebot.types.InlineKeyboardButton(" ", callback_data="ignore")
leftButton, rightButton = emptyArrow,emptyArrow
#Check if there are more list
if AvailableListsToUser(call.from_user.id, limit=1, offset=Settings.subscriptionRows-1) != False:
rightButton = nextArrow
markup.row(leftButton, rightButton)
#msg = bot.reply_to(message, msg, reply_markup=markup)
bot.edit_message_reply_markup(call.message.chat.id , call.message.message_id, call.id, reply_markup=markup)
return
@bot.inline_handler(func=lambda chosen_inline_result: True)
def getUserBioInlineQuery(inline_query):
user = inline_query.query.lower()
responses = []
usersIDs = getUsersIdLike(user)
for userid in usersIDs:
userNick = GetUserNickname(userid[0])
responses.append(
telebot.types.InlineQueryResultArticle(len(responses)+1, userNick[0].upper() + userNick[1:] + "'s Bio: " + GetUserBio(userid[0]), telebot.types.InputTextMessageContent(userNick[0].upper() + userNick[1:] + "'s Biography is \"" +GetUserBio(userid[0]) + "\""))
)
bot.answer_inline_query(inline_query.id, responses)
# Query message is text
###
#Starts the bot
###
bot.polling()