LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.68k stars 1.38k forks source link

problem with send file #1131

Closed parazitt closed 5 years ago

parazitt commented 5 years ago

hello Dear,

i have a problem with send file my code is: x= clients.upload_file("/tmp/5.jpg") clients.send_file("me", x)

i got:

Traceback (most recent call last):
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/utils.py", line 303, in get_input_media
    if media.SUBCLASS_OF_ID == 0xfaf846f4:  # crc32(b'InputMedia')
AttributeError: 'NoneType' object has no attribute 'SUBCLASS_OF_ID'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/client/uploads.py", line 176, in send_file
    msg = self._get_response_message(request, self(request), entity)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/client/users.py", line 19, in __call__
    r.resolve(self, utils)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/tl/functions/messages.py", line 3403, in resolve
    self.media = utils.get_input_media(self.media)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/utils.py", line 310, in get_input_media
    _raise_cast_fail(media, 'InputMedia')
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/utils.py", line 108, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast NoneType to any kind of InputMedia.

or clients.send_message("me", "sdf", file="/tmp/5.jpg") # or clients.send_file("me", file="/tmp/5.jpg")

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/client/messages.py", line 431, in send_message
    buttons=buttons
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/client/uploads.py", line 176, in send_file
    msg = self._get_response_message(request, self(request), entity)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/client/users.py", line 38, in __call__
    future = self._sender.send(request, ordered=ordered)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/network/mtprotosender.py", line 217, in send
    message = self.state.create_message(request)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/network/mtprotostate.py", line 50, in create_message
    out=True  # Pre-convert the request into bytes
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/tl/core/tlmessage.py", line 52, in __init__
    body = GzipPacked.gzip_if_smaller(self.obj)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/tl/core/gzippacked.py", line 21, in gzip_if_smaller
    data = bytes(request)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/tl/functions/messages.py", line 3426, in __bytes__
    bytes(self.media),
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/tl/types/__init__.py", line 7391, in __bytes__
    bytes(self.file),
TypeError: cannot convert 'NoneType' object to bytes

even when i run this: clients(SendMediaRequest("me",x,'dsf'))

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/client/users.py", line 19, in __call__
    r.resolve(self, utils)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/tl/functions/messages.py", line 3403, in resolve
    self.media = utils.get_input_media(self.media)
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/utils.py", line 384, in get_input_media
    _raise_cast_fail(media, 'InputMedia')
  File "/home/user/telegram/lib/python3.7/site-packages/telethon/utils.py", line 108, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast InputSizedFile to any kind of InputMedia.

how i can fix it?

Lonami commented 5 years ago

This code works fine in the latest versions of the library.

parazitt commented 5 years ago

i fixed this problem:

uploaded = clients.upload_file("/tmp/5.jpg")
media = InputMediaUploadedPhoto(uploaded)
clients.send_file("me",media)

worked fine

Lonami commented 5 years ago

That should not be necessary in the latest version, but okay.

parazitt commented 5 years ago

you right! but i using sync version in multi-thread script

Lonami commented 5 years ago

Sync version is not supported. You can use asyncio and threads if you know how. But this is going offtopic.

ted24 commented 4 years ago

Hi! I met the same problem resending messages with links:

async def main():
    async for message in client.iter_messages('@some_chanel', limit=5):
        try:
            await client.send_message(entity='xxxxx', message=message.message, reply_to=None, file=message.media)
        except NameError:
            print('error: ' + str(NameError))
            pass  
with client:
    client.loop.run_until_complete(main())

then get an error: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/telethon/utils.py", line 348, in get_input_media if media.SUBCLASS_OF_ID == 0xfaf846f4: # crc32(b'InputMedia') AttributeError: 'NoneType' object has no attribute 'SUBCLASS_OF_ID'

Lonami commented 4 years ago

@ted24 your traceback does not correspond with the code of the latest version (thus, you have an old version installed).

ted24 commented 4 years ago

thanks, but latest version doesn't allow to use file=message.media, could you please advise how to resend media without downloading it?

File "/usr/local/lib/python3.7/site-packages/telethon/client/messages.py", line 685, in send_message buttons=buttons File "/usr/local/lib/python3.7/site-packages/telethon/client/uploads.py", line 343, in send_file raise TypeError('Cannot use {!r} as file'.format(file)) TypeError: Cannot use <telethon.tl.types.MessageMediaWebPage object at 0x10e38bda0> as file

Lonami commented 4 years ago

No version has ever supported file=MessageMediaWebPage(...), and where it crashed before, it now raises the correct error.

MrMarble commented 4 years ago

Hi! I'm trying to create an userbot with Telethon but I'm running into the exact same error as @parazitt.

I'm running the current latest version of Telethon (1.16.2) installed from the main branch.

I've tried both uploading first then sending and only sending the file, same outcome.

I was able to send the image a couple of times, like if I send it to myself it works but to a friend the error comes out

uploaded = await bot.upload_file(str(carbon_file))
media = InputMediaUploadedPhoto(uploaded)
await bot.send_file(event.chat, media)

# Just sending
await bot.send_file(event.chat, str(carbon_file))
Traceback (most recent call last):
  File "C:\Users\MrMarble\PycharmProjects\userbot\userbot\modules\carbonsh.py", line 27, in carbon
    await bot.send_file(event.chat, media)
  File "C:\Users\MrMarble\PycharmProjects\userbot\venv\lib\site-packages\telethon\client\uploads.py", line 355, in send_file
    entity = await self.get_input_entity(entity)
  File "C:\Users\MrMarble\PycharmProjects\userbot\venv\lib\site-packages\telethon\client\users.py", line 419, in get_input_entity
    peer = utils.get_peer(peer)
  File "C:\Users\MrMarble\PycharmProjects\userbot\venv\lib\site-packages\telethon\utils.py", line 909, in get_peer
    _raise_cast_fail(peer, 'Peer')
  File "C:\Users\MrMarble\PycharmProjects\userbot\venv\lib\site-packages\telethon\utils.py", line 139, in _raise_cast_fail
    type(entity).__name__, target))
TypeError: Cannot cast NoneType to any kind of Peer.
Lonami commented 4 years ago

The problem is that event.chat is None. It has nothing to do with sending the file. The documentation states chat may be None, and if you're going to use it in a input parameter, event.input_chat should be used instead.