如何使用 Telethon 获取电报私人频道 ID [英] How to get a telegram private channel id with telethon
问题描述
您好,不知道如何解决这个问题,所以任何帮助将不胜感激.我订阅了一个私人频道.该频道没有用户名,我也没有邀请链接(管理员刚刚添加了我).由于我在工作中使用此频道,因此为了加快处理速度,我想使用 Telethon 处理发布在频道上的消息.
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.
程序的核心是:
@events.register(events.NewMessage(chats = my_private_channel))
async def handler(event):
#do things
问题是我无法过滤到达该特定频道 ID 的消息.我收到错误:
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"
我尝试了不同的技术来获取我的频道 ID,但错误始终相同.特别是:
I have tried different technique to obtain my channel Id but the error is always the same. In particular:
- 该频道是私有的,因此没有用户名(@blablabla")
- 我没有邀请链接
- 我已尝试处理所有传入消息,直到管理员在频道上发送消息、打印发件人信息并从ID"中获取值.键
- 我尝试使用电报网络并从 url 获取 ID(也在其前面添加 -100)
但是当我把 ID 放在参数 chats 中时,我总是得到上面报告的错误.
But when I put the ID in the parameter chats, I get always the error reported above.
提前致谢,祝你有美好的一天
Thanks in advance, Have a nice day
推荐答案
如果您有权访问该频道,则会显示在您的聊天列表中.
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)
您可以过滤发送到 my_private_channel 的消息.
Than you can filter messages sent to my_private_channel.
这篇关于如何使用 Telethon 获取电报私人频道 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!