我正在使用 aiogram 和 aiosmtplib 库等编写电报机器人。我陷入了一个问题。我决定在 Telegram 中为我的 Minecraft 服务器制作密码恢复功能。首先用户输入它的电子邮件地址,如果这个电子邮件地址存在于服务器数据库中 - 机器人发送带有确认码的电子邮件,之后用户必须输入它并输入新密码(我还没有完成输入新密码)。但是,无论我输入哪个代码——正确或正确——它总是向我表明代码是正确的!请帮助我解决我的错误,我已经用 print() 检查了所有状态,但仍然无法确定问题所在。
main.py(部分代码)
dp.message_handler(commands=['forgot_password'])
async def forgot_password_handle(message: types.Message, state:FSMContext):
await message.reply("<b>Пожалуйста, введите email-адресс вашего аккаунта</b>",
parse_mode="HTML")
await state.set_state('input_email')
@dp.message_handler(state="input_email")
async def input_name(message: types.Message, state: FSMContext):
email = message.text
await state.update_data(email=email)
print("CHINAAA " + email)
#await state.reset_state()
if not await is_valid_email(email):
await bot.send_message(chat_id=message.from_user.id,
text="<b>Извините, электронная почта недействительна \n. Перепроверьте правильность ввода!</b>",
parse_mode="HTML")
return
confirmation_code = await generate_confirmation_code()
print("CHINAAA " + confirmation_code)
await send_email(email, confirmation_code)
await state.update_data(confirmation_code=confirmation_code)
await state.set_state('confirmation_code_proof')
@dp.message_handler(state="confirmation_code_proof")
async def confirm_code(message: types.Message, state: FSMContext):
async with state.proxy() as data:
email = data.get('email')
confirmation_code = data.get('confirmation_code')
print("China " + email)
print("China " + confirmation_code)
if not await is_valid_confirmation(confirmation_code, email, state):
await bot.send_message(chat_id=message.from_user.id,
text="<b>Извините, код подтверждения недействительный. Перепроверьте правильность ввода!</b>",
parse_mode="HTML")
else:
await bot.send_message(chat_id=message.from_user.id,
text="<b>Код подтверждения действителен. Пароль сброшен.</b>",
parse_mode="HTML")
await state.reset_state()
emails.py - 这是验证函数和代码生成器函数。
async def generate_confirmation_code() -> str:
#Генерація рандомного коду
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6))
async def is_valid_confirmation(confirmation_code: str, email: str, state: FSMContext) -> bool:
async with state.proxy() as data:
stored_code = data.get('confirmation_code')
stored_email = data.get('email')
print("\n" + stored_code)
print("\n" + stored_email)
#and email == stored_email
if confirmation_code == stored_code and email == stored_email:
return True
else:
return False