skip to Main Content

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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. You can print all the dialogs/conversations that you are part of.
    also you need to remove -100 prefix from the id you got like: -1001419092328 = 1419092328 (actual ID)

    from telethon import TelegramClient, events
    
    
    client = TelegramClient("bot", API_ID, API_HASH)
    
    client.start()
    print("🎉 Connected")
    
    
    @client.on(events.NewMessage())
    async def my_event_handler(event):
        
        async for dialog in client.iter_dialogs():
            print(dialog.name, 'has ID', dialog.id) # test ID -1001419092328
    
    
    
    client.run_until_disconnected()
    

    if you want to listen to a specific channel, you can use channel_id=1419092328. you will only receive messages that are broadcasted to it:

    from telethon import TelegramClient, events
    from telethon.tl.types import PeerChannel
    
    
    
    print(f"👉 Connecting...")
    client = TelegramClient("bot", API_ID, API_HASH)
    
    client.start()
    print("🎉 Connected")
    
    
    @client.on(events.NewMessage(PeerChannel(channel_id=1419092328)))
    async def my_event_handler(event):
        msg = event.text    
    
        print(f"[M] {msg}")
    
    
    client.run_until_disconnected()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search