-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
202 lines (173 loc) · 7.27 KB
/
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
import logging
import telebot
from bank import Bank
from database.deposit_bot_database import SQLDatabase
import windows
import exceptions
from constants import config, errorquotes, text_templates, responses
logging.basicConfig(filename=config.LOGS_PATH,
format=config.LOGS_FORMAT,
level=logging.INFO)
class Bot(telebot.TeleBot):
def __init__(self, token, threaded=True):
super().__init__(token, threaded=threaded)
self.logger = telebot.logger
self.logger.setLevel(logging.INFO)
self.logger.info('Starting up...')
self._db = SQLDatabase(self, config.DATABASE_PATH)
self._db.setup()
self.logger.info('Database started up.')
self.users = dict()
self.chats = dict()
self.banks = dict()
self.windows = dict()
self._init_users()
self._init_chats()
self._init_banks()
self.user_connections = dict(self._db.get_user_connections())
self._init_windows()
self.logger.info('Running...')
def _init_users(self):
assert not bool(self.users)
for user in self._db.get_users_list():
self.users[user.id] = user
self.logger.info('Users initialized.')
def _init_chats(self):
assert not bool(self.chats)
for chat in self._db.get_chats_list():
self.chats[chat.id] = chat
self.logger.info('Chats initialized.')
def _init_banks(self):
assert not bool(self.banks)
for chat_id in self._db.get_chats_with_banks():
self.banks[chat_id] = self._db.get_bank(chat_id)
self.logger.info('Banks initialized.')
def _init_windows(self):
assert not bool(self.windows)
for window_ in self._db.get_windows_list():
self.windows[window_.user.id] = window_
if isinstance(window_, windows.ConnectWindow):
self.switch_to_connect_window(window_.user)
try:
self.windows[window_.user.id].update_window()
except telebot.apihelper.ApiException as exception:
self.logger.warning(exception)
self.logger.info('Windows initialized.')
def scan_user(self, user):
if user.id not in self.users.keys():
self._db.insert_new_user(user)
self.users[user.id] = user
self.logger.info('New user: {}'.format(user))
def scan_message(self, message):
self.logger.info(text_templates.MESSAGE_LOG.format(message.text,
message.from_user))
self.scan_user(message.from_user)
if message.chat.id not in self.chats.keys():
self._db.insert_new_chat(message.chat)
self.chats[message.chat.id] = message.chat
self.logger.info('New chat: {}'.format(message.chat))
def create_new_bank(self, chat, owner):
assert chat.id not in self.banks.keys()
self._db.insert_new_bank(chat, owner)
self.banks[chat.id] = Bank(chat, owner, self._db)
def connect_user(self, user, chat):
self._db.connect_user(user, chat)
self.user_connections[user.id] = chat.id
def get_active_bank(self, user):
""" Returns Bank object which user is connected to.
Throws exception.BankNotFoundError if such bank
was not found. """
try:
return self.banks[self.user_connections[user.id]]
except KeyError as exception:
raise exceptions.BankNotFoundError(
errorquotes.BANK_NOT_FOUND_USER.format(str(user))
) from exception
def get_chat_bank(self, chat):
try:
return self.banks[chat.id]
except KeyError as exception:
raise exceptions.BankNotFoundError(
errorquotes.BANK_NOT_FOUND_CHAT.format(chat)
) from exception
def get_window(self, user):
""" Returns Window object which user is connected to.
Throws window.WindowNotFoundError if such window
was not found. """
try:
return self.windows[user.id]
except KeyError as exception:
raise exceptions.WindowNotFoundError(
errorquotes.WINDOW_NOT_FOUND.format((user))
) from exception
def bank_info(self, user):
""" Returns text. """
bank = self.get_active_bank(user)
text = text_templates.BANK_INFO.format(bank.title(),
bank.owner_name(),
bank.current_sum())
return text
def create_window(self, for_user, chat, text):
if chat.type != 'private':
raise exceptions.WrongChatError(errorquotes.WINDOW_PRIVATE_ONLY)
sent_message = self.send_message(chat.id, responses.WINDOW_LOADING)
self.windows[for_user.id] = windows.DefaultWindow(
bot=self,
user=for_user,
chat=chat,
message_id=sent_message.message_id
)
self._db.insert_new_window(self.windows[for_user.id])
self.windows[for_user.id].update_window(text)
def switch_to_default_window(self, user):
try:
self.windows[user.id] = windows.DefaultWindow.from_window(
self.windows[user.id]
)
except KeyError as exception:
raise exceptions.WindowNotFoundError(
errorquotes.WINDOW_NOT_FOUND.format(user)
) from exception
def switch_to_keyboard_window(self, user):
try:
self.windows[user.id] = windows.KeyboardWindow.from_window(
self.windows[user.id]
)
except KeyError as exception:
raise exceptions.WindowNotFoundError(
errorquotes.WINDOW_NOT_FOUND.format(user)
) from exception
def switch_to_history_window(self, user):
try:
self.windows[user.id] = windows.HistoryWindow.from_window(
self.windows[user.id]
)
except KeyError as exception:
raise exceptions.WindowNotFoundError(
errorquotes.WINDOW_NOT_FOUND.format(user)
) from exception
def switch_to_connect_window(self, user):
try:
current_chat = self.chats[self.user_connections[user.id]]
except KeyError as exception:
raise exceptions.BankNotFoundError(
errorquotes.BANK_NOT_FOUND_USER.format(user)
) from exception
available_chats = self._db.get_available_chats(user)
try:
self.windows[user.id] = windows.ConnectWindow.from_window(
self.windows[user.id]
)
except KeyError as exception:
raise exceptions.WindowNotFoundError(
errorquotes.WINDOW_NOT_FOUND.format(user)
) from exception
self.windows[user.id].set_chats(current_chat,
available_chats)
def update_window_record(self, window_):
self._db.update_window(window_)
def get_event_description(self, event_id):
return self._db.get_event(event_id).description
def log_query(self, query):
self.logger.info(text_templates.QUERY_LOG.format(query.data,
query.from_user))