Random Userpic Sender with Pyrogram

  • Share this:

Code introduction


This function randomly selects a user picture from a predefined list and sends it to the specified chat ID using the Pyrogram library.


Technology Stack : Pyrogram

Code Type : Pyrogram API call

Code Difficulty : Intermediate


                
                    
def random_userpic(chat_id, file_name):
    from pyrogram import Client, filters
    from pyrogram import enums
    import random

    app = Client('my_bot', api_id=123456, api_hash='my_api_hash')

    def get_random_userpic():
        # List of userpics to choose from
        userpics = ['userpic1.jpg', 'userpic2.jpg', 'userpic3.jpg', 'userpic4.jpg']
        return random.choice(userpics)

    try:
        with app:
            # Send a random userpic to the specified chat
            app.send_photo(chat_id=chat_id, photo=get_random_userpic())
    except Exception as e:
        print(f"An error occurred: {e}")                
              
Tags: