You can download this code by clicking the button below.
This code is now available for download.
This function uses the Pyrogram library to send a randomly selected photo or video to a specified chat.
Technology Stack : Pyrogram
Code Type : Pyrogram API Function
Code Difficulty : Intermediate
def send_random_media(client, chat_id, media_type='photo'):
"""
This function sends a random media (photo or video) to a specified chat using Pyrogram.
"""
import random
from pyrogram import Client, filters
from pyrogram import types
# Define a list of media files
media_files = [
"path/to/photo1.jpg",
"path/to/video1.mp4"
]
# Select a random media file
selected_media = random.choice(media_files)
# Create a file object for the selected media
with open(selected_media, 'rb') as file:
media = types.InputMediaFile(file.read(), media_type)
# Send the media to the chat
client.send_media(chat_id, media)