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

slackメッセージの一括削除(python3)

More than 1 year has passed since last update.

Slackの無料枠では10000メッセージまでしか見ることが(検索も)出来ない。
警告メッセージが来ていて困ったなーと。
リマインドなど無駄なメッセージを削除して、何とかならないかとおもったら、まさにQiitaでの投稿が。

https://qiita.com/ono0708/items/b517cc83ec84bb787456

早速下記からトークンを発行。

https://api.slack.com/custom-integrations/legacy-tokens

ここで削除対象とするチャネルのIDを確認。

https://api.slack.com/methods/channels.list/test?

早速、Qiitaのソースを実行してみるとpython3では動かないことが判明。
確かにpython2.7って書いてある。
せっかくなのでソースをpython3に書き直してみる。
下記は取得したメッセージがBotだったら削除するようにしている。

#coding: utf-8

import urllib.request
import urllib.parse
import json
import time

hist_url = "https://slack.com/api/channels.history"
delete_url = "https://slack.com/api/chat.delete"

token = 'ここにトークン'
channel = 'ここにチャネルのID'

hist_params = {
    'channel' : channel,
    'token' : token,
    'count' : '200'
}

req = urllib.request.Request(hist_url)
hist_params = urllib.parse.urlencode(hist_params).encode('ascii')
req.data = hist_params

res = urllib.request.urlopen(req)

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

for m in data['messages']:
    if (m['user'] == 'USLACKBOT'):
        print(m)
        delete_params = {
            'channel' : channel,
            'token' : token,
            'ts' :  m["ts"]
        }
        req = urllib.request.Request(delete_url)
        delete_params = urllib.parse.urlencode(delete_params).encode('ascii')
        req.data = delete_params

        res = urllib.request.urlopen(req)
        body = res.read()

        print(body)

        time.sleep(2)
maruga
アウトプットをしたいと思っている技術者。
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
No 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
ユーザーは見つかりませんでした