-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
748 lines (634 loc) · 23.5 KB
/
Copy pathbot.py
File metadata and controls
748 lines (634 loc) · 23.5 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
"""
Telegram bot working in groups and channels. Sends available information about
new members to group administrators.
"""
import datetime
import logging
import hashlib
import base64
import json
import math
import sys
import re
import os
import string
import secrets
import threading
import asyncio
from telebot import asyncio_helper, util
from telebot.async_telebot import AsyncTeleBot
from telebot.asyncio_handler_backends import BaseMiddleware
from mess import msg
def load_file(filename):
try:
with open(filename, 'r', encoding="utf-8") as file:
data = json.load(file)
except OSError:
print('Could not load file:', filename)
sys.exit(1)
return data
def save_file(filename, data):
try:
with open(filename, 'w', encoding="utf-8") as file:
file.write(json.dumps(data, indent=2))
except OSError:
print('Could not save file:', filename)
sys.exit(1)
def get_fullname(first_name, last_name):
return ' '.join(filter(None, (first_name, last_name)))
def get_user_dict(user_id, username, first_name, last_name, enabled):
username = username if username else ''
fullname = get_fullname(first_name, last_name)
return {'id': user_id, 'username': username,
'fullname': fullname, 'enabled': enabled}
def update_config():
if 'channels' not in config:
config['channels'] = []
if 'owner' not in config:
if config['owner_id']:
config['owner'] = get_user_dict(config['owner_id'], '', '', '',
config['owner_enabled'])
else:
config['owner'] = {}
config.pop('owner_id')
config.pop('owner_enabled')
NAME = 'Inspector Bot'
VERSION = '1.16'
CONFIG_FILE = 'config.json'
AGES_FILE = 'ages.json'
config = load_file(CONFIG_FILE)
update_config()
ages = load_file(AGES_FILE)
PASS_MAX_AGE = 120
PASS_LEN = 16
private = {'timer': None, 'password': ''}
bot = AsyncTeleBot(config['token'], parse_mode='HTML')
LOG_FILE = os.path.splitext(os.path.basename(__file__))[0] + '.log'
LOG_LEVEL = logging.INFO
def lang():
return config['language']
def check_owner(user_id):
return config['owner'] and user_id == config['owner']['id']
async def check_owner_set(chat_id, show_tip):
if not config['owner']:
text = msg[lang()]['mess_owner_not_set']
if show_tip:
text += msg[lang()]['mess_bot_help_tip']
await bot.send_message(chat_id, text)
def get_ids(section):
return [x['id'] for x in config[section]]
def get_enabled_uids():
uids = [config['owner']['id'], ] \
if config['owner'] and config['owner']['enabled'] else []
for user in config['users']:
if user['enabled'] and user['id'] not in uids:
uids.append(user['id'])
return uids
def get_owner(user):
owner = get_user_dict(user.id, user.username, user.first_name,
user.last_name, True)
if config['owner']:
if config['owner']['id'] != user.id:
uids = get_ids('users')
if config['owner']['id'] not in uids:
config['users'].append(config['owner'])
if user.id in uids:
index = uids.index(user.id)
owner = config['users'][index]
config['users'].pop(index)
else:
owner = config['owner']
return owner
def get_timestamp(user_id):
ids = list(ages.keys())
nids = list(map(int, ids))
min_id = nids[0]
max_id = nids[len(nids) - 1]
if user_id < min_id:
return [-1, ages[ids[0]]]
if user_id > max_id:
return [1, ages[ids[-1]]]
lid = nids[0]
for i in range(len(ids)):
if user_id <= nids[i]:
uid = nids[i]
lage = ages[str(lid)]
uage = ages[str(uid)]
did = 1 if uid == lid else uid - lid
idratio = (user_id - lid) / did
mid_date = math.floor(idratio * (uage - lage) + lage)
return [0, mid_date]
lid = nids[i]
return [0, 0]
def get_age(user_id):
tst = get_timestamp(user_id)
if tst[0] < 0:
res = msg[lang()]['mess_keys']['before']
elif tst[0] > 0:
res = msg[lang()]['mess_keys']['after']
else:
res = '~ '
date = datetime.datetime.fromtimestamp(tst[1] / 1e3)
return res + str(date.month).zfill(2) + '/' + str(date.year)
def password_generate():
alph = string.ascii_letters + string.digits
private['password'] = ''.join(secrets.choice(alph) for i in range(PASS_LEN))
def password_reset():
private['password'] = ''
def is_command_allow_user(message, owner_set):
res = not owner_set or config['owner']
if res and config['owner']:
uids = [config['owner']['id'], ] + get_ids('users')
res = message.chat.id in uids
return res
async def update_chat(chat_dict, chat_type):
try:
chat = await bot.get_chat(chat_dict['id'])
if chat_dict['title'] != chat.title:
chat_dict['title'] = chat.title
save_file(CONFIG_FILE, config)
logging.debug('%s ID:%d data updated: %s', chat_type.capitalize(),
chat_dict['id'], str(chat_dict))
except asyncio_helper.ApiTelegramException:
pass
async def update_user(user):
for group in config['groups']:
try:
member = await bot.get_chat_member(group['id'], user['id'])
fullname = get_fullname(member.user.first_name, member.user.last_name)
if user['username'] != member.user.username \
or user['fullname'] != fullname:
user['username'] = member.user.username
user['fullname'] = fullname
save_file(CONFIG_FILE, config)
logging.debug('User ID:%d data updated: %s', user['id'], str(user))
break
except asyncio_helper.ApiTelegramException:
pass
async def get_admin_status(chat_id):
res = False
try:
if bot.user is not None:
member = await bot.get_chat_member(chat_id, bot.user.id)
res = member.status == 'administrator'
except asyncio_helper.ApiTelegramException:
pass
return res
def format_chat_id(chat_id):
chat_id = chat_id[1:] if chat_id[0] == '@' else chat_id
chat_id = '-' + chat_id if not chat_id or chat_id[0] != '-' else chat_id
return chat_id
def get_chat_title(chat_type, title):
chat_key = msg[lang()]['mess_keys'][chat_type]
return f'<u>{chat_key}: {title}</u>\n'
def get_user_text(user):
user, text = user.to_dict(), ''
keys = ['id', 'first_name', 'last_name', 'username', 'language_code',
'is_premium', 'is_bot']
for key in keys:
if key in user and user[key]:
if key == 'username':
val = '@' + str(user[key])
elif key in ['is_premium', 'is_bot']:
val = msg[lang()]['mess_keys']['yes']
else:
val = str(user[key])
text_key = msg[lang()]['mess_keys'][key]
text += f'<b>{text_key}:</b> {val}\n'
if user['id'] < int(list(ages.keys())[-1]):
rd_key = msg[lang()]['mess_keys']['registration_date']
text += f'<b>{rd_key}:</b> {get_age(user["id"])}\n'
return text
def get_member_text(member):
status_key = msg[lang()]['mess_keys']['status']
text = f'<b>{status_key}:</b> {str(member.status)}\n'
return text
async def get_user_photo(user):
photo = None
try:
upp = await bot.get_user_profile_photos(user.id)
if upp.total_count > 0:
file_id = upp.photos[0][0].file_id
file = await bot.get_file(file_id)
photo = await bot.download_file(file.file_path)
except asyncio_helper.ApiException:
pass
except UnboundLocalError:
pass
log_text = f'Member ID:{user.id} profile photo '
if photo is None:
log_text += 'is not available'
else:
log_text += 'downloaded successfully'
logging.debug(log_text)
return photo
async def send_message(user, chat_type, title):
text = get_chat_title(chat_type, title)
text += get_user_text(user)
photo = await get_user_photo(user)
uids = get_enabled_uids()
for user_id in uids:
if photo is not None:
await bot.send_photo(user_id, photo, text)
else:
await bot.send_message(user_id, text)
logging.debug('Message about new member ID:%d sent to user ID:%d',
user.id, user_id)
@bot.chat_member_handler()
async def message_chat_member(cmu):
chat_type = 'channel' if cmu.chat.type == 'channel' else 'group'
ids = get_ids(chat_type + 's')
if cmu.chat.id not in ids:
return
new = cmu.new_chat_member
logging.debug('%s "%s" member update: {\'new_chat_member\': %s, '
'\'difference\': %s}', chat_type.capitalize(),
cmu.chat.title, str(new), str(cmu.difference))
if 'is_member' in cmu.difference \
and cmu.difference['is_member'] == [False, True] \
or 'status' in cmu.difference \
and cmu.difference['status'][0] in ['left', 'kicked'] \
and cmu.difference['status'][1] not in ['restricted', 'kicked', 'left']:
logging.info('New member in %s "%s": %s', chat_type,
cmu.chat.title, str(new))
await send_message(new.user, chat_type, cmu.chat.title)
@bot.message_handler(commands=['owner'], chat_types=['private'])
async def command_owner(message):
args = message.text.split()[1:]
if len(args) != 1:
return
chat = message.chat
reg = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[A-Za-z\d]{16,20}$'
pat = re.compile(reg)
if re.search(pat, args[0]):
salt = base64.b64decode(config['owner_pass_salt'].encode('ascii'))
if not salt:
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', args[0].encode('utf-8'), salt, 100000)
if not config['owner'] or check_owner(chat.id) \
or key == base64.b64decode(config['owner_pass_hash'].encode('ascii')):
config['owner_pass_hash'] = base64.b64encode(key).decode('ascii')
config['owner_pass_salt'] = base64.b64encode(salt).decode('ascii')
config['owner'] = get_owner(chat)
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id, msg[lang()]['mess_owner_succ']
+ msg[lang()]['mess_bot_help_tip'])
logging.info('Owner ID:%d has been set successfully',
config['owner']['id'])
elif not config['owner'] or check_owner(chat.id):
await bot.send_message(message.chat.id, msg[lang()]['mess_owner_password'])
@bot.message_handler(commands=['group_add'],
chat_types=['group', 'supergroup'])
async def command_group_add(message):
await check_owner_set(message.chat.id, False)
if not check_owner(message.from_user.id):
return
chat_id = message.chat.id
is_admin = await get_admin_status(chat_id)
gids = get_ids('groups')
group_dict = {'id': chat_id, 'title': message.chat.title}
if chat_id in gids:
config['groups'][gids.index(chat_id)] = group_dict
else:
config['groups'].append(group_dict)
save_file(CONFIG_FILE, config)
text = msg[lang()]['mess_group_added']
if not is_admin:
text += msg[lang()]['mess_bot_admin']
await bot.send_message(chat_id, text)
logging.info('Group "%s" added: %s', message.chat.title, str(group_dict))
@bot.message_handler(commands=['group_del'], chat_types=['private'])
async def command_group_del(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
args = message.text.split()[1:]
if len(args) != 1:
return
chat_id = format_chat_id(args[0])
found = False
for index, group in enumerate(config['groups']):
if str(group['id']) == chat_id:
group_dict = group
config['groups'].pop(index)
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id,
msg[lang()]['mess_group_deleted'])
logging.info('Group "%s" deleted: %s', group_dict['title'],
str(group_dict))
found = True
break
if not found:
await bot.send_message(message.chat.id,
msg[lang()]['mess_group_not_found'])
@bot.message_handler(commands=['group_list'], chat_types=['private'])
async def command_group_list(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
if config['groups']:
text = f'<b>{msg[lang()]["mess_group_list"]}:</b>\n'
for group in config['groups']:
await update_chat(group, 'group')
text += f'{group["id"]} ({group["title"]})\n'
await bot.send_message(message.chat.id, text)
else:
await bot.send_message(message.chat.id, msg[lang()]['mess_group_empty'])
@bot.message_handler(commands=['channel_add'], chat_types=['private'])
async def command_channel_add(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
args = message.text.split()[1:]
if len(args) != 1:
return
chat_id = format_chat_id(args[0])
is_admin = await get_admin_status(chat_id)
try:
chat = await bot.get_chat(chat_id)
cids = get_ids('channels')
channel_dict = {'id': chat.id, 'title': chat.title}
if chat.id in cids:
config['channels'][cids.index(chat.id)] = channel_dict
else:
config['channels'].append(channel_dict)
save_file(CONFIG_FILE, config)
text = msg[lang()]['mess_channel_added']
if not is_admin:
text += msg[lang()]['mess_bot_admin']
await bot.send_message(message.chat.id, text)
logging.info('Channel "%s" added: %s', chat.title, str(channel_dict))
except asyncio_helper.ApiTelegramException:
await bot.send_message(message.chat.id,
msg[lang()]['mess_channel_not_found'])
@bot.message_handler(commands=['channel_del'], chat_types=['private'])
async def command_channel_del(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
args = message.text.split()[1:]
if len(args) != 1:
return
chat_id = format_chat_id(args[0])
found = False
for index, channel in enumerate(config['channels']):
if str(channel['id']) == chat_id:
channel_dict = channel
config['channels'].pop(index)
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id,
msg[lang()]['mess_channel_deleted'])
logging.info('Channel "%s" deleted: %s', channel_dict['title'],
str(channel_dict))
found = True
break
if not found:
await bot.send_message(message.chat.id,
msg[lang()]['mess_channel_not_found'])
@bot.message_handler(commands=['channel_list'], chat_types=['private'])
async def command_channel_list(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
if config['channels']:
text = f'<b>{msg[lang()]["mess_channel_list"]}:</b>\n'
for channel in config['channels']:
await update_chat(channel, 'channel')
text += f'{channel["id"]} ({channel["title"]})\n'
await bot.send_message(message.chat.id, text)
else:
await bot.send_message(message.chat.id, msg[lang()]['mess_channel_empty'])
@bot.message_handler(commands=['lang'], chat_types=['private'])
async def command_lang(message):
if config['owner'] and not check_owner(message.chat.id):
return
lkeys = list(msg.keys())
index = (lkeys.index(lang()) + 1) % len(lkeys)
config['language'] = lkeys[index]
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id, msg[lang()]['mess_change_lang'])
logging.info('Language changed to "%s"', lang())
@bot.message_handler(commands=['user_add'], chat_types=['private'])
async def command_user_add(message):
await check_owner_set(message.chat.id, True)
if not config['owner']:
return
args = message.text.split()[1:]
if check_owner(message.chat.id):
if len(args) > 0:
return
password_generate()
await bot.send_message(message.chat.id, msg[lang()]['mess_user_pass']
% (PASS_MAX_AGE, private['password']))
if private['timer'] is not None:
private['timer'].cancel()
private['timer'] = threading.Timer(PASS_MAX_AGE, password_reset)
private['timer'].start()
else:
user = message.chat
uids = get_ids('users')
password = private['password']
if user.id in uids or not password or len(args) != 1 \
or password != args[0]:
return
password_reset()
user_dict = get_user_dict(user.id, user.username, user.first_name,
user.last_name, True)
config['users'].append(user_dict)
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id, msg[lang()]['mess_user_added']
+ msg[lang()]['mess_bot_help_tip'])
logging.info('User ID:%d added: %s', user.id, str(user_dict))
@bot.message_handler(commands=['user_del'], chat_types=['private'])
async def command_user_del(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
args = message.text.split()[1:]
if len(args) != 1:
return
uname = args[0][1:] if args[0][0] == '@' else args[0]
if not uname:
return
found = False
for index, user in enumerate(config['users']):
if uname in {str(user['id']), user['username']}:
user_dict = user
config['users'].pop(index)
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id, msg[lang()]['mess_user_deleted'])
logging.info('User ID:%d deleted: %s', user_dict['id'], str(user_dict))
found = True
break
if not found:
await bot.send_message(message.chat.id, msg[lang()]['mess_user_not_found'])
@bot.message_handler(commands=['user_list'], chat_types=['private'])
async def command_user_list(message):
await check_owner_set(message.chat.id, True)
if not check_owner(message.chat.id):
return
if config['users']:
text = f'<b>{msg[lang()]["mess_user_list"]}:</b>\n'
for user in config['users']:
await update_user(user)
text += str(user['id'])
if user['username']:
text += f' @{user["username"]}'
text += f' ({user["fullname"]})\n'
await bot.send_message(message.chat.id, text)
else:
await bot.send_message(message.chat.id, msg[lang()]['mess_user_empty'])
async def process_chat_member(chat_id, user_id):
async def check_chat(chat, chat_type, data):
await update_chat(chat, chat_type)
member = None
try:
member = await bot.get_chat_member(chat['id'], user_id)
except asyncio_helper.ApiTelegramException:
pass
if member is not None:
if data[1]:
data[1] += ', '
data[1] += f'{chat_type.capitalize()} "{chat["title"]}" ' \
f'(ID:{chat["id"]}): {str(member)}'
if not data[0]:
data[0] = get_user_text(member.user)
data[2] = await get_user_photo(member.user)
data[0] += '\n' + get_chat_title(chat_type, chat["title"])
data[0] += get_member_text(member)
data = ['', '', None]
for group in config['groups']:
await check_chat(group, 'group', data)
for channel in config['channels']:
await check_chat(channel, 'channel', data)
if data[0]:
if data[2] is not None:
await bot.send_photo(chat_id, data[2], data[0])
else:
await bot.send_message(chat_id, data[0])
else:
await bot.send_message(chat_id, msg[lang()]['mess_user_not_found'])
logging.info('Getting chat member ID:%s info: [%s]', user_id, data[1])
async def process_chat(chat_id, chat):
def format_text(key, val):
if val:
val = '@' + val if key == 'username' else val
text_key = msg[lang()]['mess_keys'][key]
return f'<b>{text_key}:</b> {val}\n'
return ''
text = format_text('id', chat.id)
text += format_text('title', chat.title)
text += format_text('username', chat.username)
text += format_text('type', chat.type)
await bot.send_message(chat_id, text)
logging.info('Getting chat ID:%s info: [%s]', chat.id, str(chat))
@bot.message_handler(commands=['member'], chat_types=['private'])
async def command_chat_member(message):
await check_owner_set(message.chat.id, True)
if not is_command_allow_user(message, True):
return
args = message.text.split()[1:]
if len(args) != 1:
return
user_id = args[0][1:] if args[0][0] == '@' else args[0]
if not user_id or not user_id.isdigit():
return
await process_chat_member(message.chat.id, user_id)
@bot.message_handler(func=lambda message: message.forward_from is not None
or message.forward_sender_name is not None
or message.forward_from_chat is not None,
chat_types=['private'],
content_types=util.content_type_media)
async def message_forward_from(message):
await check_owner_set(message.chat.id, True)
if not is_command_allow_user(message, True):
return
if message.forward_from is None:
if message.forward_from_chat is not None:
await process_chat(message.chat.id, message.forward_from_chat)
else:
await bot.send_message(message.chat.id,
msg[lang()]['mess_member_hidden'])
else:
await process_chat_member(message.chat.id, message.forward_from.id)
@bot.message_handler(commands=['help'], chat_types=['private'])
async def command_help(message):
if not is_command_allow_user(message, False):
return
text = f'<b>{msg[lang()]["mess_bot_commands"]}:</b>\n'
if not config['owner'] or check_owner(message.chat.id):
text += msg[lang()]['mess_bot_help_owner']
text += msg[lang()]['mess_bot_help_user']
await bot.send_message(message.chat.id, text)
async def command_start_stop(message, value, key):
def set_owner_value():
res = message.chat.id == config['owner']['id']
if res:
config['owner']['enabled'] = value
return res
def set_user_value():
res = message.chat.id in uids
if res:
index = uids.index(message.chat.id)
config['users'][index]['enabled'] = value
return res
await check_owner_set(message.chat.id, True)
if config['owner']:
uids = get_ids('users')
if set_owner_value():
usr_msg = f'Owner ID:{message.chat.id}'
set_user_value()
elif set_user_value():
usr_msg = f'User ID:{message.chat.id}'
else:
return
save_file(CONFIG_FILE, config)
await bot.send_message(message.chat.id,
msg[lang()][key] + msg[lang()]['mess_bot_help_tip'])
com_msg = 'started' if value else 'stopped'
logging.info('%s %s the bot', usr_msg, com_msg)
@bot.message_handler(commands=['start'], chat_types=['private'])
async def command_start(message):
await command_start_stop(message, True, 'mess_bot_started')
@bot.message_handler(commands=['stop'], chat_types=['private'])
async def command_stop(message):
await command_start_stop(message, False, 'mess_bot_stopped')
class DebugLogMiddleware(BaseMiddleware):
def __init__(self):
self.update_types = ['message']
async def pre_process(self, message, data):
if message.chat.type != 'private':
return
if message.text is not None:
cmd = message.text.split()
if 'owner' in cmd[0]:
text = cmd[0]
if len(cmd) > 1:
text += ' <PASSWORD>'
else:
text = message.text.replace('\n', ' ')
else:
text = message.content_type
user_id = message.from_user.id if message.from_user is not None else None
logging.debug('User ID:%s chat ID:%d sent a message "%s". '
'User details: %s', str(user_id), message.chat.id, text,
str(message.from_user))
async def post_process(self, message, data, exception):
pass
def setup_logging():
logging.basicConfig(handlers=[
logging.FileHandler(filename=LOG_FILE, encoding='utf-8')],
format='%(asctime)s - %(levelname)s - %(message)s', level=LOG_LEVEL)
if LOG_LEVEL == logging.DEBUG:
bot.setup_middleware(DebugLogMiddleware())
logging.info('%s version %s has started', NAME, VERSION)
logging.debug('Python version: %s', sys.version.replace('\n', ''))
def main():
setup_logging()
if not config['token']:
logging.error('The bot\'s authorization token is not set in "%s"',
CONFIG_FILE)
sys.exit(1)
asyncio.run(bot.infinity_polling(allowed_updates=["message", "chat_member"]))
if __name__ == '__main__':
main()