Last active
March 26, 2019 07:30
-
-
Save shinyaoguri/3d6ba03a3a72f99fb62e497693c4f693 to your computer and use it in GitHub Desktop.
This is a Python script to delete Slack unnecessary messages. When the suffix of the channel name is "_bot", deletes the message that has passed 48 hours or more.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests | |
datetime | |
timedelta | |
python-dotenv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
# _で始まるチャンネル →削除しない | |
# 普通のチャンネル →1ヶ月で削除 | |
# _botで終わるチャンネル →48時間で削除 | |
import os | |
import sys | |
from os.path import join, dirname | |
from dotenv import load_dotenv | |
import requests | |
import json | |
from time import sleep | |
from datetime import datetime, timedelta | |
import re | |
SLACK_ACCESS_TOKEN = "" | |
LOG_CHANNEL_NAME = "" | |
LOG_CHANNEL_ID = "" | |
def delete_channel_message(ch_dict, limit_date): | |
global SLACK_ACCESS_TOKEN | |
log_message = "" | |
#各botチャンネルで48時間以上経過した投稿を削除する | |
for del_ch in ch_dict: | |
del_ch_id = del_ch[0] | |
del_ch_name = del_ch[1] | |
del_message_count = 0 | |
#チャンネルから1000件のメッセージを取得(これが最大値) | |
res = requests.get('https://slack.com/api/channels.history?token=' + SLACK_ACCESS_TOKEN + '&channel=' + del_ch_id + '&count=' + '1000') | |
channel_messages = json.loads(res.text) | |
log_message = log_message + "\"" + del_ch_name + "\" has " + str(len(channel_messages['messages'])) + " msg." | |
print("\"" + del_ch_name + "\" has " + str(len(channel_messages['messages'])) + " messages") | |
# 各メッセージについて経過時間を確認 | |
now = datetime.now() | |
for ch_message in channel_messages['messages']: | |
message_timestamp = datetime.fromtimestamp(float(ch_message['ts'])) | |
#2日以上経過した投稿だった場合削除する | |
if (now - message_timestamp) > timedelta(days=limit_date): | |
del_message_count = del_message_count + 1 | |
requests.post('https://slack.com/api/chat.delete?token=' + SLACK_ACCESS_TOKEN + '&channel=' + del_ch_id + '&ts=' + ch_message['ts']) | |
sleep(1) | |
log_message = log_message + " " + str(del_message_count) + " messages has been deleted\n" | |
return log_message | |
def main(): | |
global SLACK_ACCESS_TOKEN | |
global LOG_CHANNEL_NAME | |
global LOG_CHANNEL_ID | |
dotenv_path = join(dirname(__file__), '.env') | |
load_dotenv(dotenv_path) | |
SLACK_ACCESS_TOKEN = os.environ.get("SLACK_ACCESS_TOKEN") | |
LOG_CHANNEL_NAME = os.environ.get("SLACK_LOG_CHANNEL_NAME") | |
log_message = "---\n" | |
ch_dict = {} | |
print("start at " + str(datetime.now())) | |
# Slackからチャンネル一覧を取得 | |
res = requests.get('https://slack.com/api/channels.list?token=' + SLACK_ACCESS_TOKEN) | |
channels_list = json.loads(res.text) | |
# チャンネルIDとチャンネル名の辞書を生成 | |
for channel in channels_list['channels']: | |
ch_dict[channel['id']] = channel['name'] | |
# suffixが"_bot"となっているチャンネルを取得 | |
bot_channels = [ch for ch in ch_dict.items() if re.match(r".*_bot$", ch[1])] | |
# prefixが"_"となっているチャンネルを取得 | |
unimportant_channels = [ch for ch in ch_dict.items() if not (re.match(r"^_", ch[1]) or re.match(r"^general$", ch[1]) or re.match(r".*_bot$", ch[1]) )] | |
# log出力先のチャンネルを特定(存在しないチャンネル名だとエラーになる) | |
log_channel_name = LOG_CHANNEL_NAME | |
log_channel = [ch for ch in ch_dict.items() if re.match(r"^%s$" % log_channel_name, ch[1])] | |
LOG_CHANNEL_ID = log_channel[0][0] | |
#各botチャンネルで特定の期間以上経過した投稿を削除する | |
print("bot") | |
tmp_log_msg = delete_channel_message(bot_channels, 1) | |
log_message = log_message + tmp_log_msg | |
print("unimportant") | |
tmp_log_msg = delete_channel_message(unimportant_channels, 14) | |
log_message = log_message + tmp_log_msg | |
requests.post('https://slack.com/api/chat.postMessage?token=' + SLACK_ACCESS_TOKEN + '&channel=' + LOG_CHANNEL_ID + '&text=' + log_message) | |
print("done at " + str(datetime.now())) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment