-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtelegram_bot_users.py
More file actions
71 lines (49 loc) · 1.67 KB
/
telegram_bot_users.py
File metadata and controls
71 lines (49 loc) · 1.67 KB
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
__author__ = 'andheroe'
import simplejson
class User(object):
chat_id = None
def __init__(self, chat_id):
self.chat_id = chat_id
class TeamUser(User):
def __init__(self, chat_id):
super(TeamUser, self).__init__(chat_id)
class UserList(object):
def __init__(self):
try:
self.load_from_file()
except:
self.lst = []
def __contains__(self, key):
return key in [user.chat_id for user in self.lst]
def __iter__(self):
return iter(self.lst)
def check_type(self, obj):
pass
def save_to_file(self):
pass
def load_from_file(self):
pass
def add(self, user):
self.check_type(user)
self.lst.append(user)
self.save_to_file()
def remove_by_chat_id(self, chat_id):
self.lst = [user for user in self.lst if user.chat_id != chat_id]
self.save_to_file()
def get_by_chat_id(self, chat_id):
filtered_list = [user for user in self.lst if user.chat_id == chat_id]
return filtered_list[0] if 0 < len(filtered_list) else None
class TeamUserList(UserList):
lst = []
filename = 'telebot_users.json'
def __init__(self):
super(TeamUserList, self).__init__()
def check_type(self, obj):
if obj.__class__.__name__ != 'TeamUser':
raise Exception('TeamUsersList can save only TeamUser type objects')
def save_to_file(self):
simplejson.dump([user.chat_id for user in self.lst], open(self.filename, 'w'))
def load_from_file(self):
lst = simplejson.load(open(self.filename, 'r'))
for chat_id in lst:
self.lst.append(TeamUser(chat_id))