Skip to content

Instantly share code, notes, and snippets.

@X-Gorn
Last active May 29, 2024 08:09
Show Gist options
  • Save X-Gorn/8e2f7e0b86e0919d2c0ff916459c4e7c to your computer and use it in GitHub Desktop.
Save X-Gorn/8e2f7e0b86e0919d2c0ff916459c4e7c to your computer and use it in GitHub Desktop.
Pyrogram Delete All Messages in a Chat with Double Clients.
import asyncio
from pyrogram import Client
from pyrogram.errors import FloodWait
chat_id = -100123456789
api_hash = 'abc'
api_id = 123
string_session1 = ''
string_session2 = ''
clients = {}
clients['account1'] = Client(name='session1', api_id=api_id, api_hash=api_hash, session_string=string_session1)
clients['account2'] = Client(name='session2', api_id=api_id, api_hash=api_hash, session_string=string_session2)
ids = []
offset_id = 0
async def check_and_delete(ids):
if len(ids) >= 100:
try:
await clients['account1'].delete_messages(chat_id, list(set(ids)))
ids = []
except FloodWait:
try:
await clients['account2'].delete_messages(chat_id, list(set(ids)))
ids = []
except FloodWait as flood:
await asyncio.sleep(flood.value)
await clients['account2'].delete_messages(chat_id, list(set(ids)))
ids = []
async def main():
await clients['account1'].start()
await clients['account2'].start()
while True:
try:
async for message in clients['account1'].get_chat_history(chat_id, offset_id=offset_id):
ids.append(message.id)
offset_id = message.id
await check_and_delete(ids)
if ids: await clients['account2'].delete_messages(chat_id, list(set(ids)))
break
except FloodWait:
try:
async for message in clients['account2'].get_chat_history(chat_id, offset_id=offset_id):
ids.append(message.id)
offset_id = message.id
await check_and_delete(ids)
if ids: await clients['account2'].delete_messages(chat_id, list(set(ids)))
break
except FloodWait as error:
await asyncio.sleep(error.value)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment