Reputation: 100
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...
Upvotes: 1
Views: 2775
Reputation: 7141
The accepted answer may fail if you don't connect the client instance anywhere else. Make sure to connect before attempting to perform requests:
from telethon import TelegramClient
async def main():
client = TelegramClient(session_name, api_id, api_hash)
await client.connect()
if await client.is_user_authorized():
... # no need to login
else:
... # client would need to login
Do not use async with
or client.start()
if your goal is checking whether the session is logged-in. In Telethon v1, these will perform the interactive login flow and prompt for phone, code and password as needed.
If you want to handle that manually or in another manner, use client.send_code_request
and client.sign_in
yourself.
Upvotes: 0
Reputation: 1069
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.
Upvotes: 3