Skip to content
Snippets Groups Projects
Commit a981dc2d authored by Recolic's avatar Recolic :house_with_garden:
Browse files

init

parents
No related branches found
No related tags found
Loading
# Script to delete your history message from groups
This script will automatically delete your history message in all joined groups.
It will check the latest `n` messages of each joined group, and delete the message if:
1. It was sent from you
2. The group is not whitelisted
3. The group was sent at least `t` seconds ago
It's recommended to auto-run this script daily to protect your privacy.
这个脚本会自动从所有已加入群组删除你的历史消息.
它会检查每个已加入群组的最近的n条消息, 并且在满足以下条件时删除它:
1. 消息是你发的
2. 这个群组不在白名单里
3. 这个消息是一定时间段前发的
我们建议你每天自动运行一次此脚本, 来保护你的隐私.
## How to setup / deploy this script
Refer to this guide (for a similar script): <https://git.recolic.net/root/telegram-antispam-watchdog> (**Click README_en.md**)
The configuration options might be different but you should be able to figure it out.
## 如何安装和运行
请查阅这个指南(为一个类似的脚本): <https://git.recolic.net/root/telegram-antispam-watchdog>
具体配置项目可能有所不同, 但你应该足够聪明可以理解.
#!/usr/bin/python3 -u
# This script will automatically delete your history message in all joined groups.
# It will check the latest `n` messages of each joined group, and delete the message if:
# 1. It was sent from you
# 2. The group is not whitelisted in (WHITELIST_CHATS)
# 3. The group was sent at least `t` seconds ago
#
# `n` is MSG_DOWNLOAD_LIMIT, `t` is MSG_ALIVE_TIME
# It's recommended to auto-run this script daily.
##################### Configuration Begin ######################
TELEGRAM_API_ID = '11111111' # Get api_id and api_hash at my.telegram.org
TELEGRAM_API_HASH = '67e72cc9e2b603e08d05446ad5ef8e6'
TELEGRAM_PHONE = '+12223334444' # Phone number in International Format. Example: '+8617719890604'
WHITELIST_CHATS = ['-692222222', '-100195111111111']
MSG_DOWNLOAD_LIMIT = 1000 # Set to '0' for dry-run, set to a huge number for first-run.
MSG_ALIVE_TIME = 24*60*60 # 1 day
##################### Configuration End ########################
from telegram.client import Telegram
import time
tg = Telegram(
api_id=TELEGRAM_API_ID,
api_hash=TELEGRAM_API_HASH,
phone=TELEGRAM_PHONE,
database_encryption_key='any_password',
files_directory='tdlib_files/',
)
def result_of(async_result):
async_result.wait()
return async_result.update
def delete_all_msg_from_me(telegram, group_id, receive_limit, my_userid):
receive = True
from_message_id = 0
stats_data = {}
processed_msg_count = 0
current_timestamp = time.time()
while receive:
response = telegram.get_chat_history(
chat_id=group_id,
limit=1000,
from_message_id=from_message_id,
)
response.wait()
msg_to_delete = []
for message in response.update['messages']:
#if message['content']['@type'] == 'messageText':
# print(message['content']['text']['text'])
if message['sender_id']['@type'] != 'messageSenderUser':
# Not sent from user. Ignore it.
from_message_id = message['id']
continue
if message['sender_id']['user_id'] == my_userid and message['date'] < current_timestamp - 24*60*60:
msg_to_delete.append(message['id'])
print("DEBUG: MY message ", message)
else:
from_message_id = message['id']
if msg_to_delete != []:
print("DEBUG: delete msg count=", len(msg_to_delete))
tg.delete_messages(group_id, msg_to_delete)
processed_msg_count += len(response.update['messages'])
if processed_msg_count > receive_limit or not response.update['total_count']:
receive = False
print(f'[{processed_msg_count}/{receive_limit}] processed')
if __name__ == '__main__':
tg.login()
my_id = result_of(tg.get_me())['id']
for chatid in result_of(tg.get_chats())['chat_ids']:
if chatid >= 0:
print(f"Ignore chat_id {chatid}, not a group")
continue
if chatid in WHITELIST_CHATS or str(chatid) in WHITELIST_CHATS:
print(f"Ignore chat_id {chatid}, whitelisted")
continue
group_title = result_of(tg.get_chat(chatid))['title']
print("Will cleaning up chat_id ", chatid, group_title)
delete_all_msg_from_me(tg, str(chatid), MSG_DOWNLOAD_LIMIT, my_id)
tg.stop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment