← notes

MTProto is not the Bot API, and the session file is a credential

May 30, 2024

Export your Telegram chat titles to CSV so they can go into a spreadsheet.

Small script, and worth a note because it sits on the client protocol rather than the Bot API, which people conflate constantly. The Bot API is HTTP, simple, and can only see what is sent to the bot, it cannot enumerate your chats, because they are not its chats. Listing your own dialogs means acting as you, which means MTProto through Telethon, an interactive login with a code, and a persisted .session file.

That session file is credential-equivalent. Anyone holding it has your Telegram account, not a scoped token, and it does not look like a secret because there is no visible key in it. It belongs in .gitignore before the first run.

iter_dialogs pages server-side, and the error you meet is FloodWaitError, which tells you how long to wait. The correct response is to wait that long. Chats, channels and supergroups are different entity types, and any of their titles can be non-Latin, which makes CSV encoding a real consideration.

CSV is the right target, because a list of chat titles is something you want to sort and annotate by hand.