Advertisement
disk6969

Untitled

Dec 30th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. def request_telegram_code():
  2. data = request.get_json()
  3. phone_number = data['phone']
  4. session['phone_number'] = phone_number
  5.  
  6. api_id = current_app.config['TELEGRAM_API_ID']
  7. api_hash = current_app.config['TELEGRAM_API_HASH']
  8.  
  9. loop = asyncio.new_event_loop()
  10. asyncio.set_event_loop(loop)
  11. loop.run_until_complete(send_verification_code(phone_number, api_id, api_hash))
  12.  
  13. return jsonify({'message': 'Verification code requested for ' + phone_number})
  14.  
  15. async def send_verification_code(phone_number, api_id, api_hash):
  16. client = TelegramClient(phone_number, api_id, api_hash)
  17. await client.connect()
  18. try:
  19. result = await client.send_code_request(phone_number)
  20. session['phone_code_hash'] = result.phone_code_hash
  21. finally:
  22. await client.disconnect()
  23.  
  24. #input code
  25. def verify_telegram_code():
  26. data = request.get_json()
  27. phone_number = session.get('phone_number')
  28. code = data['code']
  29.  
  30. api_id = current_app.config['TELEGRAM_API_ID']
  31. api_hash = current_app.config['TELEGRAM_API_HASH']
  32.  
  33. loop = asyncio.new_event_loop()
  34. asyncio.set_event_loop(loop)
  35.  
  36. result = loop.run_until_complete(retry_login(phone_number, api_id, api_hash, code))
  37.  
  38. return jsonify({'message': 'Verification complete', 'result': result})
  39.  
  40. async def retry_login(phone_number, api_id, api_hash, code):
  41. try:
  42. result = await complete_telegram_login(phone_number, api_id, api_hash, code)
  43. return result
  44. except Exception as e:
  45. print(e)
  46. if 'The confirmation code has expired' in str(e):
  47. await send_verification_code(phone_number, api_id, api_hash)
  48. return {'status': 'expired_code_retry'}
  49. return {'status': 'error', 'message': str(e)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement