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 media message containing an image to a specified chat ID. The function takes a client object, chat ID, and file path as arguments.
Technology Stack : Telethon, Python
Code Type : Telethon API usage
Code Difficulty : Intermediate
def send_random_media_message(client, chat_id, file_path):
from telethon.tl.functions.messages import SendMediaRequest
from telethon.tl.types import InputMediaDocument, InputPeerUser
# Prepare the input file and chat
input_file = InputMediaDocument(file_path, "image/jpeg")
input_chat = InputPeerUser(chat_id)
# Send the media to the chat
client.send_message(input_chat, input_file)