HOWTO backup your Telegram chats (if you don’t fear the terminal)



Note: i’ve updated this article in april 2016 with a new method that’s still clunky, but works a little bit better.

Considering that WhatsApp was sold in February 2014 to Facebook for a petty $19 billion dollars you might have looked around for alternatives. Currently the most promising messaging client is Telegram, an alternative mostly financed by Pavel Durov, who ironically founded Facebook’s biggest competitor in Russia: VK.

Now some people might suggest switching from WhatsApp to Telegram is simply trading one evil (Facebook) for another (VK). However, Durov has nothing to do with VK anymore and the people behind Telegram say they respect your privacy. Telegram is mostly open source as well. Time will tell if Telegram can stay true to their promises.

Anyway, one missing feature (the only one, in my opinion) is the ‘export chat’ option that is available on WhatsApp. I’m not that interested in transferring messages between devices, but i do think it’s important to have an export of your digital history, whether it’s e-mail, instant messaging or your tweets.

There are multiple feature requests for the various (semi) official apps, but currently there doesn’t seem to be a simple solution.

Anyway, i found a hack, but unfortunately it’s pretty technical and not for those afraid of the command line. Unfortunately it’s only for Linux and Mac systems as well.

Basically you’re using a combination of the command line tg command and a Ruby script called telegram-history-dump. Used in conjunction, you get a set of JSON files that you can further process, for example using a Python script that compiles it to a CSV file you can open in Excel. Here’s something i wrote for that.

Here’s what you should do:

  1. Open up a Terminal and follow the instructions to install tg for your operating system. On my MacBook with Yosemite i needed this ugly hack to properly install the brew dependencies. On El Capitan i ran into countless other problems including something where i actually needed to comment out some C code in a source file. Following the instructions in this issue solved it for me.
  2. Now run tg (bin/telegram-cli) and follow the steps to get an activation code.
  3. Follow the instructions to install telegram-history-dump. The default configuration worked fine for me.
  4. Run the script from the command line. This will get you a map of JSONL files that contain your precious messages.
  5. You could try and run my python script to get CSV files for easier viewing.

Alternatively, if you don’t want to run the telegram-history-dump script for some reason you could also try these steps with just tg:

  1. Run the command contact_list to get an overview of all your contacts.
  2. Use the command history to get the chat log. The first argument is the name of your friend (note that tg offers tab completion!), the second one is the number of messages. There doesn’t seem to be a setting for ‘all messages’ so simply pick a high number, e.g.:
    history My_Friends_Name 10000
  3. tg doesn’t offer a way to export the history to a text file, so you either need to copy-paste the stuff from the terminal or save the terminal output to a file (the last option works best for the OS X Terminal).
  4. Note that history also works for groups. There is no groups_list command, but using dialog_list will also show groups.
  5. Repeat for every user and/or group you want to export.

I know, this is all pretty clunky. Hopefully the people at Telegram and/or the coders who create the clients will create a better export option in the future.

Add a comment