10/11/2021

Code Auto Remove Slack เปลี่ยน API ใหม่ Code เดิมลบไม่ได้

Python Auto Remove Slack Chet History.

 เปลี่ยน API

  1. response = sc.api_call('conversations.history', channel=channel, limit=1000, oldest=oldest_ts, latest=latest_ts)


Code สำเร็จ
  1.  
  2.  
  3. from slackclient import SlackClient
  4. #from time import sleep
  5. import time
  6. #import datetime
  7. from datetime import datetime, timedelta
  8.  
  9. legacy_token = 'XXXX'  # don't if you share your code
  10. ########## Chanel Camera ##############
  11. channel = 'XXXX'  # Camera Channel.
  12.  
  13. sc = SlackClient(legacy_token)
  14. response = sc.api_call('channels.info', channel=channel)
  15.  
  16. #oldest_ts = response['channel']['created']
  17. date_days_ago = datetime.now() - timedelta(days=90)
  18. oldest_ts = time.mktime(date_days_ago.timetuple())
  19. print(datetime.fromtimestamp(oldest_ts))
  20.  
  21. #date_days_ago = datetime.now() - timedelta(days=60) # Keep File On Slack 60 Day.
  22. date_days_ago = datetime.now() - timedelta(days=7) # Keep File On Slack 7 Day.
  23. latest_ts = time.mktime(date_days_ago.timetuple())
  24. print(datetime.fromtimestamp(latest_ts))
  25.  
  26. #response = sc.api_call('channels.history', channel=channel, count=1000, oldest=oldest_ts, latest=latest_ts)
  27. response = sc.api_call('conversations.history', channel=channel, limit=1000, oldest=oldest_ts, latest=latest_ts)
  28. allmsgs = [item['ts'] for item in response['messages']]
  29. print(len(allmsgs))
  30. for msg in allmsgs:
  31.     sc.api_call('chat.delete', channel=channel, ts=msg)
  32.     time.sleep(1)
  33.  
  34. ########### Chanel Server  ###################
  35. channel = 'XXXXX' # Server Channel.
  36.  
  37. sc = SlackClient(legacy_token)
  38. response = sc.api_call('channels.info', channel=channel)
  39.  
  40. #oldest_ts = response['channel']['created']
  41. date_days_ago = datetime.now() - timedelta(days=90)
  42. oldest_ts = time.mktime(date_days_ago.timetuple())
  43. print(datetime.fromtimestamp(oldest_ts))
  44.  
  45. date_days_ago = datetime.now() - timedelta(days=60)
  46. latest_ts = time.mktime(date_days_ago.timetuple())
  47. print(datetime.fromtimestamp(latest_ts))
  48. #response = sc.api_call('channels.history', channel=channel, count=1000, oldest=oldest_ts, latest=latest_ts)
  49. response = sc.api_call('conversations.history', channel=channel, limit=1000, oldest=oldest_ts, latest=latest_ts)
  50. allmsgs = [item['ts'] for item in response['messages']]
  51. print(len(allmsgs))
  52. for msg in allmsgs:
  53.     sc.api_call('chat.delete', channel=channel, ts=msg)
  54.     time.sleep(1)