def pass_count_and_len_callback(query: CallbackQuery) -> None: chat_id = query.message.chat.id lang = query.from_user.language_code logger.info(f"{query.from_user.id} setting {query.data}") if query.data == "pass_count": bot.send_message( chat_id, get_translated_message('pass_count_question', lang_code=lang)) bot.register_next_step_handler_by_chat_id( chat_id, pass_count_and_len_step, dict(max_val=MAX_PASS_COUNT, db_field="pass_count", success_message=get_translated_message('pass_count_setted', lang_code=lang))) if query.data == "pass_len": bot.send_message( chat_id, get_translated_message('pass_len_generation_question', lang_code=lang)) bot.register_next_step_handler_by_chat_id( chat_id, pass_count_and_len_step, dict(max_val=MAX_PASS_LEN, db_field="pass_len", success_message=get_translated_message('pass_len_setted', lang_code=lang)))
def pass_count_and_len_step(message: Message, options: dict) -> None: """ Step handler for write new values of pass_count and pass_len to database This function check value for is_digit, max_val is bigger then val, val less then 0 and update val to database After all, bot reply a success_message and send settings message :param message: Message obj :param options: dict with max_val, db_field and success_message :return: """ chat_id = message.chat.id val = message.text if not str.isdigit(val): return bot.reply_to(message, get_translated_message('need_int', message)) # Converting to int after checking val = int(val) if val > options["max_val"]: return bot.reply_to( message, get_translated_message('int_too_big', message).format(options["max_val"])) if val <= 0: return bot.reply_to(message, get_translated_message('int_too_small', message)) storj.update(options["db_field"], val, chat_id) bot.reply_to(message, options["success_message"].format(val)) # Sending settings keyboard... send_settings_message(message)
def only_numbers(length: int, lang_code: str) -> InlineQueryResultArticle: return InlineQueryResultArticle( uuid4().hex, get_translated_message('inline_only_int', lang_code=lang_code), InputTextMessageContent(generate_only_numbers(length)), thumb_url=only_numbers_pic_url )
def str_nums_and_spec(length: int, lang_code: str) -> InlineQueryResultArticle: return InlineQueryResultArticle( uuid4().hex, get_translated_message('inline_str_nums_and_specs', lang_code=lang_code), InputTextMessageContent(generate_str_nums_and_spec(length)), thumb_url=str_nums_and_spec_pic_url )
def send_settings_message(message: Message) -> None: chat_id = message.from_user.id bot.send_message(chat_id, get_translated_message('settings_message', message), reply_markup=settings_keyboard( storj.get_or_create(chat_id), message.from_user.language_code))
def edit_settings_message(chat_id: int, message_id: int, lang_code: str) -> None: bot.edit_message_text(get_translated_message('settings_message', lang_code=lang_code), chat_id, message_id, reply_markup=settings_keyboard( storj.get_or_create(chat_id), lang_code))
def generate_from_preset(message: Message, generator: Callable, pass_length: int = None) -> None: """Generate password from preset and send to user""" if pass_length: length = pass_length else: length = MAX_PASS_LEN msg = message.text.split(" ") if len(msg) > 1: if str.isdigit(msg[1]): # Set length from message length = int(msg[1]) else: return bot.reply_to(message, get_translated_message('need_int', message)) if length > MAX_PASS_LEN: return bot.reply_to( message, get_translated_message('int_too_big', message).format(MAX_PASS_LEN)) bot.reply_to(message, f"<code>{escape(generator(length))}</code>", parse_mode="html")
def settings_keyboard(user_data: dict, lang_code: str) -> InlineKeyboardMarkup: buttons = InlineKeyboardMarkup() buttons.add( InlineKeyboardButton( bool_to_emoji(user_data['allow_numbers']), callback_data="nums" ), InlineKeyboardButton( get_translated_message('keyboard_nums', lang_code=lang_code), callback_data="nums" ) ) buttons.add( InlineKeyboardButton( bool_to_emoji(user_data['allow_lowercase']), callback_data="lowercase" ), InlineKeyboardButton( get_translated_message('keyboard_lowercase', lang_code=lang_code), callback_data="lowercase" ) ) buttons.add( InlineKeyboardButton( bool_to_emoji(user_data['allow_uppercase']), callback_data="uppercase" ), InlineKeyboardButton( get_translated_message('keyboard_uppercase', lang_code=lang_code), callback_data="uppercase" ) ) buttons.add( InlineKeyboardButton( bool_to_emoji(user_data['allow_spec']), callback_data="spec_chars" ), InlineKeyboardButton( get_translated_message('keyboard_spec_chars', lang_code=lang_code), callback_data="spec_chars" ) ) buttons.add( InlineKeyboardButton( get_translated_message('keyboard_pass_count', lang_code=lang_code), callback_data="pass_count" ), InlineKeyboardButton( user_data['pass_count'], callback_data="pass_count" ) ) buttons.add( InlineKeyboardButton( get_translated_message('keyboard_pass_len', lang_code=lang_code), callback_data="pass_len" ), InlineKeyboardButton( user_data['pass_len'], callback_data="pass_len" ) ) return buttons
def generate(message: Message) -> None: chat_id = message.chat.id logger.info(f"Generate password for {chat_id}") user_data = storj.get_or_create(chat_id) if check_for_generate_nothing(user_data): return bot.send_message( chat_id, get_translated_message('nothing_to_generate', message)) passwords = [ escape( generate_password(length=int(user_data["pass_len"]), add_lowercase=user_data["allow_lowercase"], add_uppercase=user_data["allow_uppercase"], add_nums=user_data["allow_numbers"], add_spec=user_data["allow_spec"])) for _ in range(0, user_data["pass_count"]) ] passwords = list( map(lambda password: f"<code>{password}</code>", passwords)) bot.send_message(chat_id, '\n'.join(passwords), parse_mode="html")
def send_welcome(message: Message) -> None: chat_id = message.from_user.id logger.info(f"Send welcome to {chat_id}") bot.reply_to(message, get_translated_message('start', message)) storj.get_or_create(chat_id)