telegram-chat-parser
telegram-chat-parser copied to clipboard
Python script to parse a Telegram chat history backup (JSON) into tabular format (CSV). No extra packages required, only Python 3.x!
telegram-chat-parser
Python script to parse a Telegram chat history backup (JSON) into tabular format (CSV). No extra packages required, only Python 3.x!
How to use it
Using Telegram's Desktop or Web interfaces, go to the chat you want to backup, click on the options button (three dots in the upper right corner) and them click on Export chat history. In the dialog window, right next to Format, chose JSON. After the backup is completed, Telegram will generate a results.json file. This will be the input of our script.
Next, navigate to the directory where the results.json is located and make sure that the script is accessible, probably by placing it next to the results file. Then, run the script by typing:
python3 telegram-chat-parser.py results.json
For each chat backup in results.json, a .csv file will be created in the same directory. The output filename is a stripped down version of the actual chat name (only letters and numbers are kept).
The output format
Once the script is done parsing, the result CSV file will have the format bellow:
msg_id: the unique identifier of the messagesender: the literal name of the sendersender_id: the unique identifier of the senderreply_to_mesg_id: if the message is a reply, this column will store the id of that message, or -1 otherwisedate: date time stamp of the messagemsg_type: can be one of the following:text, sticker, file, photo, poll, location or linkmsg_content: the text content the message, already cleaned in terms of newline and spaces; if the message was not a text (sticker, media, etc) this field will store the path pointing to the mediahas_mention: it will be1if there's a mention in the text,0otherwisehas_email: it will be1if there's a email in the text,0otherwisehas_phone: it will be1if there's a phone contact in the message,0otherwisehas_hashtag: it will be1if there's a hashtag in the text,0otherwiseis_bot_command: it will be1if the message is a bot command,0otherwise
Contributing
I hope this little script helps you in your project! If you have any suggestions or ideas to improve it, please feel free to open an issue. Thank you!