Skip to content

Instantly share code, notes, and snippets.

@recuraki
Last active March 1, 2021 14:39
Show Gist options
  • Save recuraki/c3dc90571ef80d31ce43df2288b7060f to your computer and use it in GitHub Desktop.
Save recuraki/c3dc90571ef80d31ce43df2288b7060f to your computer and use it in GitHub Desktop.
"""
これなに?
Slackのあなたの書き込みを消します。
クエリの調べ方
https://api.slack.com/methods/pins.list
とか見て、適当にTester画面からクエリ送ります。
URLでるので、良しなに書き換えましょう
準備
https://api.slack.com/apps/
で create newappして、SlackappをWorkSpaceに登録します
OAuth & Permissionsを開いて、
user token scopesで
channels:read
chat:write
im:read
mpim:read
search:read
を与えます。権限変えたら、
使い方
1. tokenにxoxp-4123.....とかを入れます(上の画面のUser OAuth Tokenです)
2. yourhandleに自分の名前 taroとかを入れます
3. channelnameに適当に名前を入れます (utf-8で日本語名も入ります)
4. 走らせます
5. 消していいなら"YES"と入力
Q: 1回に20件しか消せないんだけど?
A: countを適当にいじります。
Q: 何回も実行するの面倒
A: 全体をwhile1してlen(eraseCandidates) == 0, breakとかしましょう
Q: 遅い
A: sleepいじればいいけど、50/minのAPIなので適当にやってください
"""
import requests
import sys
url = "https://slack.com/api/search.messages"
token = "xoxp-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" #################################
yourhandle = "kanaikanai" ###############################################
channelname="general" ####################################
count="100"
from pprint import pprint
headers = {'Authorization': 'Bearer ' + token}
response = requests.post("https://slack.com/api/search.messages?count="+count+"&query=from%3A%40" + yourhandle + "%20in%3A"+channelname+"&pretty=1", headers=headers)
# DM含めて全部消していいなら 以下のクエリ(in:channelがないです)
#response = requests.post("https://slack.com/api/search.messages?count="+count+"&query=from%3A%40" + yourhandle + "&pretty=1", headers=headers)
json_data = response.json()
blocks = json_data["messages"]["matches"]
eraseCandidates = []
for block in blocks:
thisChannel = block["channel"]["id"]
thisTs = block["ts"]
thisText = block["text"]
eraseCandidates.append((thisChannel, thisTs, thisText))
for x in eraseCandidates:
print(x)
print(">>>> Delete All? type [YES]" )
s = input()
if s != "YES":
print("Abort")
sys.exit(1)
from time import sleep
for x in eraseCandidates:
print("erase", x[2])
sleep(2)
response = requests.post("https://slack.com/api/chat.delete?channel={0}&ts={1}&pretty=1".format(x[0], x[1])
,headers=headers)
print(response.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment