Search code examples
python-3.xtelethon

How to get the "last seen..." data of a user


Within each Telegram application's GUI there's a setting for each user called "Last seen & online" if this setting is set to "Everybody" on an account other users should be able to view the "Last seen..." data of the account with the changed settings. I am building an application using Telethon and need this data for the current user. In the Telethon library how is it programmatically possible to get the "last seen..." data of a user other than the current user (the account making the Telethon API calls)?

Example last seen online image

Example "Last seen & online" settings


Solution

  • Like this;

    suser = await client.get_entity('username')
    _last = None
    
    
    from datetime import date
    from telethon.tl.types import UserStatusOnline, UserStatusOffline
    
    if isinstance(suser.status, UserStatusOnline):
        _last = date(suser.status.expires.year, suser.status.expires.month, suser.status.expires.day)
    
    if isinstance(suser.status, UserStatusOffline):
        _last = date(suser.status.was_online.year, suser.status.was_online.month, suser.status.was_online.day)
    
    
    print(_last)