Sending Random Photos to Private Chats via Pyrogram

  • Share this:

Code introduction


This function uses the Pyrogram library to send a random photo to a specified private chat.


Technology Stack : Pyrogram, Client, filters, enums

Code Type : Function

Code Difficulty : Intermediate


                
                    
def send_random_photo(chat_id, photo):
    from pyrogram import Client, filters
    from pyrogram import enums

    # Initialize the client
    app = Client("my_account")

    # Define a filter for private chats
    private_chat_filter = filtersChat(filter=lambda c: c.is_private)

    # Send a random photo to the chat
    app.send_photo(chat_id, photo=photo, filter=private_chat_filter)

# Close the client after the operation
    app.send_photo(chat_id, photo=photo, filter=private_chat_filter)
    app.stop()