我需要计算电报中的消息数或获取消息历史记录。 告诉我,这可能吗? 我知道,我可以在电报桌面的某个成员的聊天中查看消息。也许我可以在任何对话中做到这一点? 谢谢!
答案 0 :(得分:1)
您可以使用Telegram API计算电报中的消息,获取消息历史记录以及执行大量其他操作。这是一篇精彩的文章,逐步描述了这个过程:https://towardsdatascience.com/introduction-to-the-telegram-api-b0cd220dbed2
对于我来说,它对于检索邮件和计算邮件都很有吸引力。让我们看看文章中的代码:
counts = {}
# create dictionary of ids to users and chats
users = {}
chats = {}
for u in dialogs.users:
users[u.id] = u
for c in dialogs.chats:
chats[c.id] = c
for d in dialogs.dialogs:
peer = d.peer
if isinstance(peer, PeerChannel):
id = peer.channel_id
channel = chats[id]
access_hash = channel.access_hash
name = channel.title
input_peer = InputPeerChannel(id, access_hash)
elif isinstance(peer, PeerChat):
id = peer.chat_id
group = chats[id]
name = group.title
input_peer = InputPeerChat(id)
elif isinstance(peer, PeerUser):
id = peer.user_id
user = users[id]
access_hash = user.access_hash
name = user.first_name
input_peer = InputPeerUser(id, access_hash)
else:
continue
get_history = GetHistoryRequest(
peer=input_peer,
offset_id=0,
offset_date=None,
add_offset=0,
limit=1,
max_id=0,
min_id=0,
)
history = client(get_history)
if isinstance(history, Messages):
count = len(history.messages)
else:
count = history.count
counts[name] = count
print(counts)
让我们添加排序:
sorted_counts = sorted(counts.items(), key=lambda x: x[1], reverse=True)
for name, count in sorted_counts:
print('{}: {}'.format(name, count))
我们得到了消息计数器结果:
Group chat 1: 10000
Group chat 2: 3003
Channel 1: 2000
Chat 1: 1500
Chat 2: 300
P.S。这是一个简单的(用户)来自官方维基的聊天方式,但从我的角度来看,它非常有限且不适合于程序化目的:
答案 1 :(得分:0)
从任何一种语言使用Telegram API的好方法是使用https://www.t-a-a-s.ru/。您无需安装任何内容-您可以仅通过HTTP请求进行查询。
{
"api_key": "YOUR_API_KEY",
"@type": "getChatHistory",
"chat_id": "xxxxxxxxxxx",
"from_message_id": '0',
"offset": 0,
"limit": 100,
}