Pyrogram-based User Information Retrieval

  • Share this:

Code introduction


This function uses the Pyrogram library to retrieve user information for a specified user ID. If the user has set privacy protection, it will return None.


Technology Stack : Pyrogram

Code Type : Function

Code Difficulty : Intermediate


                
                    
def get_random_user_info(user_id):
    from pyrogram import Client, enums
    from pyrogram.errors import UserPrivacyRestrictedError

    # Initialize the Pyrogram client
    app = Client("my_session", api_id=123456, api_hash="my_api_hash")

    def get_info(user):
        try:
            # Retrieve user details
            user_details = app.get_users(user_id=user)
            return user_details
        except UserPrivacyRestrictedError:
            return None

    # Use the client to get random user info
    user_info = get_info(user_id)
    
    # Close the client session
    app.stop()
    
    return user_info                
              
Tags: