Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration guides to slack_cleaner2 #79

Closed
sgratzl opened this issue May 7, 2020 · 20 comments
Closed

migration guides to slack_cleaner2 #79

sgratzl opened this issue May 7, 2020 · 20 comments
Assignees
Labels

Comments

@sgratzl
Copy link
Owner

sgratzl commented May 7, 2020

Slack Cleaner v2 at https://github.com/sgratzl/slack_cleaner2 is an improved version of this slack cleaner script. It is easier to maintain and more powerful. However, instead of a single script to execute it is a Python library module. Thus, it requires to code in Python.

This issue is a collection of common examples in slack-cleaner and their counterpart in slack_cleaner2

In each example <TOKEN> needs to be replaced by the corresponding Slack API token

@sgratzl sgratzl self-assigned this May 7, 2020
@sgratzl sgratzl pinned this issue May 7, 2020
@sgratzl
Copy link
Owner Author

sgratzl commented May 7, 2020

Show Information about the Slack Workspace

slack-cleaner --token <TOKEN> --info

will become

info.py

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>")
print('users', s.users)
print('public channels', s.channels)
print('private channels', s.groups)
print('instant messages', s.ims)
print('multi user direct messages', s.mpim)
python info.py

@sgratzl
Copy link
Owner Author

sgratzl commented May 7, 2020

Delete all messages from the general channel

slack-cleaner --token <TOKEN> --rate 1 --message --channel general --user "*"

will become

clean_general.py

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)
for msg in s.c.general.msgs(with_replies=True):
  msg.delete()
python clean_general.py

@sgratzl
Copy link
Owner Author

sgratzl commented May 7, 2020

Delete all messages from bots (especially flooding CI updates)

slack-cleaner --token <TOKEN> --rate 1 --message --channel auto-build --bot

will become

clean_bot.py

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)
for msg in s.c['auto-build'].msgs():
  if msg.bot:
    msg.delete()
python clean_bot.py

@sgratzl
Copy link
Owner Author

sgratzl commented May 7, 2020

Delete all messages older than 2015/09/19

slack-cleaner --token <TOKEN> --rate 1 --message --channel general --user "*" --before 20150919

will become

clean_general_before2015.py

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)
for msg in s.c.general.msgs(before="20150919", with_replies=True):
  msg.delete()
python clean_general_before2015.py

@jpwynn
Copy link

jpwynn commented Jun 14, 2020

what if user is "sam" instead of "*" which seems to be the default? IS there a way to filter for that?

@sgratzl
Copy link
Owner Author

sgratzl commented Jun 14, 2020

Delete all messages in channel general by user named sam but keep pinned messages

slack-cleaner --token <TOKEN> --rate 1 --message --channel general --user "sam" --keeppinned

will become

clean_general_sam.py

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)
for msg in s.c.general.msgs(with_replies=True):
  if msg.user.name == 'sam' and not msg.pinned_to:
    msg.delete()
python clean_general_sam.py

alternative using predicate syntax (untested)

clean_general_sam_predicate.py

from slack_cleaner2 import SlackCleaner, is_not_pinned, match_user
s = SlackCleaner("<TOKEN>", sleep_for=1)
for msg in filter(is_not_pinned() and match_user('sam'), s.c.general.msgs(with_replies=True)):
  msg.delete()

@Freelensia
Copy link

Freelensia commented Jul 24, 2020

Hi @sgratzl is there a way to clean msg in multiple channels instead of repeating the entire script?
And btw I stumbled on this issue only by chance! These sample commands should be in the guide of the slack-cleaner2 github page.

@sgratzl
Copy link
Owner Author

sgratzl commented Jul 24, 2020

Delete all messages in all channels

slack-cleaner --token <TOKEN> --rate 1 --message --channel ".*" --regex --user "*"

will become

clean_all.py

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)

for channel in s.conversations:
  for msg in channel.msgs(with_replies=True):
    msg.delete()
python clean_all.py

@orappold
Copy link

Hey sgratzl,
would you know a smart way to only delete message with no replies, e.g. no Thread has been started? The challenge I guess is to completely exclude a thread (parent + child entries) from the deletion process. I was not able to come up with a solution for that. Would be happy to hear your opinion!

@sgratzl
Copy link
Owner Author

sgratzl commented Jul 28, 2020

Hey sgratzl,
would you know a smart way to only delete message with no replies, e.g. no Thread has been started? The challenge I guess is to completely exclude a thread (parent + child entries) from the deletion process. I was not able to come up with a solution for that. Would be happy to hear your opinion!

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)

for channel in s.conversations:
  for msg in channel.msgs():
    if not next(msg.replies(), None): # replies are empty
       msg.delete()

or using some internal knowledge what makes message starting a thread start

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)

for channel in s.conversations:
  for msg in channel.msgs():
    if 'thread_ts' not in msg.json: #reply thread_ts info is not set
       msg.delete()

@leonard84
Copy link

Why not offer both, i.e., users that need more control code in python users that just want to delete message from a channel use the script args and this is internally mapped to what you've written here?

@rogerdahl
Copy link

Delete all files:

#!/usr/bin/env python

from slack_cleaner2 import SlackCleaner

s = SlackCleaner('<TOKEN (xoxp-...)>')

for file in s.files():
    print(f'Deleting: {file}')
    file.delete()

@rogerdahl
Copy link

rogerdahl commented Nov 23, 2020

Why not offer both, i.e., users that need more control code in python users that just want to delete message from a channel use the script args and this is internally mapped to what you've written here?

Agreed - adding a thin command line client on top of the library, that handles common cases seems like it would take just about the same amount of time as listing the code for the common cases on this page.

At the same time, I really appreciate what @sgratzl has already provided for us here.

@ghost
Copy link

ghost commented Dec 5, 2020

Ok

@aeron7
Copy link

aeron7 commented Mar 19, 2021

Hi @sgratzl is there a way to clean msg in multiple channels instead of repeating the entire script?
And btw I stumbled on this issue only by chance! These sample commands should be in the guide of the slack-cleaner2 github page.

channelvars = ['daytraders','basket']

for channelvar in channelvars:
  for msg in s.c[channelvar].msgs(with_replies=True):      
    msg.delete()

@nasalaika
Copy link

nasalaika commented Apr 21, 2021

I'm wondering if someone would be kind enough to post an example of deleting a whole thread of direct (instant) messages with another specific user, say Leroy? Thanks in advance - I'm only half able to understand the available examples and my python skills are not what I'd hope they'd be at the moment...

Below is what I've been attempting, but I think there's loads wrong....

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("TOKEN_HASH_HERE", sleep_for=1)

for msg in s.ims.leroy.msgs(with_replies=True):
  msg.delete()

@k4rtik
Copy link

k4rtik commented Apr 21, 2021

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)
for msg in s.c['auto-build'].msgs():
  if msg.bot:
    msg.delete()

Is there a way to specify botname here? the msg object doesn't seem to have that attribute.

AttributeError: 'SlackMessage' object has no attribute 'botname'

@jameshibbard
Copy link

jameshibbard commented Jun 21, 2021

I'm wondering if someone would be kind enough to post an example of deleting a whole thread of direct (instant) messages with another specific user, say Leroy?

@nasalaika: you can do it like this.

from slack_cleaner2 import SlackCleaner
s = SlackCleaner(<TOKEN>, sleep_for=1)

for c in s.ims:
  if c.name == 'leroy':
    for msg in c.msgs():
      if msg.user == s.myself:
        msg.delete()

Bear in mind that the Slack API doesn't permit the deletion of direct messages that are not written by you, so Leroy would have to delete his half of the conversation.

@sgratzl
Copy link
Owner Author

sgratzl commented Jun 22, 2021

'NoneType' object has no attribute 'msgs' why see this?

seems like it couldn't find the channel named auto-build

@devaeterne
Copy link

Hey sgratzl,
would you know a smart way to only delete message with no replies, e.g. no Thread has been started? The challenge I guess is to completely exclude a thread (parent + child entries) from the deletion process. I was not able to come up with a solution for that. Would be happy to hear your opinion!

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)

for channel in s.conversations:
  for msg in channel.msgs():
    if not next(msg.replies(), None): # replies are empty
       msg.delete()

or using some internal knowledge what makes message starting a thread start

from slack_cleaner2 import SlackCleaner
s = SlackCleaner("<TOKEN>", sleep_for=1)

for channel in s.conversations:
  for msg in channel.msgs():
    if 'thread_ts' not in msg.json: #reply thread_ts info is not set
       msg.delete()

cannot delete entry: xxxxx:1624088712.0007 (xxxxxxxx (xxxxxxxxxxxxx) SERHAT BARLAS) cant_delete_message

When I want to delete all messages, I cannot delete the messages of the error user

@sgratzl sgratzl closed this as completed Jun 24, 2021
Repository owner locked and limited conversation to collaborators Jun 24, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests