Skip to content

Instantly share code, notes, and snippets.

@stek29
Last active June 6, 2026 14:02
Show Gist options
  • Select an option

  • Save stek29/8a7ac0e673818917525ec4031d77a713 to your computer and use it in GitHub Desktop.

Select an option

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.
@farzadmf

Copy link
Copy Markdown

Does anyone know by any chance if there's a way to know if the auto-delete feature is enabled in a chat/conversation or not?

@ekzotech

Copy link
Copy Markdown

Woah, thanks for such great job! I tried another script and I can get list of peers for chats where are multiple users. Can I access personal chats too?
Anyway with this script I have an error when trying to find any message: long debug output and message "ValueError: 33 is not a valid TelegramMediaAction.Type". It fails on some enum. How can I fix that?
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.
But I don't know what to do with the error above

@badguy123

Copy link
Copy Markdown

IOS not have .tempkeyEncrypted file, is .tempkey , and is 48 bytes not 64 bytes, code maybe not work. : (

@stek29

stek29 commented Jan 10, 2025

Copy link
Copy Markdown
Author

IOS not have .tempkeyEncrypted file, is .tempkey , and is 48 bytes not 64 bytes, code maybe not work. : (

@badguy123 then it should just be the key and salt in unencrypted form, try that

dbKey = data[0:32]
dbSalt = data[32:48]

@lindsaymorgan

Copy link
Copy Markdown

I am trying to use this script and get the following error:
image

Any suggestion to solve it? Or maybe this is a version issue? which version of mmh3 should I use?

@stek29

stek29 commented Jan 26, 2025

Copy link
Copy Markdown
Author

@lindsaymorgan I think @ekzotech wrote about it, try this

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.

@stek29

stek29 commented Jan 26, 2025

Copy link
Copy Markdown
Author

@stek29

stek29 commented Jan 26, 2025

Copy link
Copy Markdown
Author

@lindsaymorgan I’ve updated the gist code, hope I didn’t mess it up

@lindsaymorgan

Copy link
Copy Markdown

@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
Copy Markdown

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.

@StarTechx99

StarTechx99 commented Mar 21, 2025

Copy link
Copy Markdown

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.

@pauloneves

pauloneves commented Apr 8, 2025

Copy link
Copy Markdown

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

stek29 commented Apr 8, 2025

Copy link
Copy Markdown
Author

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

@ianwongkj

Copy link
Copy Markdown

Hey, thanks so much for this. I'm trying to extract the key to decrypt db - I don't have a local passcode, only the cloud one, and I'm getting raise Exception(f'hash mismatch: {dbHash} != {calcHash}'). Any ideas on what else I could try?

@aromanyuk

Copy link
Copy Markdown

Hey, thanks so much for this. I'm trying to extract the key to decrypt db - I don't have a local passcode, only the cloud one, and I'm getting raise Exception(f'hash mismatch: {dbHash} != {calcHash}'). Any ideas on what else I could try?

The same issue for me. No password, but still hash mismatch.

@stek29

stek29 commented Jul 11, 2025

Copy link
Copy Markdown
Author

@ianwongkj @aromanyuk works fine for me on 11.13.273693 Stable. Maybe you're trying to pass cloud password instead of default no-matter-key?

@pfurovYnP

pfurovYnP commented Sep 19, 2025

Copy link
Copy Markdown

Big thanks for the working script! was able to retrieve all the messages on Mac. Please direct me how do I acquire the dump from IOS? only found belkasoft which looks difficult to get

@soar

soar commented Dec 2, 2025

Copy link
Copy Markdown

It did the job for me to recover some accidentally deleted messages. Thank you!

@soakes

soakes commented Feb 4, 2026

Copy link
Copy Markdown

Hey @stek29,

Thank you for the original research and examples — it saved me a headache today. A family member accidentally removed a chat, but we managed to keep a Mac offline long enough to recover the local cache. Your work made it possible to build a clean, user‑friendly tool that exports Telegram (macOS) chats to HTML, Markdown, or CSV.

I’ve published it here to help others in the same situation:
https://github.com/soakes/telegram-message-exporter

You’re credited in the README. If you see anything worth improving, feel free to open an issue or PR.

Thanks again — this was hugely helpful.

@stek29

stek29 commented Feb 5, 2026

Copy link
Copy Markdown
Author

Hey @soakes, that's amazing, thank you for sharing!

Just in case I'd like to clarify that I absolutely do not mind since this gist didn't state any license.

A minor suggestion is not to call the app "Telegram Desktop" since Telegram Desktop is the name of the cross-platform Qt app (named Telegram Lite in App Store for some reason) to avoid confusion — the native Telegram macOS app is different from Telegram Desktop.

@soakes

soakes commented Feb 5, 2026

Copy link
Copy Markdown

Hey @soakes, that's amazing, thank you for sharing!

Just in case I'd like to clarify that I absolutely do not mind since this gist didn't state any license.

A minor suggestion is not to call the app "Telegram Desktop" since Telegram Desktop is the name of the cross-platform Qt app (named Telegram Lite in App Store for some reason) to avoid confusion — the native Telegram macOS app is different from Telegram Desktop.

Hi @stek29

Great catch!

Will rename it later today.

Any ideas what we shell call it?

In brew its just called telegram, in the app its just called the same.

image image image

Open to suggestions for app name and repo name.

I will also adjust the documentation to make sure its clear which version its for.

Update
I have just made some adjustments, see what you think.
https://github.com/soakes/telegram-message-exporter/pull/4/changes

@carmolim

Copy link
Copy Markdown

I cant find the .tempkeyEncrypted file inside my Telegram app folder:
image
don't know if i use the "correct" app.

@soakes

soakes commented Apr 30, 2026

Copy link
Copy Markdown

I cant find the .tempkeyEncrypted file inside my Telegram app folder: image don't know if i use the "correct" app.

Hi @carmolim,

I believe the App Store version stores its data in a different location.

image

For reference, I tested this using the Homebrew version of Telegram:

❯ brew list | grep telegram
telegram

Update

I have updated the README to make the setup and usage instructions clearer.

Hope that helps.

@carmolim

carmolim commented May 1, 2026

Copy link
Copy Markdown

I found it, I opened it on vscode and by mistake hit save CTRL + S on the wrong tab and messed up with the .tempkeyEncrypted file 😭

@imoyakin

imoyakin commented May 15, 2026

Copy link
Copy Markdown

I cant find the .tempkeyEncrypted file inside my Telegram app folder

cp ~/Library/'Group Containers'/6N38VWS5BX.ru.keepcoder.Telegram/appstore/.tempkeyEncrypted anywhere where you want to put the file

@soakes

soakes commented May 15, 2026

Copy link
Copy Markdown

I cant find the .tempkeyEncrypted file inside my Telegram app folder

cp ~/Library/'Group Containers'/6N38VWS5BX.ru.keepcoder.Telegram/appstore/.tempkeyEncrypted anywhere where you want to put the file

If you don't see it here then your using a different version:

 ❯ TELEGRAM_STABLE="$HOME/Library/Group Containers/6N38VWS5BX.ru.keepcoder.Telegram/stable"
ls -la "$TELEGRAM_STABLE"
total 16
drwxr-xr-x 13 soakes staff  416 May 15 06:23 .
drwx------  5 soakes staff  160 Apr  1 10:30 ..
-rw-r--r--  1 soakes staff  602 Mar 31 14:55 .com.apple.containermanagerd.metadata.plist
-rw-r--r--  1 soakes staff   64 Mar 31 14:55 .tempkeyEncrypted
drwxr-xr-x  6 soakes staff  192 Apr  2 17:14 account-5018265511298720455
drwxr-xr-x  6 soakes staff  192 Mar 31 14:55 accounts-metadata
-rw-r--r--  1 soakes staff 2192 Apr 22 09:31 accounts-shared-data
-rw-r--r--  1 soakes staff   21 May 15 06:23 crashhandler
drwx------  6 soakes staff  192 Mar 31 14:55 Library
drwxr-xr-x 22 soakes staff  704 May 15 04:03 logs
drwxr-xr-x  3 soakes staff   96 Mar 31 16:20 temp
drwxr-xr-x 64 soakes staff 2048 May 15 00:56 trlottie-animations
drwxr-xr-x 18 soakes staff  576 Mar 31 14:55 Wallpapers

@stek29

stek29 commented Jun 6, 2026

Copy link
Copy Markdown
Author

@soakes I needed to export some chats and I didn't have enough time to do proper contributions with a PR, I've just forked the project and tasked codex with updating the project for media exports, forwards and support for newer actions and stuff like that, giving it this gist and iOS sources.

I also needed to work on a huge db (~13GB) and to export only some chats, so I've added some optimisations for streaming and filtering in sqlite instead of doing full scans like current references do.

thanks for the project, would be nice if you would be able to incorporate some stuff from my fork into yours.

https://github.com/stek29/telegram-message-exporter

@soakes

soakes commented Jun 6, 2026

Copy link
Copy Markdown

Hey @stek29, thanks for the kind words — and for putting this work together.

The media exports, forwarding support, newer action handling, and SQLite streaming/filtering optimisations all sound very useful, especially for larger databases. I’ll take a look through your fork and see what can reasonably be incorporated back into this project.

Thanks again for sharing it.

@soakes

soakes commented Jun 6, 2026

Copy link
Copy Markdown

Postbox Media and Forward Export Review

Hi @stek29,

Thanks again for the work and research on Postbox media and forwarded-message support. I have put together a PR that brings those ideas into this repository in a reviewable branch.

PR: soakes/telegram-message-exporter#5

Branch: feature/media-forwards-postbox

Rather than merging the fork directly, I selectively reimplemented the useful parts so we could keep the existing CLI behaviour, avoid unrelated README/repo URL changes, and add tests around the new parsing/export paths.

What Changed

  • Added Postbox schema constants for known Telegram media, action, and message-attribute types.
  • Added forwarded-message metadata decoding and export rendering.
  • Added best-effort media decoding for embedded and referenced Postbox media entries.
  • Added cached media export through a new --media-dir option.
  • Added automatic detection of the sibling Telegram postbox/media directory when possible.
  • Added media rendering in Markdown, HTML, and CSV outputs.
  • Preserved the existing --me-name option for outgoing speaker labels.
  • Added a --debug option for SQL key ranges and query plans.
  • Improved Postbox export performance by using peer key-range scans instead of full-table scans for targeted exports.
  • Fixed Postbox --contact lookup so filtered exports can resolve peer names correctly.
  • Kept date filtering after Postbox key decoding so newer or unknown namespaces are not silently skipped.
  • Made unknown media/action payloads best-effort so one unsupported Telegram object does not abort the whole export.

CSV and Render Behaviour

  • Existing CSV columns are preserved and metadata is appended rather than replacing or reordering existing fields.
  • Attachments are exported as JSON metadata in CSV.
  • Forward metadata is exported as JSON metadata in CSV.
  • Markdown and HTML show forwarded-from details and include links or embeds for copied media where possible.

How To Try It

  • Fetch the branch: git fetch origin
  • Check it out: git checkout feature/media-forwards-postbox
  • Install locally: python -m pip install -e .
  • Export with explicit media directory: telegram-exporter export --db /path/to/plaintext/db_sqlite --peer-id 123456789 --format html --out chat.html --media-dir /path/to/account/postbox/media
  • Export with media auto-detection: telegram-exporter export --db /path/to/account/postbox/db/db_sqlite --peer-id 123456789 --format html --out chat.html

If the database path is inside postbox/db/ and the sibling postbox/media/ directory exists, the tool should pick it up automatically.

Verification Already Done

  • black --check src/telegram_message_exporter tests telegram_exporter.py .github/scripts/bump_version.py
  • ruff check src/telegram_message_exporter tests telegram_exporter.py .github/scripts/bump_version.py
  • pylint src/telegram_message_exporter telegram_exporter.py
  • python -m pytest -q, currently 14 passed
  • python telegram_exporter.py --help && python telegram_exporter.py export --help
  • GitHub CI is passing on Python 3.10, 3.11, 3.12, and 3.13.

Review Request

Could you please check out the branch and try it against your Telegram data, especially exports with forwarded messages and cached media?

The main things to confirm are:

  • Forwarded-message metadata looks correct.
  • Photos/files/stickers/audio/video are copied or represented sensibly where cached media exists.
  • Existing text-only exports still behave as before.
  • --me-name still labels outgoing messages the way you expect.
  • --contact and --peer-id both work for your Postbox database.

If you are happy with the branch, I will merge the PR.

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