Setting Random Telegram User Status with Telethon

  • Share this:

Code introduction


This code uses the Telethon library to set a random status message for a Telegram user.


Technology Stack : Telethon, Telegram

Code Type : The type of code

Code Difficulty : Advanced


                
                    
def generate_random_user_status(message):
    from telethon import TelegramClient, errors
    from telethon.tl.functions.account import UpdateStatusRequest
    from telethon.tl.types import InputPeerUser

    # Initialize the Telegram client
    api_id = 'YOUR_API_ID'
    api_hash = 'YOUR_API_HASH'
    phone = 'YOUR_PHONE_NUMBER'
    client = TelegramClient('session_name', api_id, api_hash)

    async def set_random_status():
        try:
            # Connect to the Telegram server
            await client.start(phone)
            print("Client connected")
            
            # Generate a random status message
            random_message = "Random Status " + str(random.randint(1, 100))
            
            # Set the random status
            await client.send_message('me', random_message)
            await client(UpdateStatusRequest(random_message))
            print("Random status set")
        except errors.RPCError as e:
            print(f"RPC Error: {e}")
        except Exception as e:
            print(f"An error occurred: {e}")
        finally:
            await client.stop()

    # Run the asynchronous function
    client.loop.run_until_complete(set_random_status())

# Run the function
generate_random_user_status("Random Status")