首页 > 解决方案 > 使用 Telethon 查找会话 ID 并终止会话

问题描述

在我问这个问题之前,我检查了这里。我想杀死除了我现在连接的会话之外的所有其他会话。基于我使用的 Telethon api 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'
}

我想终止这个会话,但我不知道session id上面的链接中提到了什么(telethon API 文档)。我尝试使用这些命令:

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

但不仅没有删除会话,而且没有来自 api 的响应,并且命令等待并等待响应没有错误或没有异常。我怎样才能终止会话?

标签: pythontelegramtelethon

解决方案


要终止其他会话,您需要使用该ResetAuthorizationRequest功能。

来自官方文档的示例:

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


推荐阅读