Skip to content

Instantly share code, notes, and snippets.

@stek29
Last active April 17, 2025 21:28
Show Gist options
  • Save stek29/8a7ac0e673818917525ec4031d77a713 to your computer and use it in GitHub Desktop.
Save stek29/8a7ac0e673818917525ec4031d77a713 to your computer and use it in GitHub Desktop.
Extract Telegram messages from db_sqlite PostBox – made for Telegram for macOS, but should work with Telegram for iOS
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@lindsaymorgan
Copy link

@lindsaymorgan I think @ekzotech wrote about it, try this@lindsaymorgan我认为@ekzotech写过它,试试这个

Also there's an error with calling murmur because of mmh3 update 5.0.0 - it fails with negative seed. As workaround you can install mmh3==4.1.0 worked for me.此外,由于 mmh3 更新 5.0.0 而调用 murmur 时出错 - 它失败并出现负种子。作为解决方法,您可以安装 mmh3==4.1.0 对我有用。

It works! Thank you so much!

@elezar42
Copy link

This has been working great for me for awhile. But recently, I've been getting an exception on this line in the TelegramMediaFile decoder:

self.fileId = (bio.read_int32(), bio.read_int64())

The call to read_int32() is failing because when it reads the buffer, it's getting b''. This isn't happening for all Media Files, but it is happening for a lot of them. I'm assuming it's some new feature in Telegram that's causing this, but I thought I would ask here if anyone knows what the change is, before spending time trying to dig into what's causing it.

@TrikzMe
Copy link

TrikzMe commented Mar 20, 2025

Thank you very much for your work, it's so helpful!

I was able to get the FORWARDED messages with my partial code:

if msg.get('fwd'):
    fwd_info = msg['fwd']
    if isinstance(fwd_info, dict):
        author_id = fwd_info.get('author')
        date = fwd_info.get('date')
        signature = fwd_info.get('signature')
        fwd_parts = []
        if author_id:
            fwd_parts.append(f"author: {peer_str(author_id)}")
        if date:
            fwd_parts.append(f"date: {datetime.datetime.fromtimestamp(date).strftime('%Y-%m-%d %H:%M:%S')}")
        if signature:
            fwd_parts.append(f"signature: {signature}")
        
        fwd_str = " | ".join(fwd_parts)
        full_content = f"[FORWARDED {fwd_str}]{full_content}[/FORWARDED]"

But I am unable to handle REPLY messages.
Ideally I would like to output in this format:
"[REPLY from user name or phone]the original message[/REPLY]additionnal message if exists"

Do you think you can help about this?

I noticed raw mgs data contains some things like:
{'i': 123456789101112, 'p': 654321, '@type': 478003709} from "attributes"
It looks to tells it's a reply message (probably from it's "@type" value), I can say the current message is from the peer_id "654321" but the quoted reply is NOT from peer_id "654321".
I can't get what this "i" with the value "123456789101112" represents and how to get the message referenced by "123456789101112".
I'm maybe in the wrong way with the mgs attributes, don't know.

I tried few things already based on SyncCore_ReplyMessageAttribute.swift for example but I don't know why I can't get the data.

Thanks again for your time!

@StarTechx99
Copy link

StarTechx99 commented Mar 21, 2025

I assume this needs to be ran on the host machine? I've tried exporting the account postbox directory to another machine and running the jup notebook but doesn't seem to be working. Does the notebook file need to be in the same directory with the db_sqlite? Once I run the jup notebook server and open this ipynb I just get a 0 byte DB and an error saying no such table T7

Just looking for initial setup guidance.

@TrikzMe
Copy link

TrikzMe commented Mar 21, 2025

...

Just looking for initial setup guidance.

Hi, You better install Python (v3.10x or later), copy the code parts within a .PY file, edit this line "con = sqlite3.connect('plaintext.db')" to match the path of the db_sqlite file and execute the .PY with Python.

@pauloneves
Copy link

pauloneves commented Apr 8, 2025

How to get the Sqlite DB file from an Android phone or Windows desktop computer? I searched a lot, but can't find them.

I'm stuck in the fist step of the problem 🤷

@stek29
Copy link
Author

stek29 commented Apr 8, 2025

@pauloneves this is for iOS/macOS apps only, other apps are completely different

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment