Random User Fetcher with Telethon

  • Share this:

Code introduction


This function uses the Telethon library to fetch random users from Telegram's database.


Technology Stack : Telethon

Code Type : Function

Code Difficulty : Intermediate


                
                    
def get_random_user[arg1, arg2,...]:
    from telethon import TelegramClient
    from telethon.tl.functions.users import GetUsersRequest
    from telethon.tl.types import InputPeerEmpty

    # Initialize TelegramClient
    client = TelegramClient('session_name', 'api_id', 'api_hash')

    async def fetch_random_users():
        await client.start()
        # Fetch a random user from the database
        random_users = await client(GetUsersRequest([InputPeerEmpty()]))
        return random_users.users

    # Run the asynchronous function and fetch the users
    users = asyncio.get_event_loop().run_until_complete(fetch_random_users())

    return users                
              
Tags: