How can I restore old sessions in the telegram of the telethon and connect it again (without sending the code)

I use this script to connect and create sessions in a 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 well enter this telegram and create a file: + 15159947451.session.

now I am closing and shutting down, how can I enter this number again with this file + 15159947451.session.

I am using this code, but this code has an 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.')
0
source share
2 answers

The problem is this line:

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

You do not need to pass your phone number as the first parameter. You must pass a session name, for example, "myname".

You get the following:

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

( "00" ), . , :

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)

.send_code_request(...):

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

, "some_name" .session, , . , .session , , ( ).

+1
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')
+1

Source: https://habr.com/ru/post/1679495/


All Articles