Skip to content

Instantly share code, notes, and snippets.

@Kylmakalle
Last active September 21, 2017 14:02
Show Gist options
  • Save Kylmakalle/fabba9c23af36b2cff2ec63551b01422 to your computer and use it in GitHub Desktop.
Save Kylmakalle/fabba9c23af36b2cff2ec63551b01422 to your computer and use it in GitHub Desktop.
Two step login-in in Telegram using https://github.com/LonamiWebs/Telethon
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
phone = '+34600000000'
# First Connect
client = TelegramClient('user_{}'.format(phone[1:]), api_id, api_hash)
client.connect()
if not client.is_user_authorized():
sended = client.send_code_request(phone)
stored_code_hash = sended.phone_code_hash
client.disconnect()
# ...
# Requesting code input at new page/instance/step
################################
# Second Connect
client2 = TelegramClient('user_{}'.format(phone[1:]), api_id, api_hash)
client2._phone_code_hash = stored_code_hash # Applying previous code_hash
client2._phone = phone # And Phone
client2.connect()
client2.sign_in(phone, input('CODE: '))
print(client2.get_me())
else:
# Do anything, u'r already logged in
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment