Skip to content

Instantly share code, notes, and snippets.

@zachleigh
Last active December 5, 2017 03:49
Show Gist options
  • Save zachleigh/6f1f1d1365ad9d3e1d5fd8e67660b404 to your computer and use it in GitHub Desktop.
Save zachleigh/6f1f1d1365ad9d3e1d5fd8e67660b404 to your computer and use it in GitHub Desktop.

Revisions

  1. zachleigh revised this gist Dec 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SlackFileDeleter.php
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ class SlackFileDeleter
    *
    * @var string
    */
    protected $domain = 'your Slack domain;
    protected $domain = 'your Slack domain';

    /**
    * Start time for delete. For example, if set to '-60 days' all files older
  2. zachleigh renamed this gist Dec 5, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. zachleigh created this gist Dec 5, 2017.
    74 changes: 74 additions & 0 deletions SlackFileDeleter
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    <?php

    use GuzzleHttp\Client;

    class SlackFileDeleter
    {
    /**
    * Slack API token.
    *
    * @var string
    */
    protected $token = 'your api token';

    /**
    * Slack domain.
    *
    * @var string
    */
    protected $domain = 'your Slack domain;

    /**
    * Start time for delete. For example, if set to '-60 days' all files older
    * than 60 days will be deleted.
    *
    * @var string Valid strtotime string.
    */
    protected $time = '-60 days';

    /**
    * Clear the slack file backlog.
    */
    public function cleanAction(array $params)
    {
    $data = [
    'token' => $this->token,
    'ts_to' => strtotime($this->time),
    ];

    $client = new Client();

    $response = $client->request(
    'post',
    'https://slack.com/api/files.list',
    ['form_params' => $data]
    );

    $files = json_decode($response->getBody(), true)['files'];

    if ($files != 0) {
    foreach ($files as $file) {
    echo "Deleting file {$file['name']} ..." . PHP_EOL;

    $timestamp = strtotime(time());

    $delete_data = [
    'token' => $this->token,
    'file' => $file['id'],
    'set_active' => 'true',
    '_attempts' => 1,
    ];

    $client = new Client();

    $response = $client->request(
    'post',
    "https://{$this->domain}.slack.com/api/files.delete?t={$timestamp}",
    ['form_params' => $delete_data]
    );
    }

    echo "Done!" . PHP_EOL;
    }
    }
    }