简体   繁体   English

如何使用 Telethon 获取电报私人频道 ID

[英]How to get a telegram private channel id with 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.由于我在工作中使用这个频道,为了加快我想要使用 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.问题是我无法过滤到达该特定频道 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.我尝试了不同的技术来获取我的频道 ID,但错误总是相同的。 In particular:尤其是:

  1. The channel is private so it has no username ("@blablabla")该频道是私人频道,因此没有用户名(“@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我试图处理所有传入的消息,直到管理员在频道上发送消息,打印发件人信息并从“ID”键中获取值
  4. I have tried to use telegram web and get the ID from the url (also adding -100 in front of it)我尝试使用电报 web 并从 url 获取 ID(还在其前面添加 -100)

But when I put the ID in the parameter chats , I get always the error reported above.但是当我将 ID 放入参数chats时,我总是得到上面报告的错误。

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 private_chat 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.您可以过滤发送到 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.顾名思义,它是私人的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM