Смотрите, мне нужно войти в телеграмм аккаунт через мой софт, но у меня по неизвестным причинам, как только я ввожу код, считает его истекшим и как бы я не пытался исправить, проблема на месте
def add_account(api_id, api_hash, phone, proxy, code=None, password=None, phone_code_hash=None):
asyncio.set_event_loop(global_event_loop)
result, phone_code_hash = global_event_loop.run_until_complete(add_account_async(api_id, api_hash, phone, proxy, code, password, phone_code_hash))
return result, phone_code_hash
async def add_account_async(api_id, api_hash, phone, proxy, code=None, password=None, phone_code_hash=None):
if not api_id or not api_hash or not phone:
return None, None
if proxy:
host, port = proxy.split(':')
proxy_server = connection.ConnectionTcpMTProxyRandomizedIntermediate(
proxy=(host, int(port), "secret")
)
else:
proxy_server = None
session_string = accounts[phone]['session_string'] if phone in accounts else ""
string_session = StringSession(session_string)
client = TelegramClient(string_session, api_id, api_hash, proxy=proxy_server)
try:
await client.connect()
if not client.is_connected():
print("Не удалось установить соединение")
return None, None
if not await client.is_user_authorized():
if not code:
result = await client.send_code_request(phone)
return "code_required", result.phone_code_hash
else:
try:
await client.sign_in(phone, code, phone_code_hash=phone_code_hash, password=password)
except PhoneNumberUnoccupiedError:
result = await client.send_code_request(phone)
return "code_required", result.phone_code_hash
except SessionPasswordNeededError:
if not password:
return "password_required", None
else:
try:
await client.check_password(password)
except Exception as e:
print(f"Ошибка проверки пароля сессии: {e}")
return None, None
await client.sign_in(password=password)
except PhoneCodeExpiredError:
print("The confirmation code has expired. Requesting a new one.")
result = await client.send_code_request(phone)
return "code_required", result.phone_code_hash
session_string = client.session.save()
print("Авторизация успешна")
save_account(phone, api_id, api_hash, session_string, proxy)
finally:
await client.disconnect()