Search code examples
pythontelegrampyrogram

How to log in to telegram using pyrogram?


Please help, on the first run, I executed the send_code() function and saved the phone_code_hash, on the second run, I executed main() with the code received from the telegram and the hash code saved from the send_code function, but when sign_in I get an error that the code has expired, although all data is correct.

client = Client(
        'sessin', 
        api_id=app_id, 
        api_hash=app_hash,
    )

async def send_code(phone_number):
    await client.connect()
    result = await client.send_code(phone_number)
    phone_code_hash = result.phone_code_hash


async def main(phone_number, phone_code, phone_hash_code):
    await client.connect()
    result = await client.sign_in(phone_number=phone_number, phone_code=phone_code, phone_code_hash=phone_hash_code)

I tried to find information but didn't find it.


Solution

  • Here is example to login with pyrogram.

    client = Client("sessions", api_id, api_hash)
    
    client.connect()
    
    sent_code = client.send_code(phone)
    
    code = input("Enter the code : ")
    
    signed_in = client.sign_in(phone, sent_code.phone_code_hash, code)
    
    client.disconnect()