[Python] Get telegram user status with telethon

Discussão em 'Python' iniciado por Stack, Julho 31, 2025.

  1. Stack

    Stack Membro Participativo

    im trying to create a python script that checks wether the user is online or not. I succeed making a season between Telegram and python using telethon but Im really having a bad time getting the correct syntax to get one of my contacts status. Any help will be appricated !

    from telethon.tl.types import UserStatusOnline, UserStatusOffline, ContactStatus
    from telethon.sync import TelegramClient
    from datetime import datetime

    ### Client Side ###
    phone = "+"
    api_id =
    api_hash = ""
    client = TelegramClient(phone, api_id, api_hash)
    client.connect()
    if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter the code: '))
    else:
    print("Logging Complete")

    account = client.get_entity('chats_name')
    if isinstance(account.status, UserStatusOffline):
    if contact.online != False:
    contact.online = False
    event.respond(f'{utc2localtime(account.status.was_online).strftime(DATETIME_FORMAT)}: {contact.name} went offline.')
    elif contact.last_offline != account.status.was_online:
    if contact.last_offline is not None:
    event.respond(f'{utc2localtime(account.status.was_online).strftime(DATETIME_FORMAT)}: {contact.name} went offline after being online for short time.')
    else:
    event.respond(f'{utc2localtime(account.status.was_online).strftime(DATETIME_FORMAT)}: {contact.name} went offline.')
    contact.last_offline = account.status.was_online
    elif isinstance(account.status, UserStatusOnline):
    if contact.online != True:
    contact.online = True
    event.respond(f'{datetime.now().strftime(DATETIME_FORMAT)}: {contact.name} went online.')
    else:
    if contact.online != False:
    contact.online = False
    event.respond(f'{datetime.now().strftime(DATETIME_FORMAT)}: {contact.name} went offline.')
    contact.last_offline = None

    Continue reading...

Compartilhe esta Página