Всем привет, написал код для авторизации через телетон, запустил его и он работает. Но когда перенёс его на джанго он выбивает мне ошибку:
There is no current event loop in thread 'Thread-1 (process_request_thread)'. С чем может быть связано?
Код ( без джанго ):
from telethon.sync import TelegramClient
from telethon.errors import SessionPasswordNeededError
def sendOTP(phone):
api_id = 22349031
api_hash = '82746524e2bece51d28e9feebe48c033'
client = TelegramClient('telethon_sessions/anon', api_id, api_hash)
client.connect()
sent = client.send_code_request(phone)
print(sent)
code = input("Enter code: ")
try:
client.sign_in(phone, code)
except SessionPasswordNeededError:
password = input('Enter your password: ')
client.sign_in(password=password)
sendOTP("+380986278015")
Код на джанго:
from django.shortcuts import render
from telethon.sync import TelegramClient
from telethon.errors import SessionPasswordNeededError
def sendOTP(phone):
api_id = 22349031
api_hash = '82746524e2bece51d28e9feebe48c033'
client = TelegramClient('telethon_sessions/anon', api_id, api_hash)
client.connect()
sent = client.send_code_request(phone)
print(sent)
code = input("Enter code: ")
try:
client.sign_in(phone, code)
except SessionPasswordNeededError:
password = input('Enter your password: ')
client.sign_in(password=password)
# * ROUTES
def code(request):
phone_number = request.POST.get('phone') # +1 202 217 7848
sendOTP(phone_number)
return render(request, 'phone_number/code.html', {"phone_number": phone_number})
phone_number не пустой, данные приходят
Иногда бывает такое что кроме этой ошибки есть ещё:
OperationalError at /code/
database is locked
Логи:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\helpers.py", line 431, in get_running_loop
return asyncio.get_running_loop()
RuntimeError: no running event loop
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\laragon\www\telegram_stiller\phone_number\views.py", line 33, in code
sendOTP(phone_number)
File "E:\laragon\www\telegram_stiller\phone_number\views.py", line 10, in sendOTP
client = TelegramClient('telethon_sessions/anon', api_id, api_hash)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\telegrambaseclient.py", line 322, in __init__
if not callable(getattr(self.loop, 'sock_connect', None)):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\telegrambaseclient.py", line 473, in loop
return helpers.get_running_loop()
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\helpers.py", line 433, in get_running_loop
return asyncio.get_event_loop_policy().get_event_loop()
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 656, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1 (process_request_thread)'.
[13/Feb/2023 00:55:58,416] - Broken pipe from ('127.0.0.1', 63833)
Internal Server Error: /code/
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\laragon\www\telegram_stiller\phone_number\views.py", line 33, in code
sendOTP(phone_number)
File "E:\laragon\www\telegram_stiller\phone_number\views.py", line 10, in sendOTP
client = TelegramClient('telethon_sessions/anon', api_id, api_hash)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\telegrambaseclient.py", line 275, in __init__
session = SQLiteSession(session)
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\sessions\sqlite.py", line 71, in __init__
self._create_table(
File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\sessions\sqlite.py", line 162, in _create_table
c.execute('create table {}'.format(definition))
sqlite3.OperationalError: database is locked
[13/Feb/2023 00:56:03] "POST /code/ HTTP/1.1" 500 89926