Telethon TelegramClient.sign_in deadlock
Я пытаюсь разделить логику доступа к аккаунту в Telethon. Таким образом, сначала пользователь вводит свой номер телефона, пароль, а потом, когда ему приходит SMS с кодом, он вводит и его, и я могу работать с TelegramClient.
Вот мой код внутри проекта Django (код невероятно ужасен, так как я пишу его в спешке и для себя):
urls.py
from django.urls import path
from .views import main_page, verify
urlpatterns = [
path("authenticate/", main_page),
path("verification/", verify, name="verify")
]
view.py
from django.shortcuts import render, redirect
from django.urls import reverse
from telethon import TelegramClient
global phone, password, hash_, client
phone = None
password = None
hash_ = None
client = TelegramClient(
"Telegram Client",
0,
""
)
async def main_page(request):
global phone, password, client, hash_
if request.POST:
phone = request.POST['phone']
password = request.POST['password']
print("Connecting to the client")
await client.connect()
print("Sending an SMS with the code")
hash_ = await client.send_code_request(phone)
return redirect(reverse("verify"))
return render(request, "credentials.html")
async def verify(request):
global phone, password, client, hash_
if request.POST:
print("Connecting to the client")
await client.connect()
print(
"Data before signing in: "
"phone {}, password {}".format(phone, password)
)
# Here is a deadlock.
client = await client.sign_in(
phone,
request.POST["code"],
password=password,
phone_code_hash=hash_.phone_code_hash
)
print(
"Check after signing in", await client.get_me()
)
return render(request, "verification_code.html")
credentials.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Credentials</title>
</head>
<body>
<form method="post">
{% csrf_token %}
<input id="phone" type="text" name="phone" placeholder="phone">
<input id="password" type="password" name="password" placeholder="password">
<input type="submit" value="Log in">
</form>
</body>
</html>
verification_code.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Verification</title>
</head>
<body>
<form method="post">
{% csrf_token %}
<input id="code" type="text" name="code" placeholder="Verification code">
<input type="submit" value="Log in">
</form>
</body>
</html>
Когда я запрашиваю код, все работает правильно, но когда я пытаюсь войти в систему, используя его, возникает тупик. Не могли бы вы мне помочь? В чем проблема?