Random Chat Photo Retrieval in Private Chats

  • Share this:

Code introduction


This function, using the Pyrogram library, retrieves a random chat photo from a private chat and sends it to the user.


Technology Stack : Pyrogram

Code Type : Chatbot function

Code Difficulty : Intermediate


                
                    
def random_chat_photo(update, context):
    chat = update.effective_chat
    if chat.type == 'private':
        with context.bot.get_chat_photo(chat.id) as photo:
            context.bot.send_photo(chat.id, photo.file_id)
    else:
        context.bot.send_message(chat.id, "This command can only be used in private chats.")                
              
Tags: