Instantly share code, notes, and snippets.

@lyrixx /run.php
Last active Sep 20, 2018

Embed
What would you like to do?
Slack delete all your files (rewrite of https://gist.github.com/jamescmartinez/909401b19c0f779fc9c1)
<?php
<<<CONFIG
packages:
- "kriswallsmith/buzz: ^0.15.0"
- "symfony/console: ^3.2@dev"
CONFIG;
// Find you token on https://api.slack.com/docs/oauth-test-tokens
use Buzz\Message\Response;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
(new Application())
->register('delete-files')
->addArgument('token', InputArgument::REQUIRED)
->setCode(function(InputInterface $input, OutputInterface $output) {
$token = $input->getArgument('token');
$validateResponse = function( $response) {
$responseDecoded = json_decode($response->getContent(), true);
if (!$responseDecoded) {
throw new \Exception('Something goes wrong. Unable to decode the response.');
}
if (!$responseDecoded['ok']) {
throw new \Exception('Something goes wrong. Error: '.$responseDecoded['error']);
}
return $responseDecoded;
};
$client = new Buzz\Browser();
// get user id
$response = $client->get('https://slack.com/api/auth.test?'.http_build_query([
'token' => $token,
]));
$responseDecoded = $validateResponse($response);
$userId = $responseDecoded['user_id'];
// get "all" files
$response = $client->get('https://slack.com/api/files.list?'.http_build_query([
'token' => $token,
'count' => 1000,
'user' => $userId,
]));
$responseDecoded = $validateResponse($response);
$total = $responseDecoded['paging']['total'];
// Validate
$q = new ConfirmationQuestion(sprintf('You are going to delete %d files, are you sure? [Y/n]', $total));
if (!$this->getHelper('question')->ask($input, $output, $q)) {
$output->writeln('Abort the mission');
return;
}
// delete
$bar = new ProgressBar($output, $total);
foreach ($responseDecoded['files'] as $file) {
$response = $client->get('https://slack.com/api/files.delete?'.http_build_query([
'token' => $token,
'file' => $file['id'],
]));
$validateResponse($response);
$bar->advance();
}
$output->writeln('Done');
})
->getApplication()
->setDefaultCommand('delete-files', true)
->run()
;
@lyrixx

This comment has been minimized.

Show comment
Hide comment
@lyrixx

lyrixx Aug 2, 2016

To be used with melody.

Usage: melody run https://gist.github.com/lyrixx/6f3a8de137c370d8b4b137330093aa3b -t -- <YOUR TOKEN HERE>

You can find you token here: https://api.slack.com/docs/oauth-test-tokens

Owner

lyrixx commented Aug 2, 2016

To be used with melody.

Usage: melody run https://gist.github.com/lyrixx/6f3a8de137c370d8b4b137330093aa3b -t -- <YOUR TOKEN HERE>

You can find you token here: https://api.slack.com/docs/oauth-test-tokens

@Dellybro

This comment has been minimized.

Show comment
Hide comment
@Dellybro

Dellybro Feb 28, 2017

I've followed this thread about slack deletion to this PHP one, which isn't bad since you hooked it up via command line, but why PHP. PHP is gonna be a dead language soon. Why do people still like and use PHP, biggest headaches ever come from PHP.

Dellybro commented Feb 28, 2017

I've followed this thread about slack deletion to this PHP one, which isn't bad since you hooked it up via command line, but why PHP. PHP is gonna be a dead language soon. Why do people still like and use PHP, biggest headaches ever come from PHP.

@lyrixx

This comment has been minimized.

Show comment
Hide comment
@lyrixx

lyrixx Mar 6, 2017

@Dellybro I don't care about the language. I just get shit done.

Owner

lyrixx commented Mar 6, 2017

@Dellybro I don't care about the language. I just get shit done.

@lhilton

This comment has been minimized.

Show comment
Hide comment
@lhilton

lhilton Mar 7, 2017

@lyrixx I wish you could retweet a github comment for that one. The question "Why would you use language FOO???" is almost always dumb... the answer is always "because it's what got the work done when I needed it done..."

lhilton commented Mar 7, 2017

@lyrixx I wish you could retweet a github comment for that one. The question "Why would you use language FOO???" is almost always dumb... the answer is always "because it's what got the work done when I needed it done..."

@florentdestremau

This comment has been minimized.

Show comment
Hide comment
@florentdestremau

florentdestremau May 16, 2017

screw you @Dellybro, PHP rocks ! 😝

florentdestremau commented May 16, 2017

screw you @Dellybro, PHP rocks ! 😝

@JeroenJochems

This comment has been minimized.

Show comment
Hide comment
@JeroenJochems

JeroenJochems commented Jun 22, 2017

Thanks @lyrixx!

@joshlewis

This comment has been minimized.

Show comment
Hide comment
@joshlewis

joshlewis Jun 5, 2018

Thank you @lyrixx!

PS> PHP forever.

joshlewis commented Jun 5, 2018

Thank you @lyrixx!

PS> PHP forever.

@abcarroll

This comment has been minimized.

Show comment
Hide comment
@abcarroll

abcarroll Jun 28, 2018

Yep, PHP is going to be dead. The language that powers most of the top 10 websites in the world, most popular blog framework in the world, most popular CMS's in the world, and is the most used server-side language for websites. Such a dead language.

Thank you @lyrixx. Mind if I fork this to a proper package?

abcarroll commented Jun 28, 2018

Yep, PHP is going to be dead. The language that powers most of the top 10 websites in the world, most popular blog framework in the world, most popular CMS's in the world, and is the most used server-side language for websites. Such a dead language.

Thank you @lyrixx. Mind if I fork this to a proper package?

@railto

This comment has been minimized.

Show comment
Hide comment
@railto

railto Sep 13, 2018

@Dellybro: Really? strange that, but then again if PHP does die then I can up my daily rates and make an absolute killing.

Just to make something clear, the biggest headaches come from people like yourself that bemoan other languages cause they don't fit with what you personally like!

railto commented Sep 13, 2018

@Dellybro: Really? strange that, but then again if PHP does die then I can up my daily rates and make an absolute killing.

Just to make something clear, the biggest headaches come from people like yourself that bemoan other languages cause they don't fit with what you personally like!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment