How to get a telegram private channel id with telethon

Viewed 8171

Hi can't figure out how to solve this problem, so any help will be really appreciated. I'm subscribed to a private channel. This channel has no username and I don't have the invite link (the admin just added me). Since I use this channel at work, to speed up the things I want to process the messages posted on the channel using Telethon.

The core of the program is:

@events.register(events.NewMessage(chats = my_private_channel))
async def handler(event):
    
        #do things

The problem is that I am not able to filter the messages coming to that specific channel id. I get the error:

ValueError: Cannot find any entity corresponding to "0123456789"

I have tried different technique to obtain my channel Id but the error is always the same. In particular:

  1. The channel is private so it has no username ("@blablabla")
  2. I have no invite link
  3. I have tried to process all incoming messages until the admin sent a message on the channel, print sender information and get the value from the "ID" key
  4. I have tried to use telegram web and get the ID from the url (also adding -100 in front of it)

But when I put the ID in the parameter chats, I get always the error reported above.

Thanks in advance, Have a nice day

2 Answers

if you have access to the channel, then it's shown in your chat list.

You have to loop through your chats checking their titles and then store the desired chat in a variable:

my_private_channel_id = None
my_private_channel = None

async for dialog in tg.client.iter_dialogs():
    if dialog.name == "private chat name":
        my_private_channel = dialog
        my_private_channel_id = dialog.id
        break

if my_private_channel is None:
    print("chat not found")
else:
    print("chat id is", my_private_channel_id)

Than you can filter messages sent to my_private_channel.

You can't join a private channel without the invite link, nor can you get any information about it. It's private, as the name implies.

Related