async def check_message(message): chat_id = message.chat.id filters = redis.lrange('filters_cache_{}'.format(chat_id), 0, -1) if not filters: update_handlers_cache(chat_id) filters = redis.lrange('filters_cache_{}'.format(chat_id), 0, -1) if redis.llen('filters_cache_{}'.format(chat_id)) == 0: return text = message.text if text.split()[0][1:] == 'delfilter': return for keyword in filters: # keyword = keyword.decode("utf-8") keyword = re.escape(keyword) keyword = keyword.replace(r'\(\+\)', '.*') pattern = r"( |^|[^\w])" + keyword + r"( |$|[^\w])" if re.search(pattern, text, flags=re.IGNORECASE): filter = mongodb.filters_v2.find_one({ 'chat_id': chat_id, "handler": { '$regex': keyword } }) if not filter: update_handlers_cache(chat_id) return await do_filter_action(message, filter)
async def check_message(event): filters = redis.lrange('filters_cache_{}'.format(event.chat_id), 0, -1) if not filters: update_handlers_cache(event.chat_id) filters = redis.lrange('filters_cache_{}'.format(event.chat_id), 0, -1) if redis.llen('filters_cache_{}'.format(event.chat_id)) == 0: return text = event.text.split(" ") for filter in filters: filter = filter.decode("utf-8") for word in text: match = re.fullmatch(filter, word, flags=re.IGNORECASE) if not match: continue H = mongodb.filters.find_one({ 'chat_id': event.chat_id, "handler": { '$regex': str(filter) } }) if H['action'] == 'note': await send_note(event.chat_id, event.chat_id, event.message.id, H['arg'], show_none=True) elif H['action'] == 'delete': await event.delete() elif H['action'] == 'ban': await filter_ban(event, filter, None)
async def locks_processor(message): # Get locks chat_id = message.chat.id if await is_user_admin(chat_id, message.from_user.id): return key = 'locks_cache_{}'.format(chat_id) locks = None if redis.exists(key) > 0: locks = redis.lrange(key, 0, -1) if not locks: if not update_locks_cache(chat_id): return locks = redis.lrange(key, 0, -1) locks = [x.decode('utf-8') for x in locks] if 'all' in locks: await message.delete()
async def do_backup(chat_id, reply=False): await bot.send_message(chat_id, "Dumping the DB, please wait...", reply_to_message_id=reply) date = strftime("%Y-%m-%d_%H:%M:%S", gmtime()) file_name = f"Backups/dump_{date}.7z" if not os.path.exists("Backups/"): os.mkdir("Backups/") await term( f"mongodump --uri \"{CONFIG['basic']['mongo_conn']}\" --out=Backups/tempbackup" ) # Let's also save Redis cache with open('Backups/tempbackup/redis_keys.json', 'w+') as f: keys = redis.keys() new = {} for key in keys: key_type = redis.type(key) if key_type == 'string': new[key] = redis.get(key) elif key_type == 'list': new[key] = list(redis.lrange(key, 0, -1)) f.write(ujson.dumps(new, indent=2)) # Copy config file shutil.copyfile('data/bot_conf.json', 'Backups/tempbackup/bot_conf.json') await bot.send_message(chat_id, "Compressing and uploading to Telegram...", reply_to_message_id=reply) password = CONFIG['advanced']['backups_password'] await term( f"cd Backups/tempbackup/; 7z a -mx9 ../../{file_name} * -p{password} -mhe=on" ) shutil.rmtree('Backups/tempbackup') if not os.path.exists(file_name): await bot.send_message(chat_id, "Error!", reply_to_message_id=reply) return text = "<b>Backup created!</b>" size = convert_size(os.path.getsize(file_name)) text += f"\nBackup name: <code>{file_name}</code>" text += f"\nSize: <code>{size}</code>" await tbot.send_file(chat_id, file_name, reply_to=reply, caption=text, parse_mode="html")
async def check_message(message): chat_id = message.chat.id filters = redis.lrange('filters_cache_{}'.format(chat_id), 0, -1) if not filters: update_handlers_cache(chat_id) filters = redis.lrange('filters_cache_{}'.format(chat_id), 0, -1) if redis.llen('filters_cache_{}'.format(chat_id)) == 0: return text = message.text user_id = message.from_user.id msg_id = message.message_id for keyword in filters: keyword = keyword.decode("utf-8") keyword = re.escape(keyword) keyword = keyword.replace('\(\+\)', '.*') pattern = r"( |^|[^\w])" + keyword + r"( |$|[^\w])" if re.search(pattern, text, flags=re.IGNORECASE): H = mongodb.filters.find_one({ 'chat_id': chat_id, "handler": { '$regex': str(pattern) } }) if not H: update_handlers_cache(chat_id) return action = H['action'] if action == 'note': await send_note(chat_id, chat_id, msg_id, H['arg'], show_none=True) elif action == 'answer': # TODO await message.answer(H['arg']) elif action == 'delete': await message.delete() elif action == 'ban': if await ban_user(message, user_id, chat_id, None, no_msg=True) is True: text = get_string('filters', 'filter_ban_success', chat_id).format(user=await user_link_html(user_id), filter=H['handler']) await message.reply(text) elif action == 'tban': timee, unit = await convert_time(message, H['arg']) if await ban_user( message, user_id, chat_id, timee, no_msg=True) is True: text = get_string('filters', 'filter_tban_success', chat_id).format(user=await user_link_html(user_id), time=H['arg'], filter=H['handler']) await message.reply(text) elif action == 'kick': if await kick_user(message, user_id, chat_id, no_msg=True) is True: text = get_string('filters', 'filter_kick_success', chat_id).format(user=await user_link_html(user_id), filter=H['handler']) await message.reply(text) elif action == 'warn': user_id = message.from_user.id await warn_user_filter(message, H, user_id, chat_id) break