Run ที่เครื่องที่ออกเน็ต ได้ ในที่นี้ใช้เครื่อง Extranet
1. ติดตั้งโปรแกรม
- sudo apt-get install python-pip
2. ติดตั้ง Slack Client
- pip install slackclient
- from slackclient import SlackClient
- #from time import sleep
- import time
- #import datetime
- from datetime import datetime, timedelta
- legacy_token = 'xxxxxx' # don't if you share your code
- channel = 'Cxxxxx' # the id of the channel to delete all msgs inside
- sc = SlackClient(legacy_token)
- response = sc.api_call('channels.info', channel=channel)
- #oldest_ts = response['channel']['created']
- date_days_ago = datetime.now() - timedelta(days=60)
- oldest_ts = time.mktime(date_days_ago.timetuple())
- print(datetime.fromtimestamp(oldest_ts))
- date_days_ago = datetime.now() - timedelta(days=30)
- 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)
- 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 = 'Cxxxxx'
- sc = SlackClient(legacy_token)
- response = sc.api_call('channels.info', channel=channel)
- #oldest_ts = response['channel']['created']
- date_days_ago = datetime.now() - timedelta(days=60)
- oldest_ts = time.mktime(date_days_ago.timetuple())
- print(datetime.fromtimestamp(oldest_ts))
- date_days_ago = datetime.now() - timedelta(days=30)
- 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)
- 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)
5. สร้าง Cront ให้ run ตามที่ต้องการในที่นี้ run อาทิตย์ละครั้ง วันจันทร์
0 0 * * 1 python /home/pi/removeSlak.py >> /home/pi/removeSlack.log 2>&1
No comments:
Post a Comment