Search code examples
pythonfile-uploadtelegramtelethon

how to send an image group with telethon in telegram in python


hey I'm trying to send some images to a group in telegram using telethon to do it with my own account not a bot. but I have a little problem. it errors some how that I think some where I made a mistake in initializing something. the code is:

filenames = ['C:\\Users\\Achak\\Documents\\GitHub\\typhoone\\qq\\0.jpg','C:\\Users\\Achak\\Documents\\GitHub\\typhoone\\qq\\1.jpg']
    for filename in filenames:
        try:
            # Upload the file
            result = await client.upload(filename)
            media = InputMediaPhoto(result)

            # Send the file to the group
            await client.send_file(group, media)
            print(f'Sent {filename} to group with ID {group_id}')
        except PeerFloodError:
            print('Sending too many messages, stopping...')
            break

and this is the error: telethon.errors.rpcerrorlist.MediaEmptyError: The provided media object is invalid or the current account may not be able to send it (such as games as users) (caused by SendMediaRequest)

I tried changing the location of image to the code container folder bot nothing happened.


Solution

  • If you upload your file with client.upload(filename), you don't need InputMediaPhoto, you can just use the result of upload to send_file.

    From the documentation:

    file = client.upload_file('photo.jpg')
    client.send_file(chat, file)                       # sends as photo
    

    So in your case, just use the result variabel as the paramter:

    result = await client.upload(filename)
    client.send_file(group, result)