-
-
Save stek29/8a7ac0e673818917525ec4031d77a713 to your computer and use it in GitHub Desktop.
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
IOS not have .tempkeyEncrypted file, is .tempkey , and is 48 bytes not 64 bytes, code maybe not work. : (
message "ValueError: 33 is not a valid TelegramMediaAction.Type". It fails on some enum. How can I fix that?
Newer action types can be found in sources here: https://github.com/TelegramMessenger/Telegram-iOS/blob/45fa1b5ddb3a9a794bf407cb8d9bf2d0e4643b1f/submodules/TelegramCore/Sources/SyncCore/SyncCore_TelegramMediaAction.swift#L136-L262
Other enums:
- MessageTags: https://github.com/TelegramMessenger/Telegram-iOS/blob/45fa1b5ddb3a9a794bf407cb8d9bf2d0e4643b1f/submodules/TelegramCore/Sources/SyncCore/SyncCore_Namespaces.swift#L156-L171
- MessageFlags https://github.com/TelegramMessenger/Telegram-iOS/blob/45fa1b5ddb3a9a794bf407cb8d9bf2d0e4643b1f/submodules/Postbox/Sources/Message.swift#L858-L870
IOS not have
.tempkeyEncryptedfile, 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 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.
@lindsaymorgan or see hajimes/mmh3#119
@lindsaymorgan I’ve updated the gist code, hope I didn’t mess it up
@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!
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.
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.
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 🤷
@pauloneves this is for iOS/macOS apps only, other apps are completely different
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?
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.
@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?
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
It did the job for me to recover some accidentally deleted messages. Thank you!
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.
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.
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.
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
I cant find the .tempkeyEncrypted file inside my Telegram app folder:
don't know if i use the "correct" app.
Hi @carmolim,
I believe the App Store version stores its data in a different location.
For reference, I tested this using the Homebrew version of Telegram:
❯ brew list | grep telegram
telegramUpdate
I have updated the README to make the setup and usage instructions clearer.
Hope that helps.
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 😭
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
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
@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.
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.
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-diroption. - Added automatic detection of the sibling Telegram
postbox/mediadirectory when possible. - Added media rendering in Markdown, HTML, and CSV outputs.
- Preserved the existing
--me-nameoption for outgoing speaker labels. - Added a
--debugoption 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
--contactlookup 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.pyruff check src/telegram_message_exporter tests telegram_exporter.py .github/scripts/bump_version.pypylint src/telegram_message_exporter telegram_exporter.pypython -m pytest -q, currently14 passedpython 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-namestill labels outgoing messages the way you expect.--contactand--peer-idboth work for your Postbox database.
If you are happy with the branch, I will merge the PR.



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?