我需要阅读应用程序中某些公共频道的消息,例如它发生https://tlgrm.ru/channels/tech据我所知,此业务的机器人将无效。你需要使用客户端api,但是在任何地方,通道方法都连接到你需要的channel_id,但我在哪里得到它我不知道,我只有通道名称,我怎么从它得到它我没有找到这样的方法。
如何通过名称获取频道的ID?
答案 0 :(得分:5)
假设您正在使用python,我建议使用Telethon库。您可以使用此段代码从channel_id
获取access_hash
和@username
:
from telethon.tl.functions.contacts import ResolveUsernameRequest
client = TelegramClient(session_file, api_id=X, api_hash='X')
client.connect()
response = client.invoke(ResolveUsernameRequest("username"))
print(response.channel_id)
print(response.access_hash)
确保您已经拥有api_id
和api_hash
。并确保您已对您的应用进行了身份验证,即您的工作有session_file
。如果您不确定如何执行上述步骤,请阅读Github页面中的Telethon的README。
答案 1 :(得分:0)
在最新版本中,您可以使用频道的用户名
进行此操作from telethon.tl.functions.contacts import ResolveUsernameRequest
response = client.invoke(ResolveUsernameRequest(<username>))
messages = client.get_message_history(response.peer,limit=1000)
答案 2 :(得分:0)
获取所有聊天记录的与语言无关的好方法是 https://www.t-a-a-s.ru/。您可以仅通过 HTTP 请求进行聊天记录查询。
登录并接收 API 密钥后,您可以使用任何语言发出请求,如下所示(javascript 示例):
// First, receive chat id from channel username with searchPublicChat
let response = await fetch("https://api.t-a-a-s.ru/client", {
"headers": {
"content-type": "application/json",
},
"method": "POST",
"body": JSON.stringify({
api_key: "xxxxxxxxxx:xxxxxxxxxx-xxxxxxxxxx-xxxxx", // key that you will receive from TaaS after creating client
"@type": "searchPublicChat",
username: "username"
})
});
// Take channel id from response
response = await response.json();
let chatId = response.id;
// Finally get chat history
let messages = await fetch("https://api.t-a-a-s.ru/client", {
"headers": {
"content-type": "application/json",
},
"method": "POST",
"body": JSON.stringify({
api_key: "xxxxxxxxxx:xxxxxxxxxx-xxxxxxxxxx-xxxxx",
"@type": "getChatHistory",
chat_id: chatId,
from_message_id: "0",
limit: "100",
offset: "0"
}),
});