Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To obtain the most recent event in the admin log of a Telegram Telethon channel using the Telethon library in Python, you can use the get_admin_log method. Here is an example code snippet:

from telethon import TelegramClient, events, sync

# Replace the values with your own API ID, API HASH, and phone number
api_id = YOUR_API_ID
api_hash = 'YOUR_API_HASH'
phone = 'YOUR_PHONE_NUMBER'

with TelegramClient('session_name', api_id, api_hash) as client:
    client.start(phone)

    # Replace CHANNEL_USERNAME with the username of the channel
    channel = client.get_entity('CHANNEL_USERNAME')

    # Get the most recent event in the admin log
    log = client(GetAdminLogRequest(channel, 0, 0, 1, 0))
    event = log.events[0]

    print(event.stringify())

This code first creates a TelegramClient object and starts a session with the provided phone number. It then gets the Entity object for the channel using its username.

The GetAdminLogRequest method is used to retrieve the admin log of the channel. The 0 values in the method call are used for the parameters min_id, max_id, and limit, respectively. The 1 parameter for types specifies that only the most recent event should be returned. The 0 parameter for q specifies that no filter is applied.

The result of the GetAdminLogRequest is stored in the log variable. The most recent event is then accessed by indexing into the events property of the log, and printed using the stringify() method.