Send Image Media via Telethon to Chat ID

  • Share this:

Code introduction


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)