I am not able to save chat log with Telegram. Telegram on Android, Mac OS X, Windows and Linux do not give this option to user, as far as I know. This is a basic feature and it is disappointing and surprising that this software does not have it.

There is a request for this feature here.

Maybe there is a workaround because all history is loaded on the device when you scroll back.

Is it possible to capture/save chat history loaded in the device, make a local backup copy of chat log?


Official Data Export

From 23-24/06/2018 Telegram added this option, as part of the GDPR changes.

Now you can request to download all you data, and so you can get your chat logs.

  • Telegram support for Data Export:
    • Telegram Desktop - from 1.3.8 or higher
    • Telegram for Android - from 4.8.10 or higher
    • Telegram for iOS - from 4.8.3 or higher
    • Telegram Webapp - not available yet
    • Telegram for Windows Phone - no idea

You can message the @GDPRbot to request your data export, or from the app settings if already available.

Note: For security reasons, the data export is not immediate, so in case someone take control over one of your devices they won't be able to immediately download all your data.

Read the full notice from Telegram's FAQ export

Demonstration from Telegram Desktop

Other options (from GitHub)

  • Telegram History Dump
  • Telegram Export

tg (telegram-cli) accepts Lua scripts. Here is my script which saves as many messages as you want into a sqlite database.

https://github.com/psamim/telegram-cli-backup


I created my own script based on @Samim's in a gist.

It prints to a file so that you can save it as you like, rather than using a SQL database. Also, it has a sleep feature so that you can a larger number of dialogs' histories. It is still limited by telegram-cli's hard limit of 100. I changed that by editing the source and raising the limit.


Answer :

It's not possible to backup your chat history in an automated way with the official application, this feature is not provided yet

Workaround :

You can still make it manually; tested on official client on windows

  • Open a chat window
  • Click on the last Sent/Received message and maintain you click
  • Move the mouse up to select all the messages
  • Right click to copy the messages
  • Save them into a text file or else

I found pretty php library that works over telegram-cli https://github.com/zyberspace/php-telegram-cli-client

There is script that will download all messages and files.

<?php
require('vendor/autoload.php');
$telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck');

$chat = 'chat_name_that_you_want_to_download';

$limit = 50;
$offset = 0;

function save($msg)
{
        $path = '/path/where/you/want/to/store/messages';
        file_put_contents($path . '/' . $msg->id, json_encode($msg));
}

function download($telegram, $msg)
{
        $response = $telegram->exec('load_' . $msg->media->type, $msg->id);
        $msg->media->path = $response->result;
}

while($msgList = $telegram->getHistory($chat, $limit, $offset)) {
        $offset += $limit;
        foreach($msgList as $msg) {
                if (isset($msg->media)) {
                        download($telegram, $msg, $msg->media->type);
                }
                save($msg);
        }
}

You have to change two strings here:

  1. chat_name_that_you_want_to_download
  2. /path/where/you/want/to/store/messages

    • Script will save every message (from chat "chat_name_that_you_want_to_download") as separated file in folder "/path/where/you/want/to/store/messages".
    • Every file contains json representation of message.
    • For files there will be "media->path" with path to downloaded file.
    • Files will be stored at telegram-cli default folder: ~/telegram-cli/downloads