如何使用Telethon从频道ID获取Telegram频道名称

huangapple go评论61阅读模式
英文:

How to get Telegram channel name from channel id using Telethon

问题

我尝试从频道ID中提取频道名称或其他频道详情。

我正在使用Telethon文档中定义的代码:

api_id = #######
api_hash = ##################

client = TelegramClient(session_name, api_id, api_hash)
client.start()

channel_id1 = 1141171940
print(client.get_entity(PeerChannel(channel_id1)))

现在对于给定的频道ID,我得到了频道的详情!

然而,对于下面的频道ID:

channel_id2 = 1126629353
print(client.get_entity(PeerChannel(channel_id2)))

代码不起作用。

错误:

ValueError: 无法找到PeerChannel(channel_id=1126629353) (PeerChannel)的输入实体。请阅读https://docs.telethon.dev/en/stable/concepts/entities.html以获取更多详细信息。

既然我已经知道了这个频道的信息。如果我提供频道名称,然后检索频道详情,那么ID与上面提供的完全相同。

print(client.get_entity("https://t.me/kremlinprachka"))

输出(简化):Channel(id=1126629353, title='Кремлёвская прачка')

请帮助我弄清楚该怎么做?

英文:

I am trying to extract channel name or rest of channel details from channel ID.

I'm using the code as defined in Telethon's documentation:

api_id = #######
api_hash = ##################

client = TelegramClient(session_name, api_id, api_hash)
client.start()

channel_id1 = 1141171940
print(client.get_entity(PeerChannel(channel_id1)))

Now for this given channel ID, I'm getting the channel details!

However, for below channel ID:

channel_id2 = 1126629353
print(client.get_entity(PeerChannel(channel_id2)))

Code is not working.

Error:

ValueError: Could not find the input entity for PeerChannel(channel_id=1126629353) (PeerChannel). Please read https://docs.telethon.dev/en/stable/concepts/entities.html to find out more details.

Since, I already know this channel information. If I give channel name and then retrieve the channel details, there ID is exact same as provided above.

print(client.get_entity("https://t.me/kremlinprachka"))

Output (reduced): Channel(id=1126629353, title='Кремлёвская прачка')

can you please help me figure out what to do?

答案1

得分: 1

这是自然的。

当您将 int id 传递给 get_entityget_input_entity,Telethon 需要构建一个 InputPeerChannel(在您的情况下),并将其传递给 GetChannelsRequest,但是,如果您查看参数,它需要一个名为 access_hash 的东西,这些是在 Telegram 认为您需要访问某些内容时,从 User、Channel 对象、事件或进行其他请求中接收到的。例如,通过调用 get_entity("username") 会触发 ResolveUsernameRequest。在属性深处,它将包含一个 Channel 对象,如果您仔细查看,您将找到一个名为 access_hash 的属性,Telethon 的操作是,对于您接收到的每个事件和每个请求以及其响应,它将首先将所有的 id 映射到访问哈希值并存储到内存中,然后最终存储到工作目录中的 .session 文件中,以备以后重复使用。

所以,如果您只是在代码中更改了顺序,您将看到它在下一次运行时起作用...

  username = client.get_entity("https://t.me/kremlinprachka")
  print("With username:", username)
  # 现在它被缓存
  ids = username.id
  # 现在您可以在将来使用该 id,因为 access_hash 已经被存储。
  print("With Id:", client.get_entity(PeerChannel(ids)))

您必须遵循的基本原则:

  • 您传递给库的任何整数 id 必须是通过各种方式从库内打印/存储的,从其他地方获取的 id 会导致引发 ValueError,内部找不到哈希值。
  • 不要删除 .session 文件。
  • 调用 client.disconnect() 以确保内存缓存的安全导出。
英文:

This is natural.

When you pass int id to get_entity or get_input_entity, Telethon needs to construct an InputPeerChannel (in your case), and pass it to the GetChannelsRequest, however, if you look at the parameters, it requires something called access_hash, those are received to your current session when Telegram feels like you need access to something. as attributes in User, Channel objects, from events or making other requests, for instance get_entity("username") will call ResolveUsernameRequest. deep within the attributes, it will contain a Channel obj, if you look hard enough at it, you will find an attribute called access_hash, what telethon does is, for every event you receive for every request you make and its response, it will store all the ids mapped to access hashes from those objects it finds to memory first, then to a .session file in working directory eventually for upcoming re-usage.

so, if you just switched your order in code, you'd see it working for next times...

  username = client.get_entity("https://t.me/kremlinprachka")
  print("With username:", username)
  # now it gets cached
  ids = username.id
  # now you can use the id for upcoming times, since access_hash was stored.
  print("With Id:", client.get_entity(PeerChannel(ids)))

Basics you must follow:

  • Any integer id you pass to the library MUST be printed/stored from within the library through innumerable ways, id taken from anywhere else guarantees reaching a ValueError and no hash to be found internally.
  • Do NOT delete the .session file.
  • Call client.disconnect() properly to ensure export of memory cache safely.

huangapple
  • 本文由 发表于 2023年3月7日 03:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定