how i can restore sessions old in telethon telegram and connect this again(without send again code))
Asked Answered
H

2

2

i use this script for connect and create sessions in telethon

from telethon import TelegramClient
from telethon.tl.functions.messages import GetHistoryRequest
from telethon.utils import get_input_peer
api_id = 7****
api_hash = 'ef584d*****************'
client = TelegramClient('+15159947451', api_id, api_hash)
client.connect()
if not client.is_user_authorized():
    client.send_code_request('+15159947451')
client.sign_in('+15159947451', cod)

with this cod i can login good in this number telegram and create file:+15159947451.session.

now i close and disconnect, how i can again login this number with this file +15159947451.session.

i use this code but this code have error:

from telethon import TelegramClient
from telethon.tl.functions.messages import GetHistoryRequest
from telethon.utils import get_input_peer
api_id = 7****
api_hash = 'ef584d180eee*******************'
number='+15152934803'
client = TelegramClient('00', api_id, api_hash)
client.session.try_load_or_create_new(session_user_id='+15159947451')
client.connect()

but i have this error

raise error
telethon.errors.RPCError: (RPCError(...), 'AUTH_KEY_UNREGISTERED (401):       The key is not registered in the system.')
Heraclitus answered 17/6, 2017 at 13:54 Comment(1)
how do you disconnet? Are you calling any disconnet function from Telethon at the end?Tumefy
U
5

The problem is this line:

client = TelegramClient('+15xxxxxxxxx', api_id, api_hash)

You don't have to pass your phone number as a first parameter. You have to pass the name of the session, for instance, 'myname'.

You get this:

telethon.errors.RPCError: (RPCError(...), 'AUTH_KEY_UNREGISTERED (401):       The key is not registered in the system.')

Because you've changed the name of the session (and now called it '00'), and you haven't logged it on that one. So in order to solve your problem simply:

client = TelegramClient('some_name', api_id, api_hash)
client.connect()
if not client.is_user_authorized():
    client.send_code_request('+15xxxxxxxxx')
    client.sign_in('+15xxxxxxxxx', cod)

And then remove the .send_code_request(...) line:

client = TelegramClient('some_name', api_id, api_hash)
client.connect()

Notice that if you change 'some_name' for some .session that doesn't exist yet, you will have to create it again. Also, you can rename the .session file to any name you want, and use its name as a parameter (since it already exists).

Unwatched answered 19/6, 2017 at 8:26 Comment(2)
@Heraclitus This is the correct answer, Thanks to Lonami for the answer. I will be removing mine shortly. Go Telethon!Tumefy
@Unwatched Great work on Telethon. But i am having issues when implementing the login/authorization in Flask.Cuyler
Y
3
from telethon import TelegramClient

# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = ****** # Your api_id
api_hash = '********************************' # Your api_hash
phone_number = '+989122594574' # Your phone number

client = TelegramClient(phone_number, api_id, api_hash)
client.connect()

if not client.is_user_authorized():
    client.send_code_request(phone_number)
    client.sign_in(phone_number, input('Enter the code: '))


client.send_message('amir2b', 'Hello! Amir Bashiri')
Yand answered 10/9, 2017 at 6:24 Comment(1)
Welcome to SO. Code only answers are not considered as good answers, please post text explaining what was the issue and how your code resolves it.Bowlin

© 2022 - 2024 — McMap. All rights reserved.