Telethon: check if client instance is logged in

Question:

I am trying to log the user into the account using the telegram bot API, and I can’t find a way to check if the client instance has access to an account…

this is my instance:

client = TelegramClient(client_name, API_ID, API_HASH)

by using client.start() it detects if the user is logged in or not, so i must have access to that too…

Asked By: Hamid Bakhtiari

||

Answers:

You need to use get_me(): it will return the current logged in user or None if there isn’t one.

client = TelegramClient(client_name, API_ID, API_HASH)
if (await client.get_me()):
    # client has an user logged in
else:
    # client hasn't an user logged in 

Also, if you take a look at the source code, you’ll see that start() is doing the same.

Answered By: TheKill-996
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.