filippz / telegram_import

Import back messages exported from Telegram into JSON format
14 stars 2 forks source link

KeyError: 'messages' #7

Open Lucasquattro opened 1 month ago

Lucasquattro commented 1 month ago

Hi there, I tried to run the script but it seems not correcly recognizing the json content for some reason.

Is previously ran into this error and commented out the line 176 as suggested in other discussion:

Traceback (most recent call last):
  File "/Users/luca/Downloads/arbkp/import-master/exp/telegram_import.py", line 240, in <module>
    import_history(
  File "/Users/luca/Downloads/arbkp/import-master/exp/telegram_import.py", line 176, in import_history
    assert data["type"] == "personal_chat"
           ~~~~^^^^^^^^
KeyError: 'type'

But now I'm stucked here, any ideas? Thanks!

Traceback (most recent call last):
  File "/Users/luca/Downloads/arbkp/import-master/exp/telegram_import.py", line 240, in <module>
    import_history(
  File "/Users/luca/Downloads/arbkp/import-master/exp/telegram_import.py", line 179, in import_history
    messages, file_list = convert_to_whatsapp_format(data, only_first_n_messages)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/luca/Downloads/arbkp/import-master/exp/telegram_import.py", line 22, in convert_to_whatsapp_format
    df = pandas.DataFrame.from_dict(data["messages"]).sort_values(by=["date"])
                                    ~~~~^^^^^^^^^^^^
KeyError: 'messages'

I'm on macos, python3 on last version, all requirements satisfied and below the example json I'm using:

{
    "about": "Here is the data you requested. Remember: Telegram is ad free, it doesn't use your data for ad targeting and doesn't sell it to others. Telegram only keeps the information it needs to function as a secure and feature-rich cloud service.\n\nCheck out Settings > Privacy & Security on Telegram's mobile apps for the relevant settings.",
    "chats": {
        "about": "This page lists all chats from this export.",
        "list": [
            {
                "name": "testuser",
                "type": "personal_chat",
                "id": 1111111111,
                "messages": [
                    {
                        "id": 740140,
                        "type": "message",
                        "date": "2024-02-06T14:13:29",
                        "date_unixtime": "1707225209",
                        "from": "testuser",
                        "from_id": "user1111111111",
                        "text": "test test",
                        "text_entities": [
                            {
                                "type": "plain",
                                "text": "test test"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}
filippz commented 1 month ago

I've exported one of my chats and here (Windows client) JSON is a bit different and it looks something like this:

{
 "name": "XYZ",
 "type": "personal_chat",
 "id": 1234567890,
 "messages": [
 ...

Can you simply delete everything before "list": [ and the end parentheses for it and retest? On Windows I export the chat from the dotted menu and I get only one chat exported - while your structure suggest that you could have more than one chat exported in JSON file. image

Lucasquattro commented 1 month ago

Thanks, the solved the issue!

Unfortunately I can't find a way to export single chat in json format on macos, it'll do it only in html format and so I used massive chat export from setting and cleared unnecessary data.

I got one more question now: could the import be chronologically integrated with existing messages or the only way is to have a bunch of messages at the end of an existing chat with all these markers on the time?

IMG_2754

Thanks again!

filippz commented 1 month ago

Thanks, the solved the issue!

Unfortunately I can't find a way to export single chat in json format on macos, it'll do it only in html format and so I used massive chat export from setting and cleared unnecessary data.

I'm not sure why is that - perhaps the same functionality might be implemented on other clients at some point in the future. For now I guess it's not that much of a problem to "clean" the JSON out as described.

I got one more question now: could the import be chronologically integrated with existing messages or the only way is to have a bunch of messages at the end of an existing chat with all these markers on the time?

This is up to Telegram servers/API and (https://telegram.org/blog/move-history): Messages will be imported into the current day but will also include their original timestamps.

Lucasquattro commented 1 month ago

I'm not sure why is that - perhaps the same functionality might be implemented on other clients at some point in the future. For now I guess it's not that much of a problem to "clean" the JSON out as described.

idk neither, anyway with a bit of cleanup it works!

Lucasquattro commented 1 month ago

This is up to Telegram servers/API and (https://telegram.org/blog/move-history): Messages will be imported into the current day but will also include their original timestamps.

well that's unfortunate, hope they rethink about this limitation for the future!