async def your_advice(message: types.Message, state: FSMContext): chat_id = message.chat.id try: last_message = (await state.get_data()).get("last_message") await bot.edit_message_reply_markup(chat_id, last_message) except Exception as e: logging.error(f"{e}") await state.update_data(advice=message.text) last_message = await bot.send_message(chat_id, _(""" Your advice: {advice} Write your advice in the next message. """).format(advice=message.text), reply_markup=ListOfButtons( text=[ _("Publish"), _("Cancel")], callback=[ "publish", "cancel"] ).inline_keyboard) await state.update_data(last_message=last_message.message_id)
async def g_auth(call: types.CallbackQuery, state: FSMContext): chat_id = call.message.chat.id try: await bot.edit_message_reply_markup(chat_id, message_id=call.message.message_id) except Exception as e: logging.error(f"{e}") if has_g_auth(chat_id): await bot.send_message( chat_id, _("You have already received the Google Authenticator code")) return code, link, qr_code = create_google_auth(chat_id) await bot.send_message( chat_id, _(""" You will receive a recovery code and a link below. <b>IT WILL BE AUTOMATICALLY DELETED AFTER YOU CONFIRM</b> """)) message_1 = (await bot.send_message(chat_id, code)).message_id message_2 = (await bot.send_photo(chat_id, qr_code, f"{link}")).message_id await bot.send_message( chat_id, _("Please enter the code from the Google Authenticator\n" "Pay attention that it updates every 30 sec.")) await state.update_data(message_1=message_1, message_2=message_2) await GoogleAuth.next()
async def decode_start(message: types.Message, state: FSMContext): increase_message_counter() chat_id = message.chat.id text = message.text expression = re.compile(f"{OtherTexts.START}(.*){OtherTexts.END}") try: extract_encoded = expression.findall(text)[0] except IndexError: await bot.send_message(chat_id, _("Error. Wrong file")) return expression = re.compile(f"{OtherTexts.END}(.*){OtherTexts.END_CODE}") code = expression.findall(text)[0] await state.update_data(password=extract_encoded, code=code) if not enabled_g_auth(chat_id): await bot.send_message( chat_id, _(""" Please enter your master password. You can make everything faster with Google Authenticator! Press /g_auth_info """)) else: await bot.send_message(chat_id, _("Enter the code from the app")) await Decode.MASTER_PASSWORD.set()
async def g_auth(call: types.CallbackQuery, state: FSMContext): chat_id = call.message.chat.id try: await bot.edit_message_reply_markup(chat_id, message_id=call.message.message_id) except Exception as e: logging.error(f"{e}") if has_g_auth(chat_id): await bot.send_message( chat_id, _("You have already received the Google Authenticator code")) return await bot.send_message( chat_id, _(""" Please ensure you have the app installed. <a href= "https://itunes.apple.com/gb/app/google-authenticator/id388497605?mt=8">IOS</a> <a href= "https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&hl=en_GB">Android</a> Press continue when you done. After you receive the code - write it down somewhere. """), reply_markup=ListOfButtons(text=[_("Continue")], callback=["continue"]).inline_keyboard) await GoogleAuth.ONE.set()
async def encode_saved(call: types.CallbackQuery, state: FSMContext): chat_id = call.from_user.id password_message = call.message.reply_to_message password = password_message.text await call.message.delete() async with state.proxy() as data: data["encrypt_from_saved"] = True data["to_encrypt"] = password await password_message.delete() if not enabled_g_auth(chat_id): await bot.send_message( chat_id, _(""" Please enter your master password. You can make everything faster with Google Authenticator! Press /g_auth_info """)) await Encode.MASTER_PASSWORD.set() else: master_pass = get_google_auth(chat_id) if len(password) > 400: await bot.send_message( chat_id, _("Error has occurred... Too long phrase. Try to enter a phrase \ under 400 characters.")) return elif not master_pass: await bot.send_message(chat_id, _("Master Password not found.")) await state.finish() return text, code = encode(password.replace("\n", "\\n"), master_pass) hint = "Google Authenticator" await bot.send_message( chat_id, _("""<code>---------------------------- ENCRYPTION STARTS HERE ---------------------------- {passw} ---------------------------- ENCRYPTION ENDS HERE ---------------------------- CODE {code} ---------------------------- </code> Hint: {hint} Save this message wherever you want and forward it to the bot should you need to decode it. """).format(passw=text, code=code, hint=f"{hint}")) await state.finish() increase_message_counter(password=True) await asyncio.sleep(10)
async def reviews_button(message: types.Message): increase_message_counter() chat_id = message.chat.id await bot.send_message(chat_id, _("""Please, it is important for me to receive a response and advice from you. How would you change the bot? Any comments are appreciated. Your comment will be posted <b>anonymously</b> in our channel @pcypher Or you can just rate the bot using this link: https://t.me/pcypher/16 """), reply_markup=ListOfButtons( text=[_("Give an advice to the bot")], callback=["give_advice"] ).inline_keyboard)
async def unknown(message: types.Message, state: FSMContext): increase_message_counter() text = (_("π Encode") + f" '{message.text}'")[:20] + "..." await message.reply(_(""" Looks like the input is invalid... To decode your password - forward the message with encoded password you received from bot. <a href= 'https://telegra.ph/file/a9f99684284a92eb2a6a0.png'>α </a> Perhaps you wanted to encrypt this message? Click """) + _("π Encode") + _("\n\nOr setup the language again /set_language"), reply_markup=ListOfButtons( text=[text], callback=[f"encrypt_saved"]).inline_keyboard)
async def info(message: types.Message): increase_message_counter() chat_id = message.chat.id text = _( "To encrypt your phrase/file you need to enter a master password each time you want to encrypt or decrypt, or" " you can enable <b>Google Authenticator</b> and enter one-time codes from your phone <b>only to decrypt</b>" " your passwords. \n" "(Master password will be kept in database then) \n\n" "Please make your choice (you can change it later with command /reset_google_auth\n" ) await bot.send_message( chat_id, text, reply_markup=ListOfButtons(text=[_("Setup")], callback=["g_auth_setup"]).inline_keyboard)
async def give_advice(call: types.CallbackQuery, state: FSMContext): chat_id = call.message.chat.id await Other.REVIEW.set() await bot.edit_message_reply_markup(chat_id, call.message.message_id) last_message = await bot.send_message(chat_id, _(""" Your advice: {advice} Write your advice in the next message. """).format(advice=" "), reply_markup=ListOfButtons( text=[_("Cancel")], callback=["cancel"] ).inline_keyboard) await state.update_data(last_message=last_message.message_id)
async def decode_1(message: types.Message, state: FSMContext): increase_message_counter() chat_id = message.chat.id en_password = (await state.get_data()).get("password") code = (await state.get_data())["code"] if not enabled_g_auth(chat_id): master = message.text else: if not has_g_auth(chat_id): await message.reply( _("An error has occurred, you lost the Google authenticator settings\n" "Please re-configure it once again /g_auth_info")) await state.finish() return try: if verify(chat_id, message.text): master = get_google_auth(chat_id) else: await bot.send_message( chat_id, _("Code is incorrect, try again or /cancel")) return except binascii.Error: await bot.send_message( chat_id, _("Code is incorrect, try again or /cancel")) return password = (decode(master, en_password, code)).replace("\\n", "\n") if (await state.get_data()).get("doc"): with open("decoded.txt", "w") as file: file.write(password.replace("\\n", "\n")) await bot.send_document(chat_id, open("decoded.txt", "rb")) with open("decoded.txt", "w") as file: file.write(" ") else: await bot.send_message( chat_id, _(""" Your decoded password is inside citation marks '<code>{password}</code>'"""). format(password=password)) await state.finish() await asyncio.sleep(10) await message.delete()
async def starting(message: types.Message, state: FSMContext): increase_message_counter() await state.reset_state() chat_id = message.chat.id check_if_new_user(chat_id) menu = ListOfButtons(text=[ _("π Encode"), _("π Decode"), _("βΉοΈHow to use"), _("π¬π§ Set language"), _("π Two step verification"), _("π Write a review") ], align=[2, 2, 2]).reply_keyboard await bot.send_message(chat_id, _(""" Hello, <b>{}</b> This bot is designed to encrypt your passwords so you can store them publicly, for example in your \ <code>Telegram saved messages.</code> You can choose your language using command /set_language """).format(message.from_user.first_name), reply_markup=menu)
async def change_language(call: types.CallbackQuery, state: FSMContext): chat_id = call.message.chat.id try: await bot.edit_message_reply_markup(chat_id, call.message.message_id, reply_markup=None) except MessageNotModified: pass language = call.data.split()[1] set_language(chat_id, language) menu = ListOfButtons(text=[ _("π Encode", locale=language), _("π Decode", locale=language), _("βΉοΈHow to use", locale=language), _("π¬π§ Set language", locale=language), _("π Two step verification", locale=language), _("π Write a review", locale=language) ], align=[2, 2, 2]).reply_keyboard await bot.send_message(chat_id, _("""Language has changed to π¬π§<b>EN</b> <b>{users}</b> users are using this bot. <b>{passwords}</b> passwords encrypted. <b>{messages}</b> messages received. Start using this bot: /info""", locale=language).format(**get_counters()), reply_markup=menu)
async def reset(message: types.Message, state: FSMContext): await state.reset_state() chat_id = message.chat.id sql.update(table="users", google="", enabled=0, condition={"chat_id": chat_id}) await message.reply(_("That is done"))
async def encoded(message: types.Message, state: FSMContext): increase_message_counter(password=True) chat_id = message.chat.id master_pass = (await state.get_data()).get("master_pass") file_id = message.document.file_id try: await bot.download_file_by_id(file_id, "to_encode.txt") except BadRequest: return await message.reply(_("INVALID FILE")) with open("to_encode.txt", "r") as file: try: text = file.read().replace("\n", "\\n") except UnicodeDecodeError: await bot.send_message(chat_id, _("INVALID FILE")) await state.finish() return text, code = encode(text, master_pass) with open("encoded.txt", "a") as file: file.write( _(""" ---------------------------- ENCRYPTION STARTS HERE ---------------------------- {passw} ---------------------------- ENCRYPTION ENDS HERE ---------------------------- CODE {code} ---------------------------- Hint: {hint} """).format(passw=text, code=code, hint=f"{master_pass[:2]}***********")) await bot.send_document(chat_id, open("encoded.txt", "rb")) with open("encoded.txt", "w") as file: file.write(" ") with open("to_encode.txt", "w") as file: file.write(" ") await state.finish() await asyncio.sleep(10) await message.delete()
async def statistics(message: types.Message): chat_id = message.chat.id await bot.send_message( chat_id, _(""" <b>{users}</b> users are using this bot. <b>{passwords}</b> passwords encrypted. <b>{messages}</b> messages received. """).format(**get_counters()))
async def g_auth(call: types.CallbackQuery, state: FSMContext): chat_id = call.message.chat.id enabled = call.data.split()[1] sql.update(table="users", enabled=enabled, condition={"chat_id": chat_id}) try: await bot.edit_message_text(text=_("That is done"), chat_id=chat_id, message_id=call.message.message_id) except Exception as e: logging.error(f"{e}")
async def cancel_or_publish(call: types.CallbackQuery, state: FSMContext): chat_id = call.message.chat.id try: last_message = (await state.get_data()).get("last_message") await bot.edit_message_reply_markup(chat_id, last_message) except Exception as e: logging.error(f"{e}") if call.data == "cancel": await bot.send_message(chat_id, _("Cancelled")) await state.finish() return elif call.data == "publish": advice = (await state.get_data()).get("advice") await bot.send_message(config.review_channel, _(""" #Reviews Post: <b>{}</b> """).format(advice))
async def encoded(message: types.Message, state: FSMContext): increase_message_counter(password=True) chat_id = message.chat.id master_pass = (await state.get_data()).get("master_pass") if len(message.text) > 400: await bot.send_message( chat_id, _("Error has occurred... Too long phrase. Try to enter a phrase under 400 \ characters.")) return elif not master_pass: await message.reply(_("Master Password not found.")) await state.finish() await asyncio.sleep(10) await message.delete() return text, code = encode(message.text.replace("\n", "\\n"), master_pass) if master_pass == get_google_auth(chat_id): hint = "Google Authenticator" else: hint = master_pass[:2] + "***********" await bot.send_message( chat_id, _("""<code>---------------------------- ENCRYPTION STARTS HERE ---------------------------- {passw} ---------------------------- ENCRYPTION ENDS HERE ---------------------------- CODE {code} ---------------------------- </code> Hint: {hint} Save this message wherever you want and forward it to the bot should you need to decode it. """).format(passw=text, code=code, hint=f"{hint}")) await state.finish() await asyncio.sleep(10) await message.delete()
async def g_auth(message: types.Message, state: FSMContext): increase_message_counter() chat_id = message.chat.id try: ver = verify(chat_id, message.text) except binascii.Error: await bot.send_message(chat_id, _("Code is incorrect, try again or /cancel")) return if ver: async with state.proxy() as data: message_1 = data.get("message_1") message_2 = data.get("message_2") await bot.delete_message(chat_id, message_1) await bot.delete_message(chat_id, message_2) await bot.send_message( chat_id, _("Confirmation successful, you can proceed. /encode")) await state.finish() else: await bot.send_message(chat_id, _("Code is incorrect, try again or /cancel"))
async def decode_start(message: types.Message, state: FSMContext): increase_message_counter() chat_id = message.chat.id file_id = message.document.file_id try: await bot.download_file_by_id(file_id, "to_decode.txt") except BadRequest: return await message.reply(_("INVALID FILE")) with open("to_decode.txt", "r") as file: try: text = file.read() except UnicodeDecodeError: await bot.send_message(chat_id, _("INVALID FILE")) return expression = re.compile(f"{OtherTexts.START}(.*){OtherTexts.END}") try: extract_encoded = expression.findall(text)[0] except IndexError: await bot.send_message(chat_id, _("Error. Wrong file")) return expression = re.compile(f"{OtherTexts.END}(.*){OtherTexts.END_CODE}") code = expression.findall(text)[0] await state.update_data(password=extract_encoded, code=code, doc=True) if not enabled_g_auth(chat_id): await bot.send_message( chat_id, _(""" Please enter your master password. You can make everything faster with Google Authenticator! Press /g_auth_info """)) else: await bot.send_message(chat_id, _("Enter the code from the app")) await Decode.MASTER_PASSWORD.set() await asyncio.sleep(10) await message.delete()
async def set_google_auth(message: types.Message): increase_message_counter() chat_id = message.chat.id text = _( "https://telegra.ph/Passcypher-Google-Authenticator-06-02") + "\n\n" if has_g_auth(chat_id): if enabled_g_auth(chat_id): await bot.send_message( chat_id, text + _("Here you can enable and disable your Google Authenticator settings" ), reply_markup=ListOfButtons(text=[_("Turn off")], callback=["turn 0" ]).inline_keyboard) else: await bot.send_message( chat_id, text + _("Here you can enable and disable your Google Authenticator settings" ), reply_markup=ListOfButtons(text=[_("Turn on")], callback=["turn 1" ]).inline_keyboard) else: await bot.send_message( chat_id, _("Google Authenticator is not set for you. Press /g_auth_info"))
async def encode_start(message: types.Message, state: FSMContext): increase_message_counter() chat_id = message.chat.id if not enabled_g_auth(chat_id): await bot.send_message( chat_id, _(""" Please enter your master password. You can make everything faster with Google Authenticator! Press /g_auth_info """)) await Encode.MASTER_PASSWORD.set() else: await state.update_data(master_pass=get_google_auth(chat_id)) await Encode.PASSWORD.set() await bot.send_message( chat_id, _("""Enter phrase you want to encrypt. It should be under 400 characters, for best results there should be only characters from this list: <pre>{allowed_chars} </pre> <b> THE BOT DELETES YOUR MESSAGES WITH PASSWORDS AFTER 10 SECONDS</b> """).format(allowed_chars=allowed_chars))
async def language_set(message: types.Message): increase_message_counter() chat_id = message.chat.id await bot.send_message( chat_id, _(""" Hello, <b>{}</b> Let's choose your language """).format(message.from_user.first_name), reply_markup=ListOfButtons( text=["English", "Π ΡΡΡΠΊΠΈΠΉ", "Π£ΠΊΡΠ°ΡΠ½ΡΡΠΊΠ°"], callback=["language en", "language ru", "language uk"]).inline_keyboard)
async def decode_start(message: types.Message, state: FSMContext): increase_message_counter() chat_id = message.chat.id enc = message.text.replace("\n", " ") try: encoded = re.findall("#encoded_pass: '******'.*#", enc)[0] code = re.findall("#key: '(.*)'", enc)[0] except IndexError: await bot.send_message(chat_id, _("Error")) return await state.update_data(password=encoded, code=code) if not enabled_g_auth(chat_id): await bot.send_message( chat_id, _(""" Please enter your master password. You can make everything faster with Google Authenticator! Press /g_auth_info """)) else: await bot.send_message(chat_id, _("Enter the code from the app")) await Decode.MASTER_PASSWORD.set()
async def info(message: types.Message): increase_message_counter() chat_id = message.chat.id if has_g_auth(chat_id): text = _( "Here you can enable and disable your Google Authenticator settings" ) if enabled_g_auth(chat_id): await bot.send_message(chat_id, text, reply_markup=ListOfButtons( text=[_("Turn off")], callback=["turn 0"]).inline_keyboard) else: await bot.send_message(chat_id, text, reply_markup=ListOfButtons( text=[_("Turn on")], callback=["turn 1"]).inline_keyboard) else: await bot.send_message( chat_id, _("Google Authenticator is not set for you. Press /g_auth_info"))
async def lang_choose(message: types.Message): chat_id = message.chat.id increase_message_counter() try: await bot.send_message( chat_id, _(""" Hello, <b>{}</b> Firstly, let's choose your language """).format(message.from_user.first_name), reply_markup=ListOfButtons( text=["English", "Π ΡΡΡΠΊΠΈΠΉ", "Π£ΠΊΡΠ°ΡΠ½ΡΡΠΊΠ°"], callback=["language en", "language ru", "language uk"]).inline_keyboard) except CantParseEntities as err: print(f"Error. CantParseEntities: {err}")
async def info(message: types.Message): increase_message_counter() await message.reply( _("https://telegra.ph/How-to-Use-Passcypher-Instruction-EN-06-02"))
async def decode_m(message: types.Message): increase_message_counter() await message.reply( _("https://telegra.ph/Passwords-Decryption-Process-06-02"))
async def unknown_message(message: types.Message, state: FSMContext): await message.reply(_("Seems like you have an unfinished business..."))
async def unknown_message(call: types.CallbackQuery, state: FSMContext): await call.answer(_("Seems like you have an unfinished business..."))