Search code examples
pythontelegramtelethon

How to do a telethon login using GUI


I'm new to telethon, and I'm trying to make a GUI for my application using python.

The problem is when I run this code:

from telethon import TelegramClient

id = ******
hash = *******

client = TelegramClient("test",id,hash)

client.start()

it automatically runs a terminal login, so I can't actually use that in a GUI.

I also saw :

client.start(phone=your_phone_callback,password=your_password_callback,code_callback=your_code_callback)

I don't really understand how to get the confirmation code at the first place.

How to do a telethon login using GUI?


Solution

  • Don't use client.start()

    use

       client = TelegramClient(f"session", api_id, api_hash)
       await client.connect()
        
       #phone = <User phone number>
    
       # This will send the code to the user. You have to get it using the front end
       phone_code = await client.send_code_request(phone)
       
       phone_code_hash = phone_code.phone_code_hash
    
       #code = <Code from the user>
    
       await client.sign_in(phone, code=code, phone_code_hash=phone_code_hash)
       
    

    this will log you in without using the terminal.