Find session id with telethon and kill the session
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:
all_sessions = client(GetAuthorizationsRequest()).to_dict()
'_': '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:
session id
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?
2 Answers
2
To kill the other sessions, you need to use the ResetAuthorizationRequest
function.
ResetAuthorizationRequest
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
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.
.session
my_username.session
You can write a script that will manage all your session files and remove ones you do not need.
– Alex
Sep 16 '18 at 6:34
If another user has a session file with my account can kill him with telegram app with terminate other sessions section. how can I do it with telethon?
– Emad Helmi
Sep 16 '18 at 6:35
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
This just deletes the current session file. I want to not only do this with other sessions than the current session but also I want to terminate all other sessions which their session files are not on my local machine
– Emad Helmi
Sep 16 '18 at 6:01