You can download this code by clicking the button below.
This code is now available for download.
This function uses the Telethon library to send a random media message to a specified chat. It checks the file type and sends a photo or document accordingly.
Technology Stack : Telethon
Code Type : Function
Code Difficulty : Intermediate
def send_random_media_message(client, chat, file_path):
from telethon.tl.functions.messages import SendMediaRequest
from telethon.tl.types import InputPeerUser, InputMediaPhoto, InputMediaDocument
# Check if the file is an image or a document
if file_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
media = InputMediaPhoto(InputPeerUser(chat.id), open(file_path, 'rb'))
else:
media = InputMediaDocument(InputPeerUser(chat.id), open(file_path, 'rb'))
# Send the media message
client(SendMediaRequest(chat, media))