Last active
January 26, 2021 08:10
-
-
Save kasperhartwich/dec5923ffcba55686dfb to your computer and use it in GitHub Desktop.
Script to delete old files from Slack
This file contains 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
#!/usr/bin/env php | |
<?php | |
date_default_timezone_set('GMT'); | |
if (count($argv)<2) { | |
echo $argv[0] . ' <token> <until>' . PHP_EOL; | |
echo 'Example: ' . $argv[0] . ' abcd-12345678-123456789-12345 \'-3 months\'' . PHP_EOL; | |
exit; | |
} | |
$token = $argv[1]; | |
$timestamp = isset($argv[2]) ? strtotime($argv[2]) : null; | |
function slack($function, $options = []) { | |
global $token; | |
$response = file_get_contents('https://slack.com/api/' . $function . '?' . http_build_query(array_merge(['token' => $token],$options))); | |
return json_decode($response, true); | |
} | |
$files = slack('files.list', ['ts_to' => $timestamp]); | |
foreach ($files['files'] as $file) { | |
$response = slack('files.delete', ['file' => $file['id']]); | |
echo 'file ' . $file['id'] . ' deleted: ' . $file['permalink'] . PHP_EOL; | |
} |
Hi man, i created my token test but, i change this
' abcd-12345678-123456789-12345 \'-3 months\''
for this
'xoxp-39012385831-30631xxxxx483-386021xxxxxxxx7-57f91xxx3x1xxx1x91b09b927dda54d47b \'-3 months\''
`#!/usr/bin/env php
<?php
date_default_timezone_set('GMT');
if (count($argv)<2) {
echo $argv[0] . ' <token> <until>' . PHP_EOL;
echo 'Example: ' . $argv[0] . 'xoxp-39012385831-30631xxxxx483-386021xxxxxxxx7-57f91xxx3x1xxx1x91b09b927dda54d47b \'-3 months\'' . PHP_EOL;
exit;
}
$token = $argv[1];
$timestamp = isset($argv[2]) ? strtotime($argv[2]) : null;
function slack($function, $options = []) {
global $token;
$response = file_get_contents('https://slack.com/api/' . $function . '?' . http_build_query(array_merge(['token' => $token],$options)));
return json_decode($response, true);
}
$files = slack('files.list', ['ts_to' => $timestamp]);
foreach ($files['files'] as $file) {
$response = slack('files.delete', ['file' => $file['id']]);
echo 'file ' . $file['id'] . ' deleted: ' . $file['permalink'] . PHP_EOL;
}`
print this
~$ php delSlack.php
delSlack.php
Example: delSlack.php39012385831-306315224483-386041219537-57006f911a131191b09b927dda54d47b '-3 months'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks man