💔

Slackのメッセージを一括削除したい

2023/05/20に公開

いきさつ

Slackを運営する中で外部の人を招くことになった。#Generalにあまり公開したくない内容があったので、新しいプライベートチャンネルに内容をコピーしたが、メッセージの一括削除機能はないらしい。調べるとSlack APIを使ってまとめて削除するしかないらしい。

手順

  1. Slack APIの取得
  2. Pythonで処理を実行

Slack設定(API_token取得)

この記事を参考にさせてもらいました。Google Apps Scriptを使う記事ですが、APIの取得のところは参考になります。実は、スクリプトはこの通りにやってもエラーが出たのであきらめた次第です。
https://best-cloud.jp/slack-delete-all-posts/#GAS

特に注意するところは、User Token Scopeに以下二つを入れることですね。

  • channels:history
  • chat:write

削除APIのテスト

できたら、ここで試してみましょう。
https://api.slack.com/methods/chat.delete/test

各IDの検索の仕方は以下。

結果として「"ok": true」が返ってきたら成功です。

削除用スクリプト(Python)

こちらを参考にさせていただきました。
https://qiita.com/takatea/items/bb137e93c377c94c7532

削除する関数

DELMSG_URL = 'https://slack.com/api/chat.delete'
def slack_delmsg(channel, ts):
        headers = {'Authorization': 'Bearer ' + SLACK_USER_TOKEN,
                'Content-Type': 'application/json; charset=utf-8'}
        payload = {'channel': channel, 'ts': ts}
        req = requests.post(DELMSG_URL, data=json.dumps(payload), headers=headers)
        print(req.text)

投稿一覧を取得する関数(返信を含まない)

HISTORY_URL = "https://slack.com/api/conversations.history"
def slack_history(channel_id):
    header = {'Authorization': 'Bearer ' + SLACK_USER_TOKEN}
    payload  = {"channel" : "" + channel_id}
    res = requests.get(HISTORY_URL, headers=header, params=payload)
    json_data = res.json()
    messages = json_data["messages"]
    # print(res.json())
    return messages

返信一覧を取得する関数

REPLIES_URL = "https://slack.com/api/conversations.replies" 
def slack_replies(channel_id, thread_id):
    header={'Authorization': 'Bearer ' + SLACK_USER_TOKEN}
    payload  = {"channel" : "" + channel_id, "ts" : "" + thread_id}
    res = requests.get(REPLIES_URL, headers=header, params=payload)
    json_data = res.json()
    messages = json_data["messages"]
    # print(res.json())
    return messages

最終的なプログラム

import json
import requests
import time

HISTORY_URL = "https://slack.com/api/conversations.history"
DELMSG_URL = 'https://slack.com/api/chat.delete'
REPLIES_URL = "https://slack.com/api/conversations.replies" 
SLACK_USER_TOKEN = "xoxp-2827294579681-2838384521440-5294881600099-***"
channel_id = 'C02PYL*****'


def slack_delmsg(channel, ts):
        headers = {'Authorization': 'Bearer ' + SLACK_USER_TOKEN,
                'Content-Type': 'application/json; charset=utf-8'}
        payload = {'channel': channel, 'ts': ts}
        req = requests.post(DELMSG_URL, data=json.dumps(payload), headers=headers)
        print(req.text)


def slack_history(channel_id):
    header = {'Authorization': 'Bearer ' + SLACK_USER_TOKEN}
    payload  = {"channel" : "" + channel_id}
    res = requests.get(HISTORY_URL, headers=header, params=payload)
    json_data = res.json()
    messages = json_data["messages"]
    # print(res.json())
    return messages


def slack_replies(channel_id, thread_id):
    header={'Authorization': 'Bearer ' + SLACK_USER_TOKEN}
    payload  = {"channel" : "" + channel_id, "ts" : "" + thread_id}
    res = requests.get(REPLIES_URL, headers=header, params=payload)
    json_data = res.json()
    messages = json_data["messages"]
    # print(res.json())
    return messages


mes_history = slack_history(channel_id)
for i in mes_history:
    print(i["ts"])
    mes_replies = slack_replies(channel_id, i["ts"])
    for j in mes_replies:
        print("j_" + j["ts"])
        slack_delmsg(channel_id, j["ts"])
        time.sleep(0.5)
    slack_delmsg(channel_id, i["ts"])
    time.sleep(0.5)

Discussion