Reputation: 71
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:
But when I put the ID in the parameter chats, I get always the error reported above.
Thanks in advance, Have a nice day
Upvotes: 7
Views: 13308
Reputation: 51
You should try to join the channel if is public using this code:
channel = await client.get_entity('telegram.me/' + channel_username)
print(channel.id) # here will print your id without -100
Upvotes: -1
Reputation: 7043
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()
Upvotes: 1
Reputation: 1939
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.
Upvotes: 10
Reputation: 3601
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.
Upvotes: -1