Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def request_telegram_code():
- data = request.get_json()
- phone_number = data['phone']
- session['phone_number'] = phone_number
- api_id = current_app.config['TELEGRAM_API_ID']
- api_hash = current_app.config['TELEGRAM_API_HASH']
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- loop.run_until_complete(send_verification_code(phone_number, api_id, api_hash))
- return jsonify({'message': 'Verification code requested for ' + phone_number})
- async def send_verification_code(phone_number, api_id, api_hash):
- client = TelegramClient(phone_number, api_id, api_hash)
- await client.connect()
- try:
- result = await client.send_code_request(phone_number)
- session['phone_code_hash'] = result.phone_code_hash
- finally:
- await client.disconnect()
- #input code
- def verify_telegram_code():
- data = request.get_json()
- phone_number = session.get('phone_number')
- code = data['code']
- api_id = current_app.config['TELEGRAM_API_ID']
- api_hash = current_app.config['TELEGRAM_API_HASH']
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- result = loop.run_until_complete(retry_login(phone_number, api_id, api_hash, code))
- return jsonify({'message': 'Verification complete', 'result': result})
- async def retry_login(phone_number, api_id, api_hash, code):
- try:
- result = await complete_telegram_login(phone_number, api_id, api_hash, code)
- return result
- except Exception as e:
- print(e)
- if 'The confirmation code has expired' in str(e):
- await send_verification_code(phone_number, api_id, api_hash)
- return {'status': 'expired_code_retry'}
- return {'status': 'error', 'message': str(e)}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement