@@ -0,0 +1,2175 @@
+import urllib%0Aimport urllib2%0Aimport time%0Aimport json%0A%0A%0A# load the configuration file%0Awith open('clean-slack-channel.conf', 'r') as f:%0A    config = json.load(f)%0A%0Atoken = config%5B'DEFAULT'%5D%5B'TOKEN'%5D # API Token for Slack%0Adays = config%5B'DEFAULT'%5D%5B'DAYS'%5D # delete items older than this%0Achannel_ids = config%5B'DEFAULT'%5D%5B'CHANNEL'%5D # Delete messages from this channel%0A%0A%0Ats_to = int(time.time()) - days * 24 * 60 * 60%0A%0A# find list of messages in channels%0Adef list_messages(channel_ids):%0A  for channel_id in channel_ids:%0A    params = %7B%0A      'token': token,%0A      'channel': channel_id,%0A      'latest': ts_to,%0A      'oldest': 1%0A    %7D%0A    uri = 'https://slack.com/api/channels.history'%0A    response = urllib2.urlopen(uri + '?' + urllib.urlencode(params)).read()%0A    messages = json.loads(response)%5B'messages'%5D%0A    messages_ids = %5Bf%5B'ts'%5D for f in messages%5D%0A    for message_id in messages_ids:%0A        delete_messages(channel_id, message_id) %0A%0A# delete messages that match criterea%0Adef delete_messages(channel_id, message_id):%0A  time.sleep(1)%0A  params = %7B%0A    'token': token,%0A    'channel': channel_id,%0A    'ts': message_id%0A  %7D%0A  uri = 'https://slack.com/api/chat.delete'%0A  response = urllib2.urlopen(uri + '?' + urllib.urlencode(params)).read()%0A  print 'Deleting', channel_id, message_id, json.loads(response)%5B'ok'%5D%0A%0A# find list of files%0Adef list_files():%0A  params = %7B%0A    'token': token,%0A    'ts_to': ts_to,%0A    'count': 1000%0A  %7D%0A  uri = 'https://slack.com/api/files.list'%0A  response = urllib2.urlopen(uri + '?' + urllib.urlencode(params)).read()%0A  print json.loads(response)%5B'files'%5D%0A  return json.loads(response)%5B'files'%5D%0A%0A# delete files that match criterea%0Adef delete_files(file_ids):%0A  count = 0%0A  num_files = len(file_ids)%0A  for file_id in file_ids:%0A    count = count + 1%0A    params = %7B%0A      'token': token,%0A      'file': file_id%0A      %7D%0A    uri = 'https://slack.com/api/files.delete'%0A    response = urllib2.urlopen(uri + '?' + urllib.urlencode(params)).read()%0A    print count, %22of%22, num_files, %22-%22, file_id, json.loads(response)%5B'ok'%5D%0A%0A# manage Messages%0Alist_messages(channel_ids)%0A%0A# Manage Files%0Afiles = list_files()%0Afile_ids = %5Bf%5B'id'%5D for f in files%5D%0Adelete_files(file_ids)%0A

Create clean-slack-channel.py