Giter VIP home page Giter VIP logo

telegram_import's Introduction

telegram_import

Imports the messages exported to JSON format from Telegram back into it. It converts exported JSON to the WhatsApp format to trick the Telegram API to accept the import.

Getting started

Simply clone/download this repository, copy/rename config_template.ini to config.ini and fill the api_id and api_hash with appropriate values (get them here: Telegram Apps).

Dependencies

Dependencies for your python environment are listed in requirements.txt - install them with:

pip install -r requirements.txt

Usage

python3 telegram_import.py --path <path_to_exported_folder> --peer <chat_peer_phone_number> [--test_only]

Usage example

python3 telegram_import.py --path "C:\Users\filip\Downloads\Telegram Desktop\ChatExport_2022-02-05" --peer
"+123456789"

Notes

If your peer deletes the chat history on his side (without selecting "Also delete for..."), you can export the data from your side. Unfortunately, there is no way for your peer to import the exported data back without using something like this.

Imported data is seen on both sides (so you essentially have double messages - the original and imported ones on your side). Imported messages are visibly flagged as such, and have the date/time of import and visible original date/time.

When exporting the chat history you need to export to JSON format, check all the file types with maximum available size. If you skip some file types or filter some of them based on their size the import will fail as the messages refer to the files that are missing from export.

telegram_import's People

Contributors

filippz avatar

Stargazers

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

Watchers

 avatar

Forkers

valaphee pokyah

telegram_import's Issues

KeyError: 'messages'

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"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

Error with attribute 'CheckHistoryImportRequest'

Trying your script, I'm experiencing some issue:

Traceback (most recent call last):
  File "telegram_import.py", line 241, in <module>
    import_history(
  File "telegram_import.py", line 185, in import_history
    (functions.messages.CheckHistoryImportRequest(
AttributeError: module 'telethon.tl.functions.messages' has no attribute 'CheckHistoryImportRequest'

Any hint for solve that?

Module Telethon installed:

Name: Telethon
Version: 1.14.0
Summary: Full-featured Telegram client library for Python 3
Home-page: https://github.com/LonamiWebs/Telethon
Author: Lonami Exo
Author-email: [email protected]
License: MIT
Location: /home/user/.local/lib/python3.8/site-packages
Requires: rsa, pyaes
Required-by:

Thanks!

Import chat to a group

Is there a way to import the chat to a private group? If possible, how is it supposed to be done differently?

I am getting this error using the group link

telethon.errors.rpcbaseerrors.BadRequestError: RPCError 400: IMPORT_PEER_TYPE_INVALID (caused by InitHistoryImportRequest)

Import into group

Is it possible to use this to import into a newly created group? My export consists on chat from an earlier group

Formatting

Do you know if there is a way to import messages with formatting?

I'm particularly interested because it may be a way of getting around the limitation of no replies. The importer could manually add the reply as a blockquote.

I've done some simple tests, without success:

formatting tests

invalid Peer was used

Hi, thanks for your script. Whe I exec it I have this error:

$ python3 telegram_import.py --path ChatExport_2024-08-29 --peer "myphonenumber" --test_only
Loading config.ini
Loading result.json
Converting format
Traceback (most recent call last):
  File "/home/pepus/git/telegram_import/telegram_import.py", line 249, in <module>
    import_history(
  File "/home/pepus/git/telegram_import/telegram_import.py", line 198, in import_history
    client(functions.messages.CheckHistoryImportPeerRequest(
  File "/home/pepus/.local/lib/python3.12/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/pepus/.local/lib/python3.12/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pepus/.local/lib/python3.12/site-packages/telethon/client/users.py", line 87, in _call
    result = await future
             ^^^^^^^^^^^^
telethon.errors.rpcerrorlist.PeerIdInvalidError: An invalid Peer was used. Make sure to pass the right peer type and that the value is valid (for instance, bots cannot start conversations) (caused by CheckHistoryImportPeerRequest)

How can I fix it?. Is peer my Telegram phone number?.

Best regards.

telethon.errors.rpcerrorlist.FloodWaitError: A wait of X seconds is required (caused by CheckChatInviteRequest)

I am trying to import a chat backup to a private group. The backup size is 2.5 GB. I have some kind of error related to flood. This is the first test I've done since yesterday, so I don't quite understand the second wait thing.

By the way, I have to thank you for making this script. I'm trying to recover a 10-year chat history of family conversations, videos, and photos. My wife accidentally left the group and lost all of this.

This is the error:

$ python3 telegram_import.py --path Family-ChatExport_2024-08-29 --peer "joinlink"
Loading config.ini
Loading result.json
Converting format
Traceback (most recent call last):
  File "/home/user/git/telegram_import/telegram_import.py", line 251, in <module>
    import_history(
  File "/home/user/git/telegram_import/telegram_import.py", line 200, in import_history
    client(functions.messages.CheckHistoryImportPeerRequest(
  File "/home/user/.local/lib/python3.12/site-packages/telethon/sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/telethon/client/users.py", line 43, in _call
    await r.resolve(self, utils)
  File "/home/user/.local/lib/python3.12/site-packages/telethon/tl/functions/messages.py", line 218, in resolve
    self.peer = utils.get_input_peer(await client.get_input_entity(self.peer))
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/telethon/client/users.py", line 439, in get_input_entity
    await self._get_entity_from_string(peer))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/telethon/client/users.py", line 543, in _get_entity_from_string
    invite = await self(
             ^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/telethon/client/users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.12/site-packages/telethon/client/users.py", line 87, in _call
    result = await future
             ^^^^^^^^^^^^
telethon.errors.rpcerrorlist.FloodWaitError: A wait of 129 seconds is required (caused by CheckChatInviteRequest)

Best regards.

Unknown import format (caused by InitHistoryImportRequest)

Hi, I'm very unfamiliar with Python (and coding in general) and the most I've been exposed to it is a few days ago when I started learning how to utilize your Repository due to a dire need of importing my old Telegram data. So long story short, I think I was able to run it correctly but since it was going too slow, I left the PC running to continue the uploading status while I was out for work. When I came back, it showed this:

Telegram is having internal issues TimeoutError: Timeout while fetching data (caused by InitHistoryImportRequest)
Upload files mentioned in messages
Uploading (File unavailable, please try again later): 44%|███████████████████████████████████████████████████▍ | 2756/6330 [1:23:55<45:05, 1.32it/s][WinError 2] The system cannot find the file specified: 'C:/Users/trist/Downloads/Telegram Desktop/ChatExport_2024-08-12/\(File unavailable, please try again later)'
Uploading (File unavailable, please try again later): 44%|██████████████████████████████████████████████████▌ | 2756/6330 [1:23:55<1:48:49, 1.83s/it]

Now, being someone who doesn't have any idea what to do, I did what I thought was the most logical and just ran the old syntax I used. Now it showed me this:

C:\Users\trist\Downloads\telegram_import-master\telegram_import-master>python telegram_import.py --path "C:/Users/trist/Downloads/Telegram Desktop/ChatExport_2024-08-12/" --peer "+639173161058"
Loading config.ini
Loading result.json
Converting format
Staring import
Traceback (most recent call last):
File "C:\Users\trist\Downloads\telegram_import-master\telegram_import-master\telegram_import.py", line 249, in
import_history(
File "C:\Users\trist\Downloads\telegram_import-master\telegram_import-master\telegram_import.py", line 209, in import_history
history_import = client(functions.messages.InitHistoryImportRequest(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\trist\AppData\Local\Programs\Python\Python312\Lib\site-packages\telethon\sync.py", line 39, in syncified
return loop.run_until_complete(coro)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\trist\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\trist\AppData\Local\Programs\Python\Python312\Lib\site-packages\telethon\client\users.py", line 30, in call
return await self._call(self._sender, request, ordered=ordered)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\trist\AppData\Local\Programs\Python\Python312\Lib\site-packages\telethon\client\users.py", line 87, in _call
result = await future
^^^^^^^^^^^^
telethon.errors.rpcerrorlist.ImportFormatUnrecognizedError: Unknown import format (caused by InitHistoryImportRequest)

I tried it again and it just asked me to log-in once again. I did that, which started the import process again but the above errors showed up once more. Now I'm completely lost. I don't have the slightest clue on where to begin to fix this. If it's not too much, can I ask for some help regarding this? Thank you!

how to use only import part?

Hello,
since my backup is in html, i found another program which can convert directly into txt format (whastapp format)

so is there anyway, i can use your code only for import purpose?

i mean..how to strip/comment out txt conversion part and use only import part.

thanks.

KeyError: 'contact_vcard'

Any Help? Newb trying to save a dear person's chat here.

Loading config.ini
Loading result.json
Converting format
Traceback (most recent call last):
File "C:\Users\midga\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc
return self._engine.get_loc(casted_key)
File "pandas_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
File "pandas_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'contact_vcard'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\midga\Desktop\tregret-master\telegram_import.py", line 243, in
import_history(
File "C:\Users\midga\Desktop\tregret-master\telegram_import.py", line 182, in import_history
messages, file_list = convert_to_whatsapp_format(data, only_first_n_messages)
File "C:\Users\midga\Desktop\tregret-master\telegram_import.py", line 37, in convert_to_whatsapp_format
if not pd.isnull(row["contact_vcard"]):
File "C:\Users\midga\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\series.py", line 958, in getitem
return self._get_value(key)
File "C:\Users\midga\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\series.py", line 1069, in _get_value
loc = self.index.get_loc(label)
File "C:\Users\midga\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc
raise KeyError(key) from err
KeyError: 'contact_vcard'

getting error

Hello,
I am trying to use this but always getting this error.

Traceback (most recent call last):
  File "C:\laragon\bin\python\python-3.6.1\lib\site-packages\pandas\core\indexes\base.py", line 2898, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'media_type'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "telegram_import.py", line 241, in <module>
    test_only=args.test_only
  File "telegram_import.py", line 177, in import_history
    messages, file_list = convert_to_whatsapp_format(data, only_first_n_messages)
  File "telegram_import.py", line 44, in convert_to_whatsapp_format
    media_type = row["media_type"]
  File "C:\laragon\bin\python\python-3.6.1\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
    return self._get_value(key)
  File "C:\laragon\bin\python\python-3.6.1\lib\site-packages\pandas\core\series.py", line 990, in _get_value
    loc = self.index.get_loc(label)
  File "C:\laragon\bin\python\python-3.6.1\lib\site-packages\pandas\core\indexes\base.py", line 2900, in get_loc
    raise KeyError(key) from err
KeyError: 'media_type'

can you please help?

thanks.

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.