Search code examples
telegramtelethon

On message event from telethon returns from_id None


I am getting an error when trying to access the from_id attribute from an message that has only media (photos, documents, voice, video) in it. Error:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py", line 467, in _dispatch_update
    await callback(event)
  File "d:\Code\asd\back.py", line 16, in on_message
    await pl.add_message(f'saved/{event.peer_id.user_id}/chat.json', event)
  File "d:\Code\asd\plugins.py", line 25, in add_message
    'author': event.from_id.user_id if event.from_id.user_id != None else event.peer_id.user_id,
AttributeError: 'NoneType' object has no attribute 'user_id'

No error appears when I send media, it only happens when I get the media send to me. I hope some of you could help me finding an answer to my question.

I've been trying to use peer_id instead of from_id, but that didn't help eighter


Solution

  • since the introduction of Anonymous Admins, messages in Telegram chats can have no sender's.

    You should be using the message.sender_id instead of from_id, in Telethon.

    Reference: https://t.me/TelethonChat/126622