Emad Helmi
Emad Helmi

Reputation: 695

Find session id with telethon and kill the session

Before I ask this question I checked here. I want to kill all other sessions except the session with which I am connecting now. Based on the telethon api I used all_sessions = client(GetAuthorizationsRequest()).to_dict() and I get this result:

{
       '_': 'Authorization',
       'api_id': ...,
       'app_name': '...',
       'app_version': '4.1.4',
       'country': 'Unknown',
       'date_active': ...,
       'date_created': ...,
       'device_model': 'SamsungSM-G920F',
       'flags': 0,
       'hash': ...,
       'ip': '...',
       'platform': 'Android',
       'region': '',
       'system_version': 'SDK 23'
}

I want to kill this session but I dont know what is te session id mentioned in the linke above(telethon API docs). I tried with these to commands:

client(DestroySessionRequest(api_id))
client(DestroySessionRequest(hash))

But not only no sessions remove but also no response from the apis and the commands waiting and waiting for the response with no error or no exceptions.How can I kill the session?

Upvotes: 3

Views: 9347

Answers (3)

Batuhan Kahraman
Batuhan Kahraman

Reputation: 31

Try this

GetSessions = await client(functions.account.GetAuthorizationsRequest()) 
if len(GetSessions.authorizations)>1:
    print("Another Session    :\tYes")
    for ss in GetSessions.authorizations:
        SessionHash = ss.hash
        SessionIp   = ss.ip
        if SessionHash>0:
            result = await client(functions.account.ResetAuthorizationRequest(hash=SessionHash))
            print("Session Killed     :\t" + str(SessionIp)) 
else:
    print("Another Session    :\tNo")

Upvotes: 2

Avi_av
Avi_av

Reputation: 161

To kill the other sessions, you need to use the ResetAuthorizationRequest function.

Example from official documentation:

from telethon.sync import TelegramClient
from telethon import functions, types
with TelegramClient(name, api_id, api_hash) as client:
    result = client(functions.account.ResetAuthorizationRequest(hash=-12398745604826))
print(result)

https://lonamiwebs.github.io/Telethon/methods/account/reset_authorization.html#examples

Upvotes: 5

Alex
Alex

Reputation: 2174

To delete current session you:

from telethon import TelegramClient

# start session
client = TelegramClient(username, api_id, api_hash).start()

# Now you can use all client methods listed below, like for example...
client.send_message('me', 'Hello to myself!')


# list all sessions
print(client.session.list_sessions())

# delete current session (current session is associated with `username` variable)
client.log_out()

Telethon automatically creates a .session file to store the session details every time a new username is used. The file name starts with the username variable (e.g. my_username.session). Session files are stored in file system permanently so you can sometimes see several available sessions. You can manually remove session files you do not need and the associated session will not be available anymore. More info about Telethon sessions could be found in Telethon API documentation.

Upvotes: 0

Related Questions