-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
472 lines (336 loc) · 18.3 KB
/
main.py
File metadata and controls
472 lines (336 loc) · 18.3 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
from typing import Optional
from const_variables import *
import telegram
import datetime
import logging
import format_helper
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, ReplyKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Filters, CallbackQueryHandler, \
JobQueue, ConversationHandler
from package import Package
from databases.users import UsersDB
from texts import *
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('main')
logger.setLevel(GLOBAL_LOGGER_LEVEL)
users = UsersDB()
EDIT_NAME = range(1)
def get_keyboard_track(barcode, is_tracked=False, is_show_all_track=True):
keyboard = []
if is_show_all_track:
keyboard.append([InlineKeyboardButton('Показать полный путь', callback_data='show_all_route_' + barcode)])
else:
keyboard.append(
[InlineKeyboardButton('Показать последнее изменение', callback_data='show_short_route_' + barcode)])
keyboard.append([InlineKeyboardButton('Редактировать название', callback_data='edit_name_' + barcode)])
if is_tracked:
keyboard.append([InlineKeyboardButton('Перестать отслеживать',
callback_data=f'remove_from_tracked_{barcode}_{int(is_show_all_track)}')])
else:
keyboard.append(
[InlineKeyboardButton('Отслеживать', callback_data=f'add_to_tracked_{barcode}_{int(is_show_all_track)}')])
reply_markup = InlineKeyboardMarkup(keyboard)
return reply_markup
def get_keyboard_my_packages():
keyboard = [['Показать отслеживаемое']]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)
return reply_markup
def get_keyboard_remove_delivered():
keyboard = [[InlineKeyboardButton('Удалить доставленные', callback_data='remove_delivered')]]
reply_markup = InlineKeyboardMarkup(keyboard)
return reply_markup
def get_keyboard_rename():
keyboard = [[InlineKeyboardButton('Назад', callback_data='back')]]
reply_markup = InlineKeyboardMarkup(keyboard)
return reply_markup
def send_start_msg(update: Update, _: CallbackContext) -> None:
user_id = update.message.from_user.id
logger.info(f'Новое сообщение: /start или /help. пользователь:{user_id}')
users.add_user(user_id)
update.message.reply_text(TEXT_START,
reply_markup=get_keyboard_my_packages(),
disable_web_page_preview=True)
def send_count_users(update: Update, _: CallbackContext) -> None:
logger.info('Отправляем количество пользователей данного бота')
all_users_count = users.get_count_users()
active_users_count = users.get_count_active_users()
update.message.reply_text(f'Количество всех пользователей: {all_users_count}\n'
f'Количество активных пользователей (больше 2х посылок): {active_users_count}',
reply_markup=get_keyboard_my_packages(),
disable_web_page_preview=True)
def send_everyone_promo(_: Update, context: CallbackContext) -> None:
logger.info('Отправляем рекламу всем пользователяс')
users_id = users.db.keys()
for id in list(users_id):
try:
context.bot.send_message(chat_id=id, text=TEXT_PROMO,
reply_markup=get_keyboard_my_packages(),
disable_web_page_preview=True)
except telegram.error.Unauthorized as e:
logger.info(f'Пользователь заблокировал бота. user_id:{id}')
users.delete_user(id)
def send_package(update: Update, _: CallbackContext) -> None:
barcode = update.message.text
user_id = update.effective_user.id
if update.effective_user.link is not None:
__user = update.effective_user.link
elif update.effective_user.full_name is not None and update.effective_user.full_name != '':
__user = update.effective_user.full_name
else:
__user = user_id
logger.info(
f'Отправка истории передвижения посылки. пользователь:{__user}, трек-номер:{barcode}')
message = update.message.reply_text('🛳*Отслеживаю посылку...*', parse_mode=telegram.ParseMode.MARKDOWN)
package = Package(barcode)
is_tracked, name = users.get_package_specs(user_id, barcode)
output = format_helper.format_route_short(package, barcode, custom_name=name)
message.edit_text(text=output,
reply_markup=get_keyboard_track(barcode, is_tracked=is_tracked, is_show_all_track=True),
parse_mode=telegram.ParseMode.MARKDOWN)
def route_callback(update: Update, context: CallbackContext) -> Optional[range]:
query = update.callback_query
if 'show_all_route_' in query.data:
return send_all_history(update, context)
elif 'show_short_route_' in query.data:
return all_history_to_short(update, context)
elif 'edit_name_' in query.data:
return start_rename_package(update, context)
elif 'add_to_tracked_' in query.data:
return add_barcode_in_track(update, context)
elif 'remove_from_tracked_' in query.data:
return remove_barcode_in_track(update, context)
elif 'remove_delivered' in query.data:
return remove_delivered(update, context)
def send_all_history(update: Update, _: CallbackContext) -> None:
query = update.callback_query
query.answer()
user_id = query.from_user.id
barcode = query.data.replace('show_all_route_', '')
logger.debug(f'Отправка полной информации о трек-номере. трек-номер:{barcode}, пользователь:{user_id}')
package = Package(barcode)
is_tracked, name = users.get_package_specs(user_id, barcode)
output = format_helper.format_route(package, barcode, custom_name=name)
query.edit_message_text(text=output,
reply_markup=get_keyboard_track(barcode, is_tracked=is_tracked, is_show_all_track=False),
parse_mode=telegram.ParseMode.MARKDOWN)
def all_history_to_short(update: Update, _: CallbackContext) -> None:
query = update.callback_query
query.answer()
user_id = query.from_user.id
barcode = query.data.replace('show_short_route_', '')
logger.debug(f'Отправка базовой информации о трек-номере. пользователь:{user_id}, трек-номер:{barcode}')
package = Package(barcode)
is_tracked, name = users.get_package_specs(user_id, barcode)
output = format_helper.format_route_short(package, barcode, custom_name=name)
query.edit_message_text(text=output,
reply_markup=get_keyboard_track(barcode, is_tracked=is_tracked, is_show_all_track=True),
parse_mode=telegram.ParseMode.MARKDOWN)
def start_rename_package(update: Update, context: CallbackContext) -> range:
query = update.callback_query
query.answer()
barcode = query.data.replace('edit_name_', '')
user_id = query.from_user.id
logger.debug(f'Изменение названия посылки. пользователь:{user_id}, трек-номер:{barcode}')
output = 'Введите название для этой посылки'
context.user_data['rename_barcode'] = barcode
context.user_data['msg_id'] = query.message.message_id
query.edit_message_text(text=output,
reply_markup=get_keyboard_rename())
return EDIT_NAME
def cancel_rename_package(update: Update, context: CallbackContext) -> int:
user_id = update.effective_user.id
barcode = context.user_data['rename_barcode']
message_id = context.user_data['msg_id']
del context.user_data['rename_barcode']
del context.user_data['msg_id']
logger.debug(f'Название посылки не было изменено. пользователь:{user_id}')
package = Package(barcode)
is_tracked, name = users.get_package_specs(user_id, barcode)
output = format_helper.format_route_short(package, barcode, custom_name=name)
context.bot.edit_message_text(text=output,
chat_id=user_id,
message_id=message_id,
parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=get_keyboard_track(barcode, is_tracked=is_tracked,
is_show_all_track=True))
return ConversationHandler.END
def end_rename_package(update: Update, context: CallbackContext) -> int:
user_id = update.message.chat_id
name = update.message.text
barcode = context.user_data['rename_barcode']
del context.user_data['rename_barcode']
users.rename_barcode(user_id, barcode, name)
logger.debug(f'Название посылки изменено. пользователь:{user_id}, имя:{name}, трек-номер:{barcode}')
update.message.reply_text('Название посылки изменено', reply_markup=get_keyboard_my_packages())
return ConversationHandler.END
def add_barcode_in_track(update: Update, _: CallbackContext) -> None:
query = update.callback_query
query.answer()
user_id = query.from_user.id
barcode, is_show_all_track = query.data.replace('add_to_tracked_', '').split('_')
is_show_all_track = is_show_all_track == '1'
already_added = users.check_barcode(user_id=user_id, curr_barcode=barcode)
if already_added:
result = users.update_barcode(user_id=user_id, curr_barcode=barcode, tracked=True, save=True)
else:
result = users.add_barcode(user_id=user_id, curr_barcode=barcode, name='', tracked=True, save=True)
logger.debug(f'Добавление трек-номера в отслеживаемое. трек-номер:{barcode}, пользователь:{user_id}')
query.edit_message_reply_markup(
reply_markup=get_keyboard_track(barcode, is_tracked=result, is_show_all_track=is_show_all_track))
def remove_barcode_in_track(update: Update, _: CallbackContext) -> None:
query = update.callback_query
query.answer()
user_id = query.from_user.id
barcode, is_show_all_track = query.data.replace('remove_from_tracked_', '').split('_')
is_show_all_track = is_show_all_track == '1'
result = not users.update_barcode(user_id=user_id, curr_barcode=barcode, tracked=False)
logger.debug(f'Удаление трек-номера в отслеживаемое. barcode:{barcode}, user:{user_id}')
query.edit_message_reply_markup(
reply_markup=get_keyboard_track(barcode, is_tracked=result, is_show_all_track=is_show_all_track))
def send_new_package(barcode: str, package: Package, user_id: int, bot):
logger.debug(
f'Отправка нового обновления. трек-номер:{barcode}, пользователь:{user_id}')
is_tracked, name = users.get_package_specs(user_id, barcode)
output = format_helper.format_route_short(package, barcode, custom_name=name)
try:
res = bot.send_message(chat_id=user_id, text=output, parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=get_keyboard_track(barcode, is_tracked=is_tracked, is_show_all_track=True))
print(res)
return ''
except telegram.error.Unauthorized:
logger.info(f'Пользователь заблокировал бота. user_id:{user_id}')
return 'blocked'
def get_text_my_packages(user_id: int):
text = '🛳*Отслеживаемые посылки*\n\n'
barcodes = users.get_barcodes(user_id)
for curr_barcode in barcodes:
if not barcodes[curr_barcode]['Tracked']:
continue
package = Package(curr_barcode)
custom_name = users.get_package_specs(user_id, curr_barcode)[1]
parcel_name = 'Посылка' if (not custom_name and not package.name) \
else custom_name if custom_name else package.name
text += f'*{parcel_name} ({curr_barcode})*\n'
if len(package.history) > 0:
history = format_helper.format_history(package.history[-1])
if not history[0]:
return 'Error', history[1]
text += f'{history}\n\n'
else:
text += '\n'
return text
def send_my_packages(update: Update, context: CallbackContext) -> None:
user_id = update.effective_user.id
if update.effective_user.link is not None:
__user = update.effective_user.link
elif update.effective_user.full_name is not None and update.effective_user.full_name != '':
__user = update.effective_user.full_name
else:
__user = update.effective_user.id
logger.info(
f'Отправка всех отправлений:{__user}')
text = get_text_my_packages(user_id)
if text[0] == 'Error':
context.error = text[1]
return error_callback(update, context)
update.message.reply_text(text,
parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=get_keyboard_remove_delivered())
def remove_delivered(update: Update, context: CallbackContext) -> None:
query = update.callback_query
query.answer()
user_id = query.from_user.id
logger.debug(f'Удаление доставленных посылок. пользователь:{user_id}')
edit_message = False
barcodes = users.get_barcodes(user_id)
for curr_barcode in barcodes:
if not barcodes[curr_barcode]['Tracked']:
continue
package = Package(curr_barcode)
if package.is_delivered:
users.update_barcode(user_id=user_id, curr_barcode=curr_barcode, tracked=False, save=False)
edit_message = True
users.save()
text = get_text_my_packages(user_id)
if text[0] == 'Error':
context.error = text[1]
return error_callback(update, context)
if not edit_message:
logger.debug(f'Доставленных посылок не обнаружено. пользователь:{user_id}')
return
query.edit_message_text(text,
parse_mode=telegram.ParseMode.MARKDOWN,
reply_markup=get_keyboard_remove_delivered())
def check_new_update(context: CallbackContext):
logger.info('Началась проверка новый обновлений')
_users = users.db
_packages = Package.get_saved_packages()
_new_packages = {}
_new_version_old_packages = {}
_users_for_delete = []
for user_id in _users:
barcodes = _users[user_id]['barcodes']
for curr_barcode in barcodes:
if not barcodes[curr_barcode]['Tracked']:
continue
if curr_barcode in _new_packages:
package = Package.from_json(_new_packages[curr_barcode], curr_barcode)
res = send_new_package(barcode=curr_barcode, package=package, user_id=int(user_id),
bot=context.bot)
if res == 'blocked':
_users_for_delete.append(user_id)
continue
updated_package = Package.from_rpt(curr_barcode)
if curr_barcode in _packages and updated_package.history == _packages[curr_barcode]['History']:
if updated_package.features != _packages[curr_barcode]:
_new_version_old_packages[curr_barcode] = updated_package.features
continue
_new_packages[curr_barcode] = updated_package.features
res = send_new_package(barcode=curr_barcode, package=updated_package, user_id=int(user_id),
bot=context.bot)
if res == 'blocked':
_users_for_delete.append(user_id)
for user_id in _users_for_delete:
users.delete_user(user_id)
_new_packages.update(_new_version_old_packages)
Package.save_new_packages(_new_packages)
def error_callback(update: Update, context: CallbackContext):
error: Exception = context.error
logger.error(error, exc_info=True)
if update is not None and update.message is not None:
update.message.reply_text(TEXT_ERROR,
reply_markup=get_keyboard_my_packages(),
disable_web_page_preview=True)
def main() -> None:
# Create the Updater and pass it your bot token.
updater = Updater(token=TELEGRAM_BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', send_start_msg))
dispatcher.add_handler(CommandHandler('help', send_start_msg))
dispatcher.add_handler(CommandHandler('send_count_users', send_count_users))
dispatcher.add_handler(CommandHandler('send_everyone_promo', send_everyone_promo))
conv_handler = ConversationHandler(
entry_points=[CallbackQueryHandler(route_callback)],
states={
EDIT_NAME: [MessageHandler(Filters.text & ~Filters.command, end_rename_package)]
},
fallbacks=[CallbackQueryHandler(cancel_rename_package, pattern='back')],
)
dispatcher.add_handler(conv_handler)
dispatcher.add_handler(MessageHandler(Filters.text('Показать отслеживаемое'), send_my_packages))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, send_package))
dispatcher.add_handler(CallbackQueryHandler(route_callback))
dispatcher.add_error_handler(error_callback)
# Start the Bot
updater.start_polling()
j: JobQueue = updater.job_queue
j.run_daily(check_new_update, days=(0, 1, 2, 3, 4, 5, 6),
time=datetime.time(hour=10, minute=00, second=00))
# j.run_once(check_new_update, 30)
logger.info('Бот работает')
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
if __name__ == '__main__':
main()