-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_interaction.py
More file actions
53 lines (40 loc) · 1.58 KB
/
Copy pathuser_interaction.py
File metadata and controls
53 lines (40 loc) · 1.58 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
import json
class UserInteraction:
FILE_PATH = "users.json"
@classmethod
def add_user(cls, user_id):
try:
with open(cls.FILE_PATH, "r") as file:
data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
data = []
if user_id in data:
return "уже привязан к уведомлениям"
data.append(user_id)
with open(cls.FILE_PATH, 'w') as file:
json.dump(data, file)
return "успешно привязан к уведомлениям"
@classmethod
def get_alert(cls, announcement="ТРЕВОГА"):
try:
with open(cls.FILE_PATH, 'r') as file:
data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
data = []
mentions = [f"[ping](tg://user?id={uid})" for uid in data]
message = ", ".join(mentions)
return f"{message}\n{announcement}"
@classmethod
def remove_user(cls, user_id):
try:
with open(cls.FILE_PATH, "r") as file:
data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
data = []
if user_id in data:
data.remove(user_id)
with open(cls.FILE_PATH, 'w') as file:
json.dump(data, file)
return "Успех!"
else:
return "Вы не были подписаны на уведомления"