Python Auto Remove Slack Chet History.
เปลี่ยน API
- response = sc.api_call('conversations.history', channel=channel, limit=1000, oldest=oldest_ts, latest=latest_ts)
Code สำเร็จ
- from slackclient import SlackClient
- #from time import sleep
- import time
- #import datetime
- from datetime import datetime, timedelta
- legacy_token = 'XXXX' # don't if you share your code
- ########## Chanel Camera ##############
- channel = 'XXXX' # Camera Channel.
- sc = SlackClient(legacy_token)
- response = sc.api_call('channels.info', channel=channel)
- #oldest_ts = response['channel']['created']
- date_days_ago = datetime.now() - timedelta(days=90)
- oldest_ts = time.mktime(date_days_ago.timetuple())
- print(datetime.fromtimestamp(oldest_ts))
- #date_days_ago = datetime.now() - timedelta(days=60) # Keep File On Slack 60 Day.
- date_days_ago = datetime.now() - timedelta(days=7) # Keep File On Slack 7 Day.
- latest_ts = time.mktime(date_days_ago.timetuple())
- print(datetime.fromtimestamp(latest_ts))
- #response = sc.api_call('channels.history', channel=channel, count=1000, oldest=oldest_ts, latest=latest_ts)
- response = sc.api_call('conversations.history', channel=channel, limit=1000, oldest=oldest_ts, latest=latest_ts)
- allmsgs = [item['ts'] for item in response['messages']]
- print(len(allmsgs))
- for msg in allmsgs:
- sc.api_call('chat.delete', channel=channel, ts=msg)
- time.sleep(1)
- ########### Chanel Server ###################
- channel = 'XXXXX' # Server Channel.
- sc = SlackClient(legacy_token)
- response = sc.api_call('channels.info', channel=channel)
- #oldest_ts = response['channel']['created']
- date_days_ago = datetime.now() - timedelta(days=90)
- oldest_ts = time.mktime(date_days_ago.timetuple())
- print(datetime.fromtimestamp(oldest_ts))
- date_days_ago = datetime.now() - timedelta(days=60)
- latest_ts = time.mktime(date_days_ago.timetuple())
- print(datetime.fromtimestamp(latest_ts))
- #response = sc.api_call('channels.history', channel=channel, count=1000, oldest=oldest_ts, latest=latest_ts)
- response = sc.api_call('conversations.history', channel=channel, limit=1000, oldest=oldest_ts, latest=latest_ts)
- allmsgs = [item['ts'] for item in response['messages']]
- print(len(allmsgs))
- for msg in allmsgs:
- sc.api_call('chat.delete', channel=channel, ts=msg)
- time.sleep(1)