簡體   English   中英

我怎樣才能恢復老電報中的舊會話並再次連接(不再發送代碼))

[英]how i can restore sessions old in telethon telegram and connect this again(without send again code))

我用這個腳本連接並在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)

有了這個鱈魚我可以在這個號碼電報登錄好並創建文件:+ 15159947451.session。

現在我關閉並斷開連接,我怎么能再次使用此文件+ 15159947451.session登錄此號碼。

我使用此代碼但此代碼有錯誤:

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)

您不必將您的電話號碼作為第一個參數傳遞。 您必須傳遞會話的名稱,例如“myname”。

你得到這個:

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

請注意,如果您為某些尚未存在的.session更改“some_name”,則必須再次創建它。 此外,您可以將.session文件重命名為您想要的任何名稱,並將其名稱用作參數(因為它已經存在)。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM