Random User Retrieval with Telethon

  • Share this:

Code introduction


This function uses the Telethon library to randomly select a user from a provided list of users and retrieve information about that user. It first randomly selects a user from the list, then creates an InputPeerUser object, and finally sends a GetUsersRequest request to get user information.


Technology Stack : Telethon

Code Type : Telethon API Function

Code Difficulty : Intermediate


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

    # Select a random user from the provided list of users
    users = [arg1, arg2]  # Example list of user IDs
    selected_user = random.choice(users)
    
    # Create an InputPeerUser for the selected user
    input_peer_user = InputPeerUser(selected_user)
    
    # Send a request to get the user information
    client = 'your_telethon_client_instance'
    response = client(GetUsersRequest([input_peer_user]))
    
    # Return the first user's first name
    return response.users[0].first_name                
              
Tags: