/
Bot.py
283 lines (228 loc) · 9.56 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
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
# coding=utf-8
import re
from vk_api.keyboard import VkKeyboard
from vk_api.utils import get_random_id
from vk_api.exceptions import ApiError
from Duty import Duty
from StateMachine import State, States
from Data import Data, Room
from datetime import datetime, date, time
class Bot:
def __init__(self, vk, peer):
self.state = State()
self.rooms = Data.getRooms()
self.locations = Data.getLocations()
self.peer = str(peer)
self.vk = vk
self.duty: Duty = None
self.new_duty: Duty = None
self.old_duty: Duty = None
# Remind Duty
self.CURRENT_LOCATION = ''
self.CURRENT_COMMENT = ''
# Update Duty
self.CURRENT_DUTY = []
self.CURRENT_ROOMS = []
self.CURRENT_LOCS = []
def updateDuty(self, duty: Duty):
self.duty = duty
@staticmethod
def createKeyboard(keyboard, list, elements_per_line=1):
if elements_per_line == 1:
for element in list[0:-1]:
keyboard.add_button(element)
keyboard.add_line()
keyboard.add_button(list[-1])
return keyboard
elif elements_per_line == 2:
i = 1
is_odd = len(list) % 2 == 0
end_point = - 2
if not is_odd:
end_point = -1
for element in list[0:end_point]:
keyboard.add_button(element)
if i == 2:
keyboard.add_line()
i = 0
i += 1
if is_odd:
keyboard.add_button(list[-2])
keyboard.add_button(list[-1])
else:
keyboard.add_button(list[-1])
return keyboard
def getResponseData(self, args=None):
keyboard = VkKeyboard()
message = ''
# Main Menu State
if self.state.getType() == States.Menu:
keyboard = self.__startMenuKeyboard()
message = 'Выберите действие'
# Main Remind Duty State
elif self.state.getType() == States.RemindDuty:
if self.state.getState() == State.STATE[States.RemindDuty]['ChoiceLocation']:
keyboard = Bot.createKeyboard(keyboard, self.locations)
message = 'Выберите локацию'
if self.state.getState() == State.STATE[States.RemindDuty]['Comment']:
keyboard = Bot.createKeyboard(keyboard, ['Отправить без комментария'])
message = 'Введите комментарий и нажмите отправить'
self.CURRENT_LOCATION = args
elif self.state.getState() == State.STATE[States.RemindDuty]['End']:
self.CURRENT_COMMENT = args
keyboard = self.__startMenuKeyboard()
message = self.__RemindDutyEndAction(self.CURRENT_LOCATION, self.CURRENT_COMMENT)
self.CURRENT_LOCATION = ''
self.CURRENT_COMMENT = ''
# Main Show duty State
elif self.state.getType() == States.ShowDuty:
if self.state.getState() == State.STATE[States.ShowDuty]['End']:
keyboard = self.__startMenuKeyboard()
message = self.__ShowDutyEndAction()
# Main Update Duty State
elif self.state.getType() == States.UpdateDuty:
if self.state.getState() == State.STATE[States.UpdateDuty]['SelectLocation']:
if not Data.isModerator(self.peer):
self.state.setStartState()
keyboard = Bot.createKeyboard(keyboard, list(State.STATE_TYPES_NAMES.keys()))
message = 'У вас недостаточно прав доступа'
return {
'keyboard': keyboard.get_keyboard(),
'message': message
}
if args in State.STATE_TYPES_NAMES.keys():
self.CURRENT_ROOMS = [room.rooms_name for room in self.rooms]
self.CURRENT_LOCS = self.locations
else:
self.CURRENT_LOCS.remove(args)
self.CURRENT_DUTY.append(args)
message = 'Выберите место: '
keyboard = Bot.createKeyboard(keyboard, self.CURRENT_ROOMS, elements_per_line=2)
if self.state.getState() == State.STATE[States.UpdateDuty]['SelectRoom']:
self.CURRENT_ROOMS.remove(args)
self.CURRENT_DUTY.append(args)
print(self.CURRENT_ROOMS)
message = 'Выберите дежурных: '
keyboard = Bot.createKeyboard(keyboard, self.CURRENT_LOCS, elements_per_line=2)
if len(self.CURRENT_LOCS) > 0:
self.state.currentState -= 2
else:
self.state.setStartState()
return self.getResponseData(args)
if self.state.isOnLastState():
self.state.next()
return {
'keyboard': keyboard.get_keyboard(),
'message': message
}
def send(self, args=None):
self.state.next(args)
self.logState(args)
responseData = self.getResponseData(args)
self.send_message(self.peer,
responseData['message'],
responseData['keyboard'])
def logState(self, args=None):
print('Type: {}\n State: {}\n args: {}\n peer: {}\n\n'.format(self.state.getType(), self.state.getState(), args,
self.peer))
def send_message(self, peer, message, keyboard=None, name=None):
try:
if keyboard is not None:
if re.match(r'[\d]+', str(peer)) is not None:
self.vk.messages.send(
user_id=peer,
random_id=get_random_id(),
message=message,
keyboard=keyboard
)
else:
self.vk.messages.send(
domain=peer,
random_id=get_random_id(),
message=message,
keyboard=keyboard
)
else:
if re.match('[\d]+', str(peer)) is not None:
self.vk.messages.send(
user_id=peer,
random_id=get_random_id(),
message=message,
)
else:
self.vk.messages.send(
domain=peer,
random_id=get_random_id(),
message=message,
)
except ApiError as e:
print(e.args)
print(e.values)
self.send_message(self.peer, 'Кажется, резидент {} с id: {} еще не зарегистрировался.'
' \n Пожалуйста, перешлите это сообщение @mistleet'.format(str(name), str(peer)))
return False
return True
def __startMenuKeyboard(self):
keyboard = VkKeyboard()
if not Data.isModerator(self.peer):
keyboard = Bot.createKeyboard(keyboard, list(State.STATE_TYPES_NAMES.keys())[:-1])
else:
keyboard = Bot.createKeyboard(keyboard, list(State.STATE_TYPES_NAMES.keys()))
return keyboard
def __RemindDutyEndAction(self, location, comment):
room: Room = self.duty.duty[location]
message = ''
for name, id in room.room.items():
result = self.send_message(
id,
'Привет, {}, кажется, {} стоит уделить внимание!'.format(Helper.BuildPersonLink(id, name), location) +
'\n' + Helper.BuildComment(comment),
name=name
)
if result:
message += '{} уведомлен(a) \n'.format(name)
if message == '':
message = '...'
return message
def __ShowDutyEndAction(self):
message = ''
for location, room in self.duty.duty.items():
message += '{} ------ {}\n'.format(
room.rooms_name, location
)
return message
class Task:
def __init__(self, task_text, task_author, task_perfomer):
self.task_text = task_text
self.task_author = task_author
self.task_perfomer = task_perfomer
self.task_create_date = datetime.now().__str__()
self.task_close_date = 0
self.task_comment = ''
def Log(self):
f = open('log.txt', 'a')
f.write(
'!' + '=' * 10 + '!' + '\n' +
'Task: {}'.format(self.task_text) + '\n' +
'To: {}'.format(self.task_perfomer) + '\n' +
'From: {}'.format(self.task_author) + '\n' +
'Create Date ' + self.task_create_date + '\n' +
'Close Date' + datetime.now().__str__()
)
class Helper:
_url = 'https://cataas.com/cat/'
@staticmethod
def BuildComment(comment):
if comment != '' and comment != 'Отправить без комментария':
return 'Комментарий: ' + comment
else:
return ''
@staticmethod
def BuildPersonLink(id, name):
if re.match('[\d]+', str(id)) is not None:
return '[id{}|{}]'.format(id, name)
else:
return '@{}'.format(id)
@staticmethod
def getCat(tag='cute'):
pass