to ask for help with python and/or executing this script, join discord: https://discord.gg/zpu7YUP3Um
There's not telegram API method for this, we need to call MTProto methods to retrieve messages from the "Recent Actions" (Admin Log) since deleted messages (and medias) gets moved there for 48 hours before the permanent deletion.
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel, PeerChannel
from telethon.tl.types import Channel
import time
# Get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
# or from https://tjhorner.dev/webogram/#/login
api_id = API_ID
api_hash = API_HASH
client = TelegramClient('session_name', api_id, api_hash)
client.start()
group = client.get_entity(PeerChannel(GROUP_CHAT_ID))
#messages = client.get_admin_log(group)
file1 = open("dump.json","w")
c = 0
m = 0
for event in client.iter_admin_log(group):
if event.deleted_message:
print("Dumping message",c, "(", event.old.id, event.old.date,")")
file1.write(event.old.to_json() + ",")
c+=1
if event.old.media:
m+=1
#print(event.old.media.to_dict()['Document']['id'])
client.download_media(event.old.media, str(event.old.id))
print(" Dumped media", m)
time.sleep(0.1)
How do I run this?
Please check https://docs.python.org/3/faq/
I tried to do like you wrote, but it shows this
how to solve this?
Traceback (most recent call last):
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\sessions\memory.py", line 194, in get_input_entity
return utils.get_input_peer(key)
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\utils.py", line 235, in get_input_peer
_raise_cast_fail(entity, 'InputPeer')
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\utils.py", line 138, in _raise_cast_fail
raise TypeError('Cannot cast {} to any kind of {}.'.format(
TypeError: Cannot cast PeerChannel to any kind of InputPeer.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:\ackup_will_be_inside_m\backup_script.py", line 15, in
group = client.get_entity(PeerChannel('-1001654540914'))
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\sync.py", line 39, in syncified
return loop.run_until_complete(coro)
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
return future.result()
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 292, in get_entity
inputs.append(await self.get_input_entity(x))
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 430, in get_input_entity
return self.session.get_input_entity(peer)
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\sessions\memory.py", line 198, in get_input_entity
key = utils.get_peer_id(key)
File "C:\Users\Arthu\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\utils.py", line 1038, in get_peer_id
if not (0 < peer.channel_id <= 9999999999):
TypeError: '<' not supported between instances of 'int' and 'str'
PS H:\ackup_will_be_inside_m>