pythonで、Slackプライベートチャネルのメッセージを削除

Slackの無料枠で使っているため、トレードの通知は定期的にプログラムで消しています。

トレード用の通知はプライベートチャネルを使っているため、以下のサイトのをプライベート用に修正して使っています。

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

トークンの発行

参考 Legacy tokensslack API

プライベートチャネル名を指定して削除する場合

コード

#coding: utf-8

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

list_url = "https://slack.com/api/groups.list"
hist_url = "https://slack.com/api/groups.history"
delete_url = "https://slack.com/api/chat.delete"

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

list_params = {
    'token' : token
}

req = urllib.request.Request(list_url)
list_params = urllib.parse.urlencode(list_params).encode('ascii')
req.data = list_params

res = urllib.request.urlopen(req)

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

channel_id = ''
for currency in data['groups']:
    if currency['name'] == channel:
        channel_id = currency['id']

print('Channel Name:', channel, 'Channel ID:', channel_id)

if channel_id != '':
    hist_params = {
        'channel' : channel_id,
        '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)
    del_count = 0
    for m in data['messages']:
        if (m['username'] == 'ここにWebhookのユーザー名'):
            del_count += 1
            print('Delete count:', del_count)
            print(m)
            delete_params = {
                'channel' : channel_id,
                '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)

プライベートチャネルのIDを自分で調べて削除する場合

削除対象となるプライベートチャネルのIDを調べる

調べるには、トークンを先に取得してください。

参考 groups.listslack API

コード

プログラムは、200メッセージの削除になっています。

#coding: utf-8

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

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

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

hist_params = {
    'channel' : channel_id,
    '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['username'] == 'ここにWebhookのユーザー名'):
        print(m)
        delete_params = {
            'channel' : channel_id,
            '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)

Monappy: MBDQ39VHypMQwfyR8SshuHvfPNUz321F6B

Monacoinを投げる
モナゲ(tipmona)ってなに?
そもそもMonacoinってなに?

1 Comment

Kendrick

I have actually directly found that 7 - 8 strings each personal proxy
is the wonderful spot, again, if you are not scuffing target URLs from
search engines.

返信する

コメントを残す

メールアドレスが公開されることはありません。

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください