filippz / telegram_import

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

Import into group #5

Closed kocane closed 1 month ago

kocane commented 5 months ago

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

filippz commented 5 months ago

It should be possible but I haven't tried it myself. Telegram requires InputPeer (--peer command line argument) with which we specify where we want to import messages - so by specifying channel_id instead of user_id the messages should go to the appropriate channel.

kocane commented 4 months ago

@filippz Hmm... I'm a bit unsure what to change. The ID of the group I've created goes in --peer?

Firstly I get this error. I changed personal_chat in the script to private_supergroup, and it went away:

Traceback (most recent call last):
  File "C:\Temp\Telegram\scripts\telegram_import.py", line 240, in <module>
    import_history(
  File "C:\Temp\Telegram\scripts\telegram_import.py", line 176, in import_history
    assert data['type'] == 'personal_chat'
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Then I get this error:

C:\Temp\Telegram\scripts>python telegram_import.py --path C:\Temp\Telegram\import --peer "**ID OF CHANNEL/GROUP**" --test_only
Loading config.ini
Loading result.json
Converting format
Traceback (most recent call last):
  File "C:\Temp\Telegram\scripts\telegram_import.py", line 240, in <module>
    import_history(
  File "C:\Temp\Telegram\scripts\telegram_import.py", line 189, in import_history
    client(functions.messages.CheckHistoryImportPeerRequest(
  File "C:\Python312\Lib\site-packages\telethon\sync.py", line 39, in syncified
    return loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\telethon\client\users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\telethon\client\users.py", line 43, in _call
    await r.resolve(self, utils)
  File "C:\Python312\Lib\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 "C:\Python312\Lib\site-packages\telethon\client\users.py", line 439, in get_input_entity
    await self._get_entity_from_string(peer))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\telethon\client\users.py", line 576, in _get_entity_from_string
    raise ValueError(
ValueError: Cannot find any entity corresponding to "**ID OF CHANNEL/GROUP**"
filippz commented 4 months ago

assert data['type'] == 'personal_chat'

This can be commented out for time being.

ValueError: Cannot find any entity corresponding to "ID OF CHANNEL/GROUP"

I got the same error so I used "join link" of the group for --peer value as stated in Telethon docs instead. Now I'm getting a different error, but latter on: 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 InitHistoryImportRequest)

So this time "Peer" has been found but it refused to import messages to it. I'm now looking at the result of CheckHistoryImportRequest and the group value is false which makes me wonder if this should be true in order to be able to import messages to a group. Maybe we need to figure out how to format messages in convert_to_whatsapp_format so the Telegram recognizes them as being messages exported from group chat.

LYWalker commented 2 months ago

Adding this to the WhatsApp export should make it register as a group chat:

05/01/2022, 10:47 pm - Messages and calls are end-to-end encrypted. No one outside of this chat, not even WhatsApp, can read or listen to them. Tap to learn more. 05/01/2022, 10:47 pm - XXXXX created group "Group name here"

LYWalker commented 2 months ago

I believe that error may still occur. I have successfully imported into a group chat by making sure it's a supergroup.

A new supergroup can be created like so:

...

client = TelegramClient('session_name', api_id, api_hash)
client.start()
result = client(functions.channels.CreateChannelRequest(
        title='Test supergroup',
        about='some string here',
        megagroup=True, #create as supergroup
        for_import=True, #expecting to import from 3rd party apps
    ))
print(result)

Though I believe there are ways to do it in app, too.

filippz commented 2 months ago

Thanks for taking a look at this - the created group message helps and with (for example) FM created group "Test" I'm getting HistoryImportParsed with group=True. Still HistoryImportParsed has pm: False which I'm guessing is used to indicate private/public groups and with pm=False I can import messages to public groups but not private ones. The line Messages and calls are end-to-end encrypted doesn't help so I'm still looking the magic line for Telegram to return pm=True.

LYWalker commented 2 months ago

From the limited Telegram documentation:

The returned constructor contains information about the exported chat, including its title or type. If the pm flag is set, the chat export file was generated from a private chat. If the group flag is set, the chat export file was generated from a group chat. If neither the pm or group flags are set, the specified chat export was generated from a chat of unknown type.

Therefore they should never both be true, and only both false if Telegram doesn't recognise the format.

From my experimentation, to set the pm flag to true the file needs to contain two users. For example:

12/05/2024, 9:45 pm - Messages and calls are end-to-end encrypted. No one outside of this chat, not even WhatsApp, can read or listen to them. Tap to learn more. 22/05/2024, 2:18 pm - User A: This is a message 28/05/2024, 12:00 pm - User B: This is a reply 28/05/2024, 12:25 pm - User B: Awesome 28/05/2024, 12:28 pm - User B: What's up?

LYWalker commented 2 months ago

Regarding imports into private groups:

Typically, history imports are allowed for private chats with a mutual contact or supergroups with change_info administrator rights. [Source: Telegram Docs]

So the group needs to be a supergroup. I've successfully imported into such private groups.

filippz commented 2 months ago

As a recap:

I have made changes to fix this issues in a new branch if you are willing to test them: https://github.com/filippz/telegram_import/tree/group_import

LYWalker commented 2 months ago

A super group need not be public, it's called a super group because it's essentially implemented as a channel