I’d like to check with the current online user and when the last time of access is if it’s offline.
Below is my code, but there are two problems.
First of all, I’m the only one who’s online. All other users are marked “recently” and not checked even though they’re talking in real time.
Second, users who are marked “Recently” are not viewed for recent access times.
Of course, I have administrative rights.
Is there a good way to solve these two problems?
<code>from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, UserStatusOnline, UserStatusRecently, UserStatusOffline
from datetime import datetime
api_id = 12345
api_hash = '12345'
phone = '12345'
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: '))
chats = []
last_date = None
chunk_size = 200
groups = []
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash=0
))
chats.extend(result.chats)
for chat in chats:
groups.append(chat)
print('Choose a group to scrape members from:')
for i, g in enumerate(groups):
print(f"{i} - {g.title}")
target_group=groups[4]
print('Fetching Members...')
all_participants = client.get_participants(target_group, aggressive=True)
print(all_participants)
online_users = [user for user in all_participants if isinstance(user.status, UserStatusOnline) or isinstance(user.status, UserStatusRecently) or isinstance(user.status, UserStatusOffline)]
print(f"Total online/recently active participants: {len(online_users)}")
for user in online_users:
name = f"{user.first_name or ''} {user.last_name or ''}".strip()
status_time = None
if isinstance(user.status, UserStatusOnline):
status_time = user.status.expires
status = "Online"
elif isinstance(user.status, UserStatusRecently):
status_time = ""
status = "Recently"
elif isinstance(user.status, UserStatusOffline):
status_time = user.status.was_online
status = "Offline"
if status_time:
status_time_str = status_time.strftime('%Y-%m-%d %H:%M:%S')
else:
status_time_str = "Unknown"
print(
f'User ID: {user.id}, Username: {user.username}, Name: {name}, Status: {status}, Last Seen: {status_time_str}')
</code>
<code>from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, UserStatusOnline, UserStatusRecently, UserStatusOffline
from datetime import datetime
api_id = 12345
api_hash = '12345'
phone = '12345'
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: '))
chats = []
last_date = None
chunk_size = 200
groups = []
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash=0
))
chats.extend(result.chats)
for chat in chats:
groups.append(chat)
print('Choose a group to scrape members from:')
for i, g in enumerate(groups):
print(f"{i} - {g.title}")
target_group=groups[4]
print('Fetching Members...')
all_participants = client.get_participants(target_group, aggressive=True)
print(all_participants)
online_users = [user for user in all_participants if isinstance(user.status, UserStatusOnline) or isinstance(user.status, UserStatusRecently) or isinstance(user.status, UserStatusOffline)]
print(f"Total online/recently active participants: {len(online_users)}")
for user in online_users:
name = f"{user.first_name or ''} {user.last_name or ''}".strip()
status_time = None
if isinstance(user.status, UserStatusOnline):
status_time = user.status.expires
status = "Online"
elif isinstance(user.status, UserStatusRecently):
status_time = ""
status = "Recently"
elif isinstance(user.status, UserStatusOffline):
status_time = user.status.was_online
status = "Offline"
if status_time:
status_time_str = status_time.strftime('%Y-%m-%d %H:%M:%S')
else:
status_time_str = "Unknown"
print(
f'User ID: {user.id}, Username: {user.username}, Name: {name}, Status: {status}, Last Seen: {status_time_str}')
</code>
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, UserStatusOnline, UserStatusRecently, UserStatusOffline
from datetime import datetime
api_id = 12345
api_hash = '12345'
phone = '12345'
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: '))
chats = []
last_date = None
chunk_size = 200
groups = []
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash=0
))
chats.extend(result.chats)
for chat in chats:
groups.append(chat)
print('Choose a group to scrape members from:')
for i, g in enumerate(groups):
print(f"{i} - {g.title}")
target_group=groups[4]
print('Fetching Members...')
all_participants = client.get_participants(target_group, aggressive=True)
print(all_participants)
online_users = [user for user in all_participants if isinstance(user.status, UserStatusOnline) or isinstance(user.status, UserStatusRecently) or isinstance(user.status, UserStatusOffline)]
print(f"Total online/recently active participants: {len(online_users)}")
for user in online_users:
name = f"{user.first_name or ''} {user.last_name or ''}".strip()
status_time = None
if isinstance(user.status, UserStatusOnline):
status_time = user.status.expires
status = "Online"
elif isinstance(user.status, UserStatusRecently):
status_time = ""
status = "Recently"
elif isinstance(user.status, UserStatusOffline):
status_time = user.status.was_online
status = "Offline"
if status_time:
status_time_str = status_time.strftime('%Y-%m-%d %H:%M:%S')
else:
status_time_str = "Unknown"
print(
f'User ID: {user.id}, Username: {user.username}, Name: {name}, Status: {status}, Last Seen: {status_time_str}')
I’d like to know who the online users are and if you’ve recently accessed them, when did you access them.