Code Monkey home page Code Monkey logo

telegram-chat-parser's Introduction

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 message
  • sender: the literal name of the sender
  • sender_id: the unique identifier of the sender
  • reply_to_mesg_id: if the message is a reply, this column will store the id of that message, or -1 otherwise
  • date: date time stamp of the message
  • msg_type: can be one of the following: text, sticker, file, photo, poll, location or link
  • msg_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 media
  • has_mention: it will be 1 if there's a mention in the text, 0 otherwise
  • has_email: it will be 1 if there's a email in the text, 0 otherwise
  • has_phone: it will be 1 if there's a phone contact in the message, 0 otherwise
  • has_hashtag: it will be 1 if there's a hashtag in the text, 0 otherwise
  • is_bot_command: it will be 1 if the message is a bot command, 0 otherwise

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!

telegram-chat-parser's People

Contributors

abhishektyagi404 avatar blzzua avatar keizerzilla avatar partylikeits1983 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

telegram-chat-parser's Issues

h

hh

error line 49

Traceback (most recent call last):
File "parser.py", line 50, in
for message in jdata["messages"]:
KeyError: 'messages'

CSV file format to included a column "is_forwarded"

Hi,

I'm wondering if it is possible to add a column in the output csv file which will reflect on whether a message is forwarded from somewhere within Telegram.

For example, if a messages is forwarded into the chat from somewhere on Telegram, that message will be tagged 1 in that column and if the message is not forwarded but an original message from the user it will be tagged 0.

I've tried adding a few lines to the existing script but I'm not sure the exact attribute that represents "is_forwarded" to use.

KeyError

Hi,

I am having quite a lot of issues getting this script to run.

When invoking the script to run, the following error is returned:

File "d:\Scripts\Python\telegram-chat-parser\telegram-chat-parser.py", line 43, in <module>
    available_chat_names = [chat['name'] for chat in telegram_export['chats']['list'] if ('name' in chat) and chat['name'] != None]
KeyError: 'chats'

Do you have a fix for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.