-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathchatbot_sql.py
102 lines (81 loc) · 2.61 KB
/
chatbot_sql.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
from sqlalchemy import Column, String, UnicodeText
from . import BASE, SESSION
class ChatBot(BASE):
__tablename__ = "chatbot"
chat_id = Column(String(14), primary_key=True)
user_id = Column(String(14), primary_key=True, nullable=False)
chat_name = Column(UnicodeText)
user_name = Column(UnicodeText)
user_username = Column(UnicodeText)
chat_type = Column(UnicodeText)
def __init__(
self, chat_id, user_id, chat_name, user_name, user_username, chat_type
):
self.chat_id = str(chat_id)
self.user_id = str(user_id)
self.chat_name = chat_name
self.user_name = user_name
self.user_username = user_username
self.chat_type = chat_type
def __eq__(self, other):
return bool(
isinstance(other, ChatBot)
and self.chat_id == other.chat_id
and self.user_id == other.user_id
)
ChatBot.__table__.create(checkfirst=True)
def is_added(chat_id, user_id):
try:
return SESSION.query(ChatBot).get((str(chat_id), str(user_id)))
except BaseException:
return None
finally:
SESSION.close()
def get_users(chat_id):
try:
return SESSION.query(ChatBot).filter(ChatBot.chat_id == str(chat_id)).all()
finally:
SESSION.close()
def get_all_users():
try:
return SESSION.query(ChatBot).all()
except BaseException:
return None
finally:
SESSION.close()
def addai(chat_id, user_id, chat_name, user_name, user_username, chat_type):
to_check = is_added(chat_id, user_id)
if not to_check:
adder = ChatBot(
str(chat_id), str(user_id), chat_name, user_name, user_username, chat_type
)
SESSION.add(adder)
SESSION.commit()
return True
rem = SESSION.query(ChatBot).get((str(chat_id), str(user_id)))
SESSION.delete(rem)
SESSION.commit()
adder = ChatBot(
str(chat_id), str(user_id), chat_name, user_name, user_username, chat_type
)
SESSION.add(adder)
SESSION.commit()
return False
def remove_ai(chat_id, user_id):
to_check = is_added(chat_id, user_id)
if not to_check:
return False
rem = SESSION.query(ChatBot).get((str(chat_id), str(user_id)))
SESSION.delete(rem)
SESSION.commit()
return True
def remove_users(chat_id):
saved_filter = SESSION.query(ChatBot).filter(ChatBot.chat_id == str(chat_id))
if saved_filter:
saved_filter.delete()
SESSION.commit()
def remove_all_users():
saved_filter = SESSION.query(ChatBot)
if saved_filter:
saved_filter.delete()
SESSION.commit()