如何在 Telegram 中下载群组的聊天记录? [英] How can I download the chat history of a group in Telegram?

查看:68
本文介绍了如何在 Telegram 中下载群组的聊天记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载在 Telegram 公共群组中发布的聊天记录(所有消息).我怎样才能用 python 做到这一点?

我在 API

然后获取群组的消息历史记录(假设您有群组ID):

chat_id = YOUR_CHAT_IDapi_id=YOUR_API_IDapi_hash = 'YOUR_API_HASH'从 Telethon 导入 TelegramClient从 Telethon.tl.types.input_peer_chat 导入 InputPeerChat客户端 = TelegramClient('session_id', api_id=api_id, api_hash=api_hash)客户端连接()聊天 = InputPeerChat(chat_id)total_count, 消息, 发件人 = client.get_message_history(聊天,限制=10)对于反向(消息)中的 msg:# 格式化消息内容如果 getattr(msg, 'media', None):内容 = '<{}>{}'.format( # 媒体可能有也可能没有标题msg.media.__class__.__name__,getattr(msg.media, '标题', ''))elif hasattr(msg, 'message'):内容 = msg.messageelif hasattr(msg, 'action'):内容 = str(msg.action)别的:# 未知消息,只需打印其类名内容 = msg.__class__.__name__text = '[{}:{}] (ID={}) {}: {} type: {}'.format(msg.date.hour, msg.date.minute, msg.id, "no name",内容)打印(文本)

该示例取自电话示例.

I would like to download the chat history (all messages) that were posted in a public group on Telegram. How can I do this with python?

I've found this method in the API https://core.telegram.org/method/messages.getHistory which I think looks like what I'm trying to do. But how do I actually call it? It seems there's no python examples for the MTproto protocol they use.

I also looked at the Bot API, but it doesn't seem to have a method to download messages.

解决方案

You can use Telethon. Telegram API is fairly complicated and with the telethon, you can start using telegram API in a very short time without any pre-knowledge about the API.

pip install telethon

Then register your app (taken from telethon):

                  

the link is: https://my.telegram.org/

Then to obtain message history of a group (assuming you have the group id):

chat_id = YOUR_CHAT_ID
api_id=YOUR_API_ID
api_hash = 'YOUR_API_HASH'

from telethon import TelegramClient
from telethon.tl.types.input_peer_chat import InputPeerChat

client = TelegramClient('session_id', api_id=api_id, api_hash=api_hash)
client.connect()
chat = InputPeerChat(chat_id)

total_count, messages, senders = client.get_message_history(
                        chat, limit=10)

for msg in reversed(messages):
    # Format the message content
    if getattr(msg, 'media', None):
        content = '<{}> {}'.format(  # The media may or may not have a caption
        msg.media.__class__.__name__,
        getattr(msg.media, 'caption', ''))
    elif hasattr(msg, 'message'):
        content = msg.message
    elif hasattr(msg, 'action'):
        content = str(msg.action)
    else:
        # Unknown message, simply print its class name
        content = msg.__class__.__name__

    text = '[{}:{}] (ID={}) {}: {} type: {}'.format(
            msg.date.hour, msg.date.minute, msg.id, "no name",
            content)
    print (text)

The example is taken and simplified from telethon example.

这篇关于如何在 Telegram 中下载群组的聊天记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆