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/
"""
STEPS TO RECOVER ALL THE MEDIA AND MESSAGES DELETED FROM TELEGRAM CHAT:
[STEP 0]
You need to have python on your machine
https://docs.python.org/3/faq/
you can check if you have python using this command:python --version
[STEP 1]
Create a folder (to save the backup inside it) using the normal way.
NOTE: OR using this command:mkdir backup_will_be_inside_me.
[STEP 2]
Copy and Paste this file inside the folder you created in [STEP 1]
backup_will_be_inside_me
THEN rename the file tobackup_script
THEN open it cause we need to change some lines (it should be with extension.py
)[STEP 3]
Open your terminal and install
telethon
using this command:pip install telethon
[STEP 4]
Get your own
api_id
,api_hash
andgroup_chat_id
and put their values in the code below the sentence THE_CODE_START_AFTER_THIS_LINE:[STEP 5]
Finally, recover the deleted media and messages:
1) Open the termenial inside the folder
backup_will_be_inside_me
(by holdSHIFT
+Right-Clic
k THEN pickOpen Powershell here
).2) Run the code in the file
backup_script.py
by using this command:python backup_script.py
3) Insert your phone number
00XXXXXXXXX
.4) Insert the code you received in the Telegram application.
5) NOW all you need to do is to wait till the process of recovering deleted media and messages is done.
NOTE: The process needs time depending on the size and the number of the media and the messages you will recover (look at the date to know where the process reaches).
THE_CODE_START_AFTER_THIS_LINE
FOR ANY QUESTION [MOHAMMAD JOUZA]
LINKEDIN LINK: https://www.linkedin.com/in/mohammad-jouza/
"""
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel, PeerChannel
from telethon.tl.types import Channel
import time
[STEP 4 - A]
api_id = REPLACE_ME_WITH_YOUR_API_ID
api_hash = 'REPLACE_ME_WITH_YOUR_API_HASH'
[STEP 4 - B]
group_chat_id=REPLACE_ME_WITH_YOUR_GROUP_CHAT_ID
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)