【问题标题】:Is it possible to get all messages history or get messages count from any chat in telegram是否可以从电报中的任何聊天中获取所有消息历史记录或消息计数
【发布时间】:2020-10-02 04:14:35
【问题描述】:

我需要计算电报中的消息数或获取消息历史记录。 告诉我,有可能做到这一点吗? 我知道,我可以在电报桌面上看到某个成员的聊天消息计数。也许我可以在任何对话中做到这一点? 谢谢!

【问题讨论】:

  • 每个频道都有自己的自增postNo。您可以通过最后一条消息的 postNo 找到频道中的消息数。例如你可以通过这个链接看到“电报新闻”频道的第21条消息:t.me/telegram/21
  • @tashakori 也许你知道这个 api 方法是如何工作的? core.telegram.org/method/messages.getHistory
  • 是的,但它在不同的客户端或 API 中的使用可能会有所延迟。所有人都知道的共同点是,您应该拥有 channel_id(每个频道唯一)和 access_hash(每个用户-频道对唯一)。你可以通过使用它的参数来调用这个方法,比如:max id, offset date, limit, ...。在此链接中查看 getHistory#afa92846 以获得完整签名:tjhorner.com/tl-schema

标签: api telegram


【解决方案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

附:这是从官方 wiki 获取一些聊天的简单(用户)方式,但从我的角度来看,它非常有限且不适合编程目的:

https://telegram.wiki/general/exporting-chats

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • @E.Moffat 很公平。相应更新。
猜你喜欢
  • 2015-10-25
  • 1970-01-01
  • 2016-12-13
  • 2014-01-05
  • 2014-04-25
  • 2022-01-25
  • 2015-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多