如何使用 Telethon bot API 获取频道的实体?

问题描述:

我无法使用 Telethon Bot get_dialogs,因为该方法仅适用于客户端.因此,当我尝试使用其 id 访问实体时,我收到一个错误.如果我无法解析所有频道,也不能通过 id 访问它们,我该如何访问实体(频道)?我想解析来自私人群组的消息并使用 Telegram Bot.

I cannot get_dialogs with Telethon Bot since the method is only available for a client. Therefore when I try to access the entity with its id I receive an error. How do I access an entity(channel) if I cannot parse all channels and neither access them by id? I would like to parse messages from a private group and use Telegram Bot for it.

在通道运行时将机器人添加到通道应该足以让机器人获取其访问哈希.或者,当机器人在里面时在那里发送消息也应该有效.该哈希值将保存到 .session 文件中.无论如何,如果您使用标记"ID(对于频道,在真实 ID 前加上 -100 前缀或使用 types.PeerChannel(real id)),Telethon 会知道您指的是一个频道,这有助于使其工作.

Adding the bot to the channel while it's running should be enough for the bot to obtain its access hash. Alternatively, sending a message there while the bot is inside should also work. This hash will be saved to the .session file. Regardless, if you use the "marked" ID (for channels, prefix -100 to the real ID or use types.PeerChannel(real id)), Telethon will know you mean a channel, which can help make it work.