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:
you need to have python on your machine https://docs.python.org/3/faq/
create folder
mkdir backup_will_be_inside_me
create file
touch backup_script.py
copy the code below and put it inside backup_script.py file
open your terminal and run
pip install telethon
Get your own api_id and api_hash and group_chat_id
from https://my.telegram.org, under API Development.
or from https://tjhorner.dev/webogram/#/login
Get your GROUP_CHAT_ID from https://tjhorner.dev/webogram/#/login
when click on the chat
and put them inside the variables down
python backup_script.py
and wait ...... :) need sometime
inside the folder backup_will_be_inside_me you will see all of what you lost
"""
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'
Get your group_chat_id from https://tjhorner.dev/webogram/#/login
when click on the chat
group_chat_id=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)