我如何恢复 Telethon 电报中旧的会话并再次连接(无需再次发送代码)) [英] how i can restore sessions old in telethon telegram and connect this again(without send again code))

查看:90
本文介绍了我如何恢复 Telethon 电报中旧的会话并再次连接(无需再次发送代码))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此脚本在 Telethon 中连接和创建会话

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)

使用这个 cod,我可以在这个号码电报中登录并创建文件:+15159947451.session.

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

现在我关闭并断开连接,我怎么能用这个文件+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()

但是我有这个错误

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

推荐答案

问题是这一行:

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'.

你明白了:

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

因为您已经更改了会话的名称(现在称为00"),并且您还没有将其登录到该会话中.因此,为了简单地解决您的问题:

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)

然后删除 .send_code_request(...) 行:

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

请注意,如果您为某个尚不存在的 .session 更改 'some_name',则必须重新创建它.此外,您可以将 .session 文件重命名为您想要的任何名称,并将其名称用作参数(因为它已经存在).

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).

这篇关于我如何恢复 Telethon 电报中旧的会话并再次连接(无需再次发送代码))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆