-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparking_bot.py
More file actions
430 lines (382 loc) · 15.9 KB
/
Copy pathparking_bot.py
File metadata and controls
430 lines (382 loc) · 15.9 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
from argparse import ArgumentParser
from json import dump, dumps, load, loads
from logging import INFO, basicConfig, getLogger
from subprocess import run
from emoji import emojize
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.error import Unauthorized
from telegram.ext import (
CallbackContext,
CallbackQueryHandler,
CommandHandler,
PicklePersistence,
Updater,
)
from structures.parking import Parking as Parking
from structures.stats import Stats as Stats
def start(update: Update, context: CallbackContext) -> None:
"""Send first messages to user."""
if (
config["whitelist"]
and str(update.effective_user.id) in users
or not config["whitelist"]
):
manage_user(update, context)
parking = context.bot_data["parking"]
markup = make_keyboard(context, str(update.effective_user.id))
welcome = (
r"Вас приветствует *Парковочный бот Logrocon*\!"
+ "\nВыберете место кнопками ниже"
)
update.effective_message.reply_text(welcome, parse_mode="MarkdownV2")
update.effective_message.reply_text(parking.state_text, reply_markup=markup)
log_event(update, "Отправил start")
def stop(update: Update, context: CallbackContext) -> None:
"""Say goodbye, delete user data."""
if (
config["whitelist"]
and str(update.effective_user.id) in users
or not config["whitelist"]
):
try:
log_event(update, "Отправил stop")
update.effective_message.reply_text(
"Вы перестали получать уведомления. Вы можете в любой момент"
+ " вернутся к их получению командой /start."
)
manage_user(update, context, False)
except KeyError:
log_event(update, "Отправил stop повторно")
def parking_handler(update: Update, context: CallbackContext) -> None:
"""Handler for place selection buttons."""
if (
config["whitelist"]
and str(update.effective_user.id) in users
or not config["whitelist"]
):
manage_user(update, context)
number = update.callback_query.data
try:
parking = context.bot_data["parking"]
stats = context.bot_data["stats"]
for place in parking.places:
if place.number == number:
stats.count(place)
place.toggle_state(str(update.effective_user.id))
state = place.state
if state == "reserved":
action_text = "зарезервировал"
elif state == "occupied":
action_text = "занял"
elif state == "free":
action_text = "освободил"
update.callback_query.answer(f"Вы {action_text}и место {number}")
action = (
f"*{users[str(update.effective_user.id)]}* "
+ f"{action_text} место *{number}*"
)
update_state(update, context, action)
log_event(update, action)
except ValueError:
update.callback_query.answer(f"Место {number} не свободно!")
log_event(update, f"Нажал на несвободное место {number}")
def cancel_handler(update: Update, context: CallbackContext) -> None:
"""Handler for cancel reserve button."""
if (
config["whitelist"]
and str(update.effective_user.id) in users
or not config["whitelist"]
):
manage_user(update, context)
number = update.callback_query.data.split(".")[1]
parking = context.bot_data["parking"]
try:
for place in parking.places:
if place.number == number:
place.cancel_reserve(str(update.effective_user.id))
update.callback_query.answer(f"Вы отменили резерв места {number}")
action = (
f"*{users[str(update.effective_user.id)]}* "
+ f"отменил резерв места *{number}*"
)
update_state(update, context, action)
log_event(update, action)
except ValueError:
update.callback_query.answer(
"Используйте клавиатуру из последнего сообщения!"
)
log_event(update, "Пытался отменить резерв на старой клавиатуре")
def clear_handler(update: Update, context: CallbackContext) -> None:
"""Handler for clear parking button."""
if (
config["whitelist"]
and str(update.effective_user.id) in users
or not config["whitelist"]
):
manage_user(update, context)
try:
parking = context.bot_data["parking"]
places = parking.clear()
if places:
stats = context.bot_data["stats"]
for place in places:
stats.count(place)
update.callback_query.answer("Вы выбрали очистку парковки")
action = (
f"*{users[str(update.effective_user.id)]}* "
+ "очистил парковочное пространство"
)
update_state(update, context, action)
log_event(update, action)
except ValueError:
update.callback_query.answer(
"Используйте клавиатуру из последнего сообщения!"
)
log_event(update, "Пытался очистить парковку на старой клавиатуре")
def statistics_handler(update: Update, context: CallbackContext) -> None:
"""Handler for statistic button."""
if (
config["whitelist"]
and str(update.effective_user.id) in users
or not config["whitelist"]
):
manage_user(update, context)
update.callback_query.answer("Вы запросили статистику")
stats = context.bot_data["stats"]
update_state(update, context, stats.message_text, True)
log_event(update, "Запросил статистику")
def update_state(
update: Update, context: CallbackContext, info: str, personal=False
) -> None:
"""Sends personal or bulk messages to users.
Args:
update: for identifying users and getting bot for bulk send.
context: for getting bot and user data.
info: info string for info message.
personal (optional): should this message be personal only.
Defaults to False.
"""
parking = context.bot_data["parking"]
if personal:
message_receivers = [str(update.effective_user.id)]
else:
message_receivers = users
for user in message_receivers:
markup = make_keyboard(context, user)
try:
update.effective_message.bot.send_message(
text=info, chat_id=user, parse_mode="MarkdownV2"
)
update.effective_message.bot.send_message(
text=parking.state_text, chat_id=user, reply_markup=markup
)
log_event(update, f"Отправили уведомление {users[user]}")
except Unauthorized:
log_event(update, f"Пользователь {users[user]} заблокировал бота")
def make_keyboard(context: CallbackContext, user_id: str) -> InlineKeyboardMarkup:
"""Making of personalized keyboards."""
keyboard = []
parking = context.bot_data["parking"]
for place in parking.state:
place_sign, state, number, occupant = place
if occupant is not None:
person = users[str(occupant)]
else:
person = "место свободно"
caption = " ".join([place_sign, number, person])
place_button = InlineKeyboardButton(caption, callback_data=number)
cancel_button = InlineKeyboardButton(
" ".join([emojize(":right_arrow_curving_left:"), "Отменить резерв"]),
callback_data="".join(["cancel.", number]),
)
clear_button = InlineKeyboardButton(
" ".join([emojize(":FREE_button:"), "Очистить парковку"]),
callback_data="clear",
)
statistics_button = InlineKeyboardButton(
" ".join([emojize(":bar_chart:"), "Статистика"]), callback_data="statistics"
)
if state == "reserved" and occupant == user_id:
keyrow = []
keyrow.append(place_button)
keyrow.append(cancel_button)
keyboard.append(keyrow)
else:
keyboard.append([place_button])
if not parking.is_free:
keyrow = []
keyrow.append(clear_button)
keyrow.append(statistics_button)
keyboard.append(keyrow)
else:
keyboard.append([statistics_button])
return InlineKeyboardMarkup(keyboard)
def manage_user(update: Update, context: CallbackContext, check=True) -> None:
"""Managing users.
Args:
check (optional): if not check - than it's user remove.
Defaults to True.
"""
user_id = str(update.effective_user.id)
if check:
if update.effective_user.full_name is None:
username = update.effective_user.username
else:
username = update.effective_user.full_name
# Replace for telegram markdown v2
for ch in [
"_",
"*",
"[",
"]",
"(",
")",
"~",
"`",
">",
"#",
"+",
"-",
"=",
"|",
"{",
"}",
".",
"!",
]:
username = username.replace(ch, "")
if user_id not in users or users[user_id] != username:
users[user_id] = username
save_json(config["users_file"], users)
stats = context.bot_data["stats"]
stats.update_users(users)
log_event(update, "Добавили пользователя")
elif not check:
users.pop(user_id)
save_json(config["users_file"], users)
log_event(update, "Удалили пользователя")
def log_event(update: Update, action: str) -> None:
try:
username = f"{users[str(update.effective_user.id)]}"
except KeyError:
username = update.effective_user.username
action = action.replace("*", "")
logger.log(INFO, f"{username} - {action}")
def load_json(filename: str) -> dict:
try:
with open(filename) as file:
data = load(file)
return data
except FileNotFoundError:
exit(f'File "{filename}" does not exist')
def save_json(filename: str, data: dict) -> None:
with open(filename, "w") as file:
dump(data, file, indent=4, sort_keys=True)
def get_config() -> dict:
parser = ArgumentParser(prog="Logrocon Parking Bot v.3")
parser.add_argument(
"-c", "--config", default="config.json", metavar="C", help="config file name"
)
args = vars(parser.parse_args())
config = load_json(args["config"])
return config
def toggle_whitelist(update: Update, context: CallbackContext) -> None:
"""Toggling bot's whitelist on and off by bot owner."""
if update.effective_user.id == config["owner_id"]:
config["whitelist"] = not config["whitelist"]
update.effective_message.reply_text(
"Whitelist mode: " + f'{config["whitelist"]}'
)
log_event(update, "Переключил режим whitelist на " + f'{config["whitelist"]}')
else:
log_event(update, "Отправил whitelist, хотя не должен о ней знать")
def get_logs(update: Update, context: CallbackContext) -> None:
"""Getting logs by messages from bot by bot owner."""
if update.effective_user.id == config["owner_id"]:
if not context.args:
length = config["logging"]["log_length"]
log_event(update, f"Отправил logs без аргументов, берем {length}")
else:
length = context.args[0]
log_event(update, f"Отправил logs с аргументом {length}")
result = run(
["tail", "-n", length, config["logging"]["log_file"]],
capture_output=True,
universal_newlines=True,
)
log = result.stdout
if len(log) > 4096:
for x in range(0, len(log), 4096):
update.effective_message.reply_text(log[x : x + 4096])
else:
update.effective_message.reply_text(log)
else:
log_event(update, "Отправил logs, хотя не должен о ней знать")
def get_stats(update: Update, context: CallbackContext) -> None:
"""Getting statistics by message from bot by bot owner."""
if update.effective_user.id == config["owner_id"]:
update.effective_message.reply_text(
dumps(context.bot_data["stats"].as_dict, indent=4)
)
log_event(update, "Экспортировал статистику в json")
else:
log_event(update, "Отправил get_stats, хотя не должен о ней знать")
def set_stats(update: Update, context: CallbackContext) -> None:
"""Setting statistics by message from bot owner to bot."""
if update.effective_user.id == config["owner_id"]:
if not context.args:
update.effective_message.reply_text("Отсутствуют аргументы")
log_event(update, f"Отправил set_stats без аргументов")
else:
stats = ""
for item in context.args:
stats = stats + " " + item
context.bot_data["stats"].as_dict = loads(stats)
update.effective_message.reply_text("Статистика импортирована")
log_event(update, "Импортировал статистику из json")
else:
log_event(update, "Отправил set_stats, хотя не должен о ней знать")
config = get_config()
"""dict: all config options."""
users = load_json(config["users_file"])
"""dict: bot users."""
# Set logging
log_format = "%(asctime)s %(levelname)s %(name)s %(message)s"
basicConfig(filename=config["logging"]["log_file"], format=log_format, level=INFO)
logger = getLogger(__name__)
handlers = [
CommandHandler("start", start),
CommandHandler("stop", stop),
CallbackQueryHandler(cancel_handler, pattern="cancel.*"),
CallbackQueryHandler(clear_handler, pattern="clear"),
CallbackQueryHandler(statistics_handler, pattern="statistics"),
CallbackQueryHandler(parking_handler),
CommandHandler("whitelist", toggle_whitelist),
CommandHandler("logs", get_logs, pass_args=True),
CommandHandler("get_stats", get_stats),
CommandHandler("set_stats", set_stats, pass_args=True),
]
def main():
updater = Updater(
token=config["token"],
persistence=PicklePersistence(
filename=config["data_file"],
store_chat_data=False,
store_user_data=False,
on_flush=False,
),
)
dispatcher = updater.dispatcher
dispatcher.bot_data["stats"] = dispatcher.bot_data.get("stats", Stats(users))
dispatcher.bot_data["parking"] = dispatcher.bot_data.get(
"parking", Parking(config["places"])
)
# Create new parking if places in config changed
if [x[2] for x in dispatcher.bot_data["parking"].state] != config["places"]:
dispatcher.bot_data["parking"] = Parking(config["places"])
for handler in handlers:
dispatcher.add_handler(handler)
updater.start_polling(drop_pending_updates=True)
updater.idle()
if __name__ == "__main__":
main()