coding

Godammit Telegram!

Written in January 31, 2021 - 🕒 2 min. read

Well isn’t that something? Less than 7 days after failing to restore my messages backup on Whatsapp, Telegram announced that you can now import messages from other chat apps. Like, godammit Telegram, how can it be so awesome? Since this last update, you can import your message history from WhatsApp, Line, and KakaoTalk, to do that you need to export your chat from, let’s say Whatsapp, and share it to your equivalent contact on Telegram.

Importing to Telegram

Telegram said they will eventually make an API available, so you can build your own scripts and import messages from any chat app. Awesome. But do we really need to wait? Uhmn not really. Let’s take a look at how the exported .txt file from Whatsapp looks like:

8/13/10, 12:11 - Scott Pilgrim: What is the website for Amazon.ca?
8/13/10, 12:12 - Wallace Wells: Amazon.ca...

So let’s say that like me, you only have access to your Whatsapp msgstore.db file, then you can’t export your messages from the Whatsapp app, right? In this case, you can use the Whatsapp Viewer tool to export your chat message as a JSON file that looks like this:

{
    "key": "[email protected]",
    "contactName": "Wallace Wells",
    "messages": [
        {
            "timestamp": "2010-08-13T12:11:00Z",
            "fromMe": true,
            "type": "text",
            "text": "What is the website for Amazon.ca?"
        },
        {
            "timestamp": "2010-08-13T12:12:00Z",
            "fromMe": false,
            "type": "text",
            "text": "Amazon.ca.."
        }
    ]
}

Now all you have to do is load this JSON file with a simple Node.js (for example) script and save it as a .txt file using that pattern above.

const {
    readFileSync,
    writeFileSync,
} = require('fs');

const yourName = 'Scott Pilgrim'; // TODO put your name here

const convertMessageObjToTxt = (messageObj, sender) => {
    // format "2010-08-13T12:11:00Z"
    const dateObj = new Date(messageObj.timestamp);
    const date = dateObj.toLocaleDateString('en-US');
    const time = dateObj.toLocaleTimeString('en-US', { hour12: false }).slice(0, 5);

    // format is "M/DD/YY, HH:MM - Sender's Name: Message"
    return date + ', ' + time + ' - ' + sender + ': ' + messageObj.text + '\n';
}

if (process.argv[2]) {
    // read the JSON file
    const chatJson = JSON.parse(readFileSync(process.argv[2], 'utf8'));
    let txtContent = '';

    chatJson.messages.forEach((message) => {
        txtContent += convertMessageObjToTxt(
            message,
            message.fromMe ? yourName : chatJson.contactName
        );
    });

    // save is as a .txt
    writeFileSync(`${process.argv[2]}.txt`, txtContent);
}

You can run the script with node script.js your-json-file.json and the result will be a .txt file that you can import into Telegram. Awesome, right? The only caveat is that you need to import this .txt via the Telegram mobile app only.

So that’s it! While WhatsApp asks you to give away your privacy for a super shitty app, Telegram continues to be free, open-source (the app, not their servers), and with an awesome API that lets, you do almost anything. Pretty crazy.

Tags:


Post a comment

Comments

No comments yet.