Giter Site home page Giter Site logo

gurland / telegram-delete-all-messages Goto Github PK

View Code? Open in Web Editor NEW
352.0 9.0 85.0 42 KB

Delete all your messages in groups / supergroups using this python script

License: GNU General Public License v3.0

Python 95.33% Batchfile 4.67%
telegram python-script cli privacy

telegram-delete-all-messages's Introduction

telegram-delete-all-messages

Delete all your messages in supergroups with python script.

Installation

To install this script you have to download project and install requirements:

Linux

git clone https://github.com/gurland/telegram-delete-all-messages
cd telegram-delete-all-messages
pip install -r requirements.txt
python cleaner.py

Windows

  • Download zip file from this repo and unpack it
  • Install latest CPython 3 version
  • Run install.bat
  • Run start.bat

Obtain standalone telegram app API credentials

  • Login to https://my.telegram.org/
  • Select API development tools link
  • Create standalone application
  • Copy app_id and app_hash

Usage

You need both App api_id and App api_hash to use script.

Environment variables

You could set API_ID and API_HASH environment variables to prevent entering API credentials manually.

Start

After starting script you will be prompted:

  • To enter your Telegram APP credentials (if no environment variables found)
  • Your account phone and then code sent to you by Telegram
$ python cleaner.py

Enter your Telegram API id: 123456
Enter your Telegram API hash: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Pyrogram v0.14.1, Copyright (C) 2017-2019 Dan <https://github.com/delivrance>
Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)

Enter phone number: +123456789012
Is "+123456789012" correct? (y/n): y
Enter phone code: 88988
Logged in successfully as Stanislav

Choosing supergroup

  • After providing needed information you will get your supergroup dialogs
  • Enter number found near desired supergroup title
1. Python community
2. Rust Beginners
3. IDE & Editors

Insert group number:

Message removal process

  • After choosing supergroup you would get informed about messages removal process
Insert group number: 2
Selected Rust Beginners

Searching messages. OFFSET: 0
Found 4 your messages in selected supergroup
Deleting 4 messages with next message IDs:
[23807, 23799, 23757, 23756]

Contribution

To make any changes in our codebase, please do the following:

  1. Create or find an Issue describing what needs to be done.
  2. Discuss all changes needed.
  3. Fork repository, clone it, create branch with briefly descriptive name of feature/bufix you are adding, e.g. git checkout -b fix-sleep-treshold.
  4. Create Pull Request. Please, test all changes before creating PR and explicitly declare whether testing was succesful or not.
  5. Wait untill available contributors review changes.
  6. If everything is OK your contribution gets approved.

Note: it's very important to keep PRs brief and clear. Resolve single issue by a single PR.

telegram-delete-all-messages's People

Contributors

2trvl avatar borontov avatar g4st0n avatar gurland avatar hannesveit avatar sinapser0x avatar theatischbein avatar vtronko avatar xanoni avatar zefirka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

telegram-delete-all-messages's Issues

TypeError: get_dialogs() got an unexpected keyword argument 'pinned_only'

Traceback (most recent call last):
File "/Users/admin/Projects/telegram-delete-all-messages/cleaner.py", line 134, in
deleter.select_groups()
File "/Users/admin/Projects/telegram-delete-all-messages/cleaner.py", line 50, in select_groups
chats = self.get_all_chats()
File "/Users/admin/Projects/telegram-delete-all-messages/cleaner.py", line 40, in get_all_chats
dialogs = app.get_dialogs(pinned_only=True)
File "/Users/admin/.pyenv/versions/3.9.12/lib/python3.9/site-packages/pyrogram/sync.py", line 53, in async_to_sync_wrap
coroutine = function(*args, **kwargs)
TypeError: get_dialogs() got an unexpected keyword argument 'pinned_only'

It used to work, now endless loop after introduce phone number

Endless loop after enter the phone number.

[client] Sleeping for 15s (required by "messages.GetDialogs")
[client] Sleeping for 15s (required by "messages.GetDialogs")
[client] Sleeping for 17s (required by "messages.GetDialogs")
[client] Sleeping for 16s (required by "messages.GetDialogs")
[client] Sleeping for 14s (required by "messages.GetDialogs")
[client] Sleeping for 16s (required by "messages.GetDialogs")
[client] Sleeping for 16s (required by "messages.GetDialogs")
[client] Sleeping for 18s (required by "messages.GetDialogs")
[client] Sleeping for 15s (required by "messages.GetDialogs")
[client] Sleeping for 14s (required by "messages.GetDialogs")
[...]

Someone has experienced that or know how to fix it?

TypeError: get_dialogs() got an unexpected keyword argument 'pinned_only'

pip3 show pyrogram

Name: Pyrogram
Version: 2.0.106
Traceback (most recent call last):
  File "/root/telegram-delete-all-messages/cleaner.py", line 154, in <module>
    deleter.select_groups()
  File "/root/telegram-delete-all-messages/cleaner.py", line 67, in select_groups
    chats = self.get_all_chats()
  File "/root/telegram-delete-all-messages/cleaner.py", line 57, in get_all_chats
    dialogs = app.get_dialogs(pinned_only=True)
  File "/usr/local/lib/python3.9/site-packages/pyrogram/sync.py", line 53, in async_to_sync_wrap
    coroutine = function(*args, **kwargs)
TypeError: get_dialogs() got an unexpected keyword argument 'pinned_only'

#53 #62

Not deleting all massages

So I tried to delete messages from a group. But it only removed like first 5000 of them. Manually searching for them in telegram I can see the rest. But script doesn't. It just reports - Found 0 of your messages. Deleting 0 messages with message ID.

Any way to run it deeply or something?

Search is broken

The search is currently broken for both groups and supergroups:

  • For groups, the search finds at most 200 messages in total
  • For supergroups, it finds 10% of the messages (finding 100 and skipping 900 in each iteration)

#26 fixes both issues.

Not all groups are shown

I am able to get the script up and running but it does not seem to list all my groups. Namely, the one group I am an admin/owner of.

That is the group I want to clear out and I'm not sure why it's not showing via the API. Any ideas?

Thanks for the good work so far on this!

Edit- This script is able to recognize all of my chats, but is unable to delete messages from the one chat I run. https://github.com/seabasssec/del_my_messages_from_chat

Raw functions and types are changed in Pyrogram version >1

With Python3 latest version of Pyrogram is installed.

Following lines are related to the old version of Pyrogram and cause error on running script on Ubuntu version 18.04 and later:

from pyrogram.api.functions.messages import Search
from pyrogram.api.types import InputPeerSelf, InputMessagesFilterEmpty
from pyrogram.api.types.messages import ChannelMessages

This code should be used instead

from pyrogram.raw.functions.messages import Search
from pyrogram.raw.types import InputPeerSelf, InputMessagesFilterEmpty
from pyrogram.raw.types.messages import ChannelMessages

Please see the docs.

Some groups are listed twice

Example:

Delete all your messages in
  [...]
  19. Sci⛷🚠
  [...]
  22. Sci⛷🚠
  [...]
  163. Birthday group
  164. Birthday group
  [...]
  170. Friends group
  [...]
  185. Friends group
  186. EP 2016
  187. EP 2016

I confirm that I any of that groups has a duplicate with the same name.

Maybe related to #16

Question

In my telegram chat export, I have all the groups I ever joined, I have no messages in the groups except "xxx joined". I'm unable to delete these messages directly. They appear in the chat export. I want these groups gone. I just want to delete these kind of messages.
Can I remove these joined messages from groups even if I'm not the admin with your tool?

Add multiple account support

Implement ability to work with multiple accounts.

For CLI interactive mode we have to:

  • Remember last successful login and display that session as default
  • Display list of all available sessions
  • Let the user to add/remove sessions

For default argument based CLI (should be implemented in #11) simply add argument with name/path to client session file.

Sessions in pyrogram are stored in .session files which are simply SQlite database files. https://docs.pyrogram.org/topics/storage-engines.

get_dialogs() got an unexpected keyword argument 'pinned_only'

Traceback (most recent call last):
File "/root/telegram-delete-all-messages/cleaner.py", line 154, in
deleter.select_groups()
File "/root/telegram-delete-all-messages/cleaner.py", line 67, in select_groups
chats = self.get_all_chats()
File "/root/telegram-delete-all-messages/cleaner.py", line 57, in get_all_chats
dialogs = app.get_dialogs(pinned_only=True)
File "/usr/local/lib/python3.9/site-packages/pyrogram/sync.py", line 53, in async_to_sync_wrap
coroutine = function(*args, **kwargs)
TypeError: get_dialogs() got an unexpected keyword argument 'pinned_only'

image

How to fix it ?

Thks !

Program continuously sleeping, doesn't work properly

The program doesn't work properly and continuously sleeping. This problem occurred from today morning and didn't solve too now.

I am not sure if this is a problem on Telegram API side, below are the error message & screenshot, please help, thanks.

[client] Sleeping for 24s (required by "messages.GetDialogs")

image

[Feature Request] Delete messages in every group

I have joined lots of chats, so instead of deleting messages of groups one by one, I want to delete all of them at once.

Currently I run a bash shell loop to run the script, but the group number seems changes sometimes. so there are chance that I didn't delete messages in some groups.

Could you add an argument that specific something like -all for this? or answer ALL to delete messages in every group?

Some groups are not listed (seems happens when there are too many chats, I got these groups listed by quitting some groups and channels), I want to delete all of messages in every group I joined. To delete them I have to send new messages in these groups and then the python script will list it.
Seems it only list 50 groups at most.
Is it possible to find out all of groups I joined and delete them at once?

msg_id is too low error

pyrogram.errors.BadMsgNotification: [16] The msg_id is too low, the client time has to be synchronized.

Validate user input

Currently user can enter 0 or negative index of dialog. It's not the expected way of selection and could cause huge problems because deleted messages are not recoverable.

problem after number

Pyrogram v1.2.9, Copyright (C) 2017-2021 Dan https://github.com/delivrance
Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)

Traceback (most recent call last):
File "/home/kali/hack/telegram-delete-all-messages/cleaner.py", line 130, in
deleter.select_groups()
File "/home/kali/hack/telegram-delete-all-messages/cleaner.py", line 50, in select_groups
chats = self.get_all_chats()
File "/home/kali/hack/telegram-delete-all-messages/cleaner.py", line 42, in get_all_chats
dialog_chunk = app.get_dialogs()
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/sync.py", line 56, in async_to_sync_wrap
return loop.run_until_complete(coroutine)
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/methods/chats/get_dialogs.py", line 96, in get_dialogs
messages[chat_id] = await types.Message._parse(self, message, users, chats)
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/types/messages_and_media/message.py", line 720, in _parse
parsed_message.reply_to_message = await client.get_messages(
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/methods/messages/get_messages.py", line 114, in get_messages
r = await self.send(rpc, sleep_threshold=-1)
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/methods/advanced/send.py", line 77, in send
r = await self.session.send(
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/session/session.py", line 425, in send
return await self._send(data, timeout=timeout)
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/session/session.py", line 395, in _send
RPCError.raise_it(result, type(data))
File "/home/kali/.local/lib/python3.9/site-packages/pyrogram/errors/rpc_error.py", line 78, in raise_it
raise getattr(
pyrogram.errors.exceptions.bad_request_400.ChannelPrivate: [400 CHANNEL_PRIVATE]: The channel/supergroup is not accessible (caused by "channels.GetMessages")

Sorry if this is a bad way to ask

I didnt know who else to ask but I was wondering if you guys could make a similar python script to remove all members from all of your groups and channels. So you keep your groups but remove everyone else in them.

SyntaxError: invalid syntax

$ python cleaner.py
  File "cleaner.py", line 55
    print(f'  {i+1}. {group.title}')
                                  ^
SyntaxError: invalid syntax

I ran into above error running in Ubuntu

not working

i have pyrogram updated, but after i after i put the hash id, when i put the phone number, the program closes, it dont even send the code, a few months ago, it worked for me

someone knows what to do?

only delete 100 msg

running the script only shows that you have deleted 100 messages. When looking at the telegram group, all the messages disappear but after a while all the messages appear again except 100 messages

[bug] update pyrogram

pyrogram.errors.exceptions.not_acceptable_406.NotAcceptable: [406 Not Acceptable]: [406 UPDATE_APP_TO_LOGIN] (caused by "auth.SendCode")

"pyrogram.errors.exceptions.flood_420.FloodWait" when searching for messages, needs sleep_threshold set

I believe, sleep_threshold parameter should be added to app.send() method when searching messages, otherwise it dies sometimes with "pyrogram.errors.exceptions.flood_420.FloodWait". Value of 60 seconds seems to work OK.
test results:
[Q]
Found 40835 your messages in selected supergroup
[client] Sleeping for 15s (required by "messages.Search")
[client] Sleeping for 9s (required by "messages.Search")
[client] Sleeping for 3s (required by "messages.Search")
[client] Sleeping for 9s (required by "messages.Search")
Searching messages. OFFSET: 40900
[UQ]

ModuleNotFoundError

Tested on Windows 10 CMD, Log:

> install.bat

python -m venv venv
call venv/Scripts/activate.bat
Requirement already satisfied: pyrogram in c:\...\telegram-delete-all-messages-master\venv\lib\site-packages (from -r requirements.txt (line 1)) (1.0.7)
Requirement already satisfied: tgcrypto in c:\...\telegram-delete-all-messages-master\venv\lib\site-packages (from -r requirements.txt (line 2)) (1.2.2)
Requirement already satisfied: async-lru==1.0.2 in c:\...\telegram-delete-all-messages-master\venv\lib\site-packages (from pyrogram->-r requirements.txt (line 1)) (1.0.2)
Requirement already satisfied: pyaes==1.6.1 in c:\...\telegram-delete-all-messages-master\venv\lib\site-packages (from pyrogram->-r requirements.txt (line 1)) (1.6.1)
Requirement already satisfied: pysocks==1.7.1 in c:\...\telegram-delete-all-messages-master\venv\lib\site-packages (from pyrogram->-r requirements.txt (line 1)) (1.7.1)
"Successfully installed requirements!"
> start.bat

call venv/Scripts/activate.bat
Traceback (most recent call last):
  File "cleaner.py", line 5, in <module>
    from pyrogram.api.functions.messages import Search
ModuleNotFoundError: No module named 'pyrogram.api'

Not starting

Getting this error:
Traceback (most recent call last):
File "/Users/bm/telegram-delete-all-messages/cleaner.py", line 5, in
from pyrogram.api.functions.messages import Search
ModuleNotFoundError: No module named 'pyrogram.api'

ModuleNotFoundError: No module named 'pyrogram.api'

>python3 cleaner.py
Traceback (most recent call last):
  File "cleaner.py", line 5, in <module>
    from pyrogram.api.functions.messages import Search
ModuleNotFoundError: No module named 'pyrogram.api'
# python3 --version
Python 3.7.3
# pip3 list
Package            Version      
------------------ -------------
asn1crypto         0.24.0       
async-lru          1.0.2        
attrs              19.3.0       
certifi            2018.8.24    
chardet            3.0.4        
cloud-init         20.1         
configobj          5.0.6        
cryptography       2.6.1        
entrypoints        0.3          
httplib2           0.11.3       
idna               2.6          
importlib-metadata 1.6.0        
Jinja2             2.10         
jsonpatch          1.25         
jsonpointer        2.0          
jsonschema         3.2.0        
keyring            17.1.1       
keyrings.alt       3.1.1        
MarkupSafe         1.1.0        
oauthlib           3.1.0        
pip                18.1         
pyaes              1.6.1        
pycrypto           2.6.1        
pycurl             7.43.0.2     
PyGObject          3.30.4       
Pyrogram           1.1.13       
pyrsistent         0.16.0       
PySimpleSOAP       1.16.2       
PySocks            1.7.1        
python-apt         1.8.4.3      
python-debian      0.1.35       
python-debianbts   2.8.2        
pyxattr            0.6.1        
pyxdg              0.25         
PyYAML             3.13         
reportbug          7.5.3-deb10u1
requests           2.21.0       
SecretStorage      2.3.1        
setuptools         40.8.0       
six                1.12.0       
TgCrypto           1.2.2        
urllib3            1.24.1       
wheel              0.32.3       
youtube-dl         2019.1.17    
zipp               3.1.0        

Delete messages in normal groups?

Just wondering if it's possible to delete messages in a normal group please?

i.e. could chat.type be modified to look for another type of group, and if so do you know what other types there are please?

Option to delete all messages at once

With the current implementation it's pretty cumbersome to try and delete all messages at once. How hard would it be to add that as a selectable option where it iterates over each group and deletes the messages automagically?

FIX - flood warning

for fix this error:

pyrogram.errors.exceptions.flood_420.FloodWait: [420 FLOOD_WAIT_X]: A wait of 21 seconds is required (caused by "messages.Search")

do you change number of count from 100 tu 1000

for i in range(0, messages_count, 1000):
                q = self.search_messages()
                self.update_ids(q)
                self.add_offset += 1000

Suggestion: Interactive Mode

Hi,

Currently the program seems to exit after deletion; in the case a user may want to delete a few groups, perhaps the program can prompt for another group after deleting the first?

Thanks!

Needs tests

Since there are currently no tests, it's hard to judge whether a new contribution will introduce subtle bugs / break currently supported functionality. It's basically up to the developer and/or the reviewer to manually test the tool in all supported scenarios as best as they can.

I'm working on a test suite with some unit tests and functional tests, which authenticate the API and create new test groups / supergroups, send random messages to those groups, and then delete those messages, and finally clean up and delete the test groups.

This should make testing and reviewing changes much safer.

Update repo description

Wouldn't Delete all your messages in groups / supergroups with python script. be more accurate?

Error

i get the following message
TypeError: GetDialogs.get_dialogs() got an unexpected keyword argument 'pinned_only'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.