How to use Python to view online users on Telegram?

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?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật