Help us understand the problem. What is going on with this article?

Slackのメッセージを一括削除するPythonコード

More than 1 year has passed since last update.

Slackの一括削除機能が昔はあったそうですが今探すとarchiveページ自体消滅していたようで、
仕方ないのでAPIですぐできそうだったのでささっと書きました。(python2.7であまりに雑なコードですが)
generalはチャンネルごと作り直せないので掃除したいときなどに需要あるかもしれないのでここに置いておきます。
少しいじれば特定の人のメッセージだけ消すなどもできると思います。

トークンはこちらから取得してください
https://api.slack.com/custom-integrations/legacy-tokens

チャンネルIDはここに必要な情報を入れると見れます(general等チャンネル名とは違うので注意)
https://api.slack.com/methods/channels.info/test

Slackを見ながら実行するとどんどん消えていくのが見えて面白いです。(?)
当たり前ですが一旦消すと復旧はできませんので予めエクスポートするなど用法用量を守ってご利用ください。

delete_slack_message.py
#coding: utf-8

import urllib
import urllib2
import json
import time

delete_url = "https://slack.com/api/chat.delete"
hist_url = "https://slack.com/api/channels.history"
token = '[yourtoken]'
channel = '[channel_id]'

delete_params = {'token':token,
          'channel':channel
}
hist_params = {'token':token,
          'channel':channel,
          'count' : '1000',
}

hist_params = urllib.urlencode(hist_params)

req = urllib2.Request(hist_url)
req.add_header('Content-Type', 'application/x-www-form-urlencoded')
req.add_data(hist_params)

res = urllib2.urlopen(req)

body = res.read()
data = json.loads(body)


for m in data["messages"]:
  delete_params['ts'] = m["ts"]
  enc_params = urllib.urlencode(delete_params)

  req = urllib2.Request(delete_url)
  req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  req.add_data(enc_params)


  res = urllib2.urlopen(req)

  body = res.read()
  print(body)
  #連続で送りすぎるとエラーになるので1秒待機
  time.sleep(1)                 
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away