【问题标题】:Find session id with telethon and kill the session使用 Telethon 查找会话 ID 并终止会话
【发布时间】:2021-10-26 09:18:13
【问题描述】:

在我问这个问题之前,我检查了here。我想杀死除了我现在连接的会话之外的所有其他会话。基于我使用的 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 的响应以及等待和等待响应的命令,没有错误或没有异常。我怎样才能终止会话?

【问题讨论】:

    标签: python telegram telethon


    【解决方案1】:

    要终止其他会话,您需要使用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

    【讨论】:

      【解决方案2】:

      要删除当前会话,您:

      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 都会自动创建一个.session 文件来存储会话详细信息。文件名以用户名变量开头(例如my_username.session)。会话文件永久存储在文件系统中,因此您有时可以看到几个可用的会话。您可以手动删除不需要的会话文件,并且相关的会话将不再可用。 有关 Telethon 会议的更多信息,请访问 Telethon API documentation

      【讨论】:

      • 这只是删除当前会话文件。我不仅想对当前会话以外的其他会话执行此操作,而且我想终止其会话文件不在我的本地计算机上的所有其他会话
      • 您可以编写一个脚本来管理您的所有会话文件并删除您不需要的文件。
      • 如果另一个用户与我的帐户有一个会话文件,可以用电报应用程序杀死他并终止其他会话部分。我怎么能用电视节目做到这一点?
      【解决方案3】:

      试试这个

      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")
      

      【讨论】:

        猜你喜欢
        • 2011-07-16
        • 1970-01-01
        • 2013-08-29
        • 2014-08-24
        • 2021-02-22
        • 1970-01-01
        • 2013-12-19
        • 1970-01-01
        • 2014-06-27
        相关资源
        最近更新 更多