Created
June 10, 2021 06:27
-
-
Save robbintt/6190a3fb92a289200cf35b192bf75787 to your computer and use it in GitHub Desktop.
clean all your slack messages, slack_cleaner2 (pip) isn't great so it's hacked together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' slack_cleaner2 in pip, also on github | |
I used an admin account. I'm not sure what perms are required but I installed an app in the space, then added permissions needed to delete my own messages. | |
''' | |
import logging | |
import os | |
import time | |
from slack_cleaner2 import SlackCleaner, match_user | |
# make sure you have enough perms to delete all this stuff... good luck, see online guides | |
usertoken = "" | |
sleeptime = 1 | |
# slack workspace username only | |
target_user_aliases = ['your_username'] | |
# cache what happened so you can debug later, mine was about 70MB | |
logname = 'msgs_deleted.log' | |
# cache these because this script crashes and needs restarted | |
finished_conversations_file = "finished_conversations.txt" | |
def delete_everything(s, finished_conversations): | |
''' | |
''' | |
for channel in s.conversations: | |
if channel.name not in finished_conversations: | |
time.sleep(1) # getting server disconnects, think it's some sort of ddos intermediate rate limiting | |
# delete all user messages | |
for msg in channel.msgs(with_replies=True): | |
# skip if not user's message | |
try: | |
if msg.user.name not in target_user_aliases: | |
continue #skip | |
except AttributeError: | |
logging.warning("Message has no user '{}'".format(msg)) | |
continue #skip | |
# delete it | |
logging.warning("Deleting message for user {}: {}".format(msg.user.name, msg)) | |
print("Deleting message for user {}: {}".format(msg.user.name, msg)) | |
msg.delete() | |
''' | |
# delete it - require cli validation | |
answer = None | |
while answer not in ['y', 'n']: | |
print("Deleting message: {}".format(m)) | |
answer = input("Delete the message? Answer 'y' or 'n': ") | |
if answer == 'y': | |
logging.warning("Deleting message for user {}: {}".format(m.user.name, m)) | |
print("Deleting message for user {}: {}".format(m.user.name, m)) | |
m.delete | |
''' | |
# repeat for files | |
for _file in channel.files(): | |
# skip if not user's file | |
try: | |
if _file.user.name not in target_user_aliases: | |
continue #skip | |
except AttributeError: | |
logging.warning("Message has no user '{}'".format(_file)) | |
continue #skip | |
# delete it | |
logging.warning("Deleting message for user {}: {}".format(_file.user.name, _file)) | |
print("Deleting message for user {}: {}".format(_file.user.name, _file)) | |
_file.delete() | |
''' | |
# delete it - require cli validation | |
answer = None | |
while answer not in ['y', 'n']: | |
print("Deleting file: {}".format(_file)) | |
answer = input("Delete the file? Answer 'y' or 'n': ") | |
if answer == 'y': | |
logging.warning("Deleting file for user {}: {}".format(_file.user.name, _file)) | |
print("Deleting message for user {}: {}".format(_file.user.name, _file)) | |
_file.delete | |
''' | |
# finally, cache the name of the completed channel | |
with open(finished_conversations_file, 'a') as f: | |
f.write(channel.name + '\n') | |
finished_conversations.append(channel.name) | |
else: | |
print("Channel completed already: {}".format(channel.name)) | |
def main(): | |
''' | |
''' | |
# load up finished_conversations from cache file | |
finished_conversations = [] | |
# clever way to make file if not exists: https://stackoverflow.com/a/30021479 | |
mode = 'r' if os.path.exists(finished_conversations_file) else 'a' | |
with open(finished_conversations_file, mode) as f: | |
if mode == 'r': | |
for line in f.readlines(): | |
finished_conversations.append(line.strip()) | |
# set up | |
logging.basicConfig(filename=logname, level=logging.WARNING) | |
s = SlackCleaner(usertoken, sleep_for=sleeptime) | |
# keep trying to find messages, files across all remaining conversations visible to the user | |
while True: | |
try: | |
delete_everything(s, finished_conversations) | |
except Exception as e: | |
# for some reason the connection errors aren't caught, so restarts are still needed | |
print("It broke, restarting in 15 seconds.") | |
logging.error(e) | |
time.sleep(15) # in case I am getting kicked off the API for a silent too many requests | |
continue | |
else: | |
break | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment