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.
5
Answers
Now, you can use TDesktop to export chats.
Here is the blog post about Aug 2018 update.
Original Answer:
Telegram MTProto is hard to use to newbies, so I recommend telegram-cli.
You can use third-party tg-export script, but still not easy to newbies too.
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.
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):
The example is taken and simplified from telethon example.
With an update (August 2018) now Telegram Desktop application supports saving chat history very conveniently.
You can store it as json or html formatted.
https://telegram.org/blog/export-and-more
The currently accepted answer is for very old versions of Telethon. With Telethon 1.0, the code can and should be simplified to the following:
Applying any formatting is still possible but
hasattr
is no longer needed.if msg.media
for example would be enough to check if the message has media.A note, if you’re using Jupyter, you need to use
async
directly:You can use the Telethon library. for this you need to register your app and connect your client code to it (look at this).
Then to obtain message history of a entry (such as channel, group or chat):