You can download this code by clicking the button below.
This code is now available for download.
This function is used to get the online status of a Telegram user. It connects to Telegram and then uses the `GetUserStatusRequest` function from the Telethon library to query the online status of the specified user.
Technology Stack : Telethon
Code Type : Function
Code Difficulty : Intermediate
def random_user_status(user_id):
from telethon import TelegramClient
from telethon.tl.functions.status import GetUserStatusRequest
from telethon.tl.types import UserStatusOnline, UserStatusOffline, UserStatusLastSeen
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
phone_number = 'YOUR_PHONE_NUMBER'
client = TelegramClient('session_name', api_id, api_hash)
with client:
client.start(phone_number)
status = client(function=GetUserStatusRequest(user_id))
if isinstance(status.status, UserStatusOnline):
return "Online"
elif isinstance(status.status, UserStatusOffline):
return "Offline"
elif isinstance(status.status, UserStatusLastSeen):
return f"Last seen: {status.status.was_online}, {status.status.date}"
else:
return "Unknown status"