How to upload group chat history to Telegram?

I want to download the chat history (all messages) that were published in the open Telegram group. How can I do this with python?

I found this method in the API https://core.telegram.org/method/messages.getHistory , which, I think, looks the way I'm trying to do. But what do I actually call it? There seems to be no python examples for the MTproto protocol that they use.

I also looked at the Bot API, but it has no way to download messages.

+8
source share
4 answers

You can use Telethon . The Telegram API is quite complex and with telethon you can start using the telegram API in a very short time without any prior knowledge of the API.

pip install telethon 

Then register your application (taken from the telethon): enter image description here

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

Then to get the group message history (assuming you have a 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 the telethon example .

+6
source

Telegram MTProto is hard to use for beginners, so I recommend telegram-cli.

You can use a third - party tg-export script, but still not easy for beginners.

+1
source

you can use telepot ( documentation here ) for python, for example:

 import telepot token = 'your_token' bot = telepot.Bot(token) tmp_history = bot.getUpdates() print(tmp_history['result']) 

but you can run away with a limit of 100 entries in history, read it about it

0
source

With the update (August 2018), the Telegram Desktop application supports saving chat history very conveniently. You can save it in json or html format.

To use this feature, make sure that the latest version of Telegram Desktop is installed on your computer, then click "Settings"> "Export Telegram Data".

https://telegram.org/blog/export-and-more

0
source

Source: https://habr.com/ru/post/1268717/


All Articles