Last active
May 14, 2018 02:10
-
-
Save alpinstang/7a65a79026488a97fcc7922d234fea4e to your computer and use it in GitHub Desktop.
Delete Slack files older than 30 days AWS Lambda ready. Just add a CloudFormation rule to run this periodically. Rewrite of https://gist.github.com/jackcarter/d86808449f0d95060a40 which is a rewrite of https://gist.github.com/jamescmartinez/909401b19c0f779fc9c1
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
import os | |
from botocore.vendored import requests | |
import time | |
import json | |
# set an environment variable when creating your lambda for your Slack key: | |
token = os.environ['LEGACY_KEY'] | |
#Delete files older than 30 days: | |
ts_to = int(time.time()) - 30 * 24 * 60 * 60 | |
def list_files(): | |
params = { | |
'token': token | |
,'ts_to': ts_to | |
,'count': 1000 | |
} | |
uri = 'https://slack.com/api/files.list' | |
response = requests.get(uri, params=params) | |
return json.loads(response.text)['files'] | |
def delete_files(file_ids): | |
count = 0 | |
num_files = len(file_ids) | |
for file_id in file_ids: | |
count = count + 1 | |
params = { | |
'token': token | |
,'file': file_id | |
} | |
uri = 'https://slack.com/api/files.delete' | |
response = requests.get(uri, params=params) | |
print count, "of", num_files, "-", file_id, json.loads(response.text)['ok'] | |
def lambda_handler(event, context): | |
files = list_files() | |
file_ids = [f['id'] for f in files] | |
delete_files(file_ids) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment