Maksim
Maksim

Reputation: 27

Telethon - Error when retrieve user info

I'm stuck with an error in telethon, when trying to get users data. First, I get new messages from some groups, it's ok, but when I try to get user data (name, first_name etc) - sometimes it's ok, but mostly fails with error

ValueError: Could not find the input entity for "12345678". 
Please read https://telethon.readthedocs.io/en/latest/extra/basic/entities.html 
to find out more details.

I read that article a lot of times, tried to use also client.get_input_entity as it says, but it doesn't help

Here is my code:

import logging
from telethon import TelegramClient, events


logging.basicConfig(level=logging.WARNING)
logging.getLogger('asyncio').setLevel(logging.ERROR)

entity = 'session'  # session
api_id = 123456
api_hash = 'hash'
phone = '1234567'

chats = ['group1', 'group2', 'group3']


client = TelegramClient(entity, api_id, api_hash)


@client.on(events.NewMessage(chats=chats))
async def normal_handler(event):


print(event.message.message)
print(event.date)
print(event.from_id)
print(event.message.to_id)
#user = await client.get_input_entity(event.from_id)
user = await client.get_entity(event.from_id)



client.start()
client.run_until_disconnected()

How can I fix that?

And one more question, how can I retrieve info about group? I know it's id from event.message.to_id, but can't get how to get it's name.

The docs for the library looks not very friendly for beginners. =(

Thank you

Upvotes: 1

Views: 4369

Answers (1)

nebm51
nebm51

Reputation: 76

Telegram does not allow to get user profile by integer id if current client had never "saw" it.

Telethon documentation (https://telethon.readthedocs.io/en/latest/extra/basic/entities.html) suggests following options to make contact "seen":

  • if you have open conversation: use client.get_dialogs()
  • if you are in same group: use client.get_participants('groupname')
  • if you are it was forward in group: use client.get_messages('groupname', 100)

Choose which one is applicable in your case. One of them will make contact "seen", and it will be possible to use client.get_entity(event.from_id)

Upvotes: 4

Related Questions